是否可以在Google文档中编写Google Apps脚本,以便从Google表格中检索非空白行的范围并将这些行显示为表格?
我正在寻找一个脚本,用于使用Google Apps脚本(我经验有限)将非空白行的数据从Google表格范围的单元格复制到Google文档中的表中。
要复制的数据似乎太大,无法直接链接到Google文档,因此从电子表格到文档的复制粘贴操作不会提示选择链接数据。
而且行数是动态的,因此固定范围不能解决问题。在电子表格中,我使用了SORTN
函数并将其设置为显示领带,因此范围的非空白行的大小发生了变化。
我从以下代码概述开始:
function myFunction() {
// Get Google Sheet data
var app = SpreadsheetApp;
var ss = app.openById('SHEET_ID');
var activeSheet = app.getActiveSpreadsheet();
var range = activeSheet.getRange("B4:D");
// Position to paste data in Google Docs
var doc = DocumentApp.getActiveDocument();
var body = DocumentApp.getActiveDocument().getBody();
// Build a table from the array.
body.appendTable(cells);
}
这是在SE上找到的最接近的问题,但没有回答以下查询:Copying Sheet Data to Doc table。
答案 0 :(得分:3)
我可以像上面那样理解。如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。
此示例脚本的流程如下。
从问题脚本中,该脚本是Google文档的容器绑定脚本。因此,为了测试脚本,请将以下脚本放入您共享的Google文档的容器绑定脚本中。
function myFunction() {
// Get Google Sheet data
var ss = SpreadsheetApp.openById("###"); // Please set the Spreadsheet ID.
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange(4, 2, 1, 5).getDataRegion(SpreadsheetApp.Dimension.ROWS);
var values = range.getValues();
var backgroundColors = range.getBackgrounds();
var styles = range.getTextStyles();
// Position to paste data in Google Docs
var body = DocumentApp.getActiveDocument().getBody();
var table = body.appendTable(values);
table.setBorderWidth(0);
for (var i = 0; i < table.getNumRows(); i++) {
for (var j = 0; j < table.getRow(i).getNumCells(); j++) {
var obj = {};
obj[DocumentApp.Attribute.BACKGROUND_COLOR] = backgroundColors[i][j];
obj[DocumentApp.Attribute.FONT_SIZE] = styles[i][j].getFontSize();
if (styles[i][j].isBold()) {
obj[DocumentApp.Attribute.BOLD] = true;
}
table.getRow(i).getCell(j).setAttributes(obj);
}
}
}
可以反映列宽。但是,当列宽设置为Google Document时,即使将单位从Spreadsheet转换为Document,结果也似乎与直接复制并粘贴表格的结果不同。
在共享电子表格中,列“ B”至“ F”的宽度分别为21、100、100、100和100像素。但是发现,当将表从电子表格手动复制到文档时,每列宽度都会从原始大小更改。这样,不幸的是,当脚本复制表的列宽时,无法复制手动复制的结果。
在以下示例脚本中,Google Spreadsheet的列宽被复制到Google Document的表中。
function myFunction() {
// Get Google Sheet data
var ss = SpreadsheetApp.openById("###"); // Please set the Spreadsheet ID.
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange(4, 2, 1, 5).getDataRegion(SpreadsheetApp.Dimension.ROWS);
var values = range.getValues();
var backgroundColors = range.getBackgrounds();
var styles = range.getTextStyles();
var colWidth = []; // Added
for (var col = 2; col <= 6; col++) { // Added
colWidth.push(sheet.getColumnWidth(col) * 3 / 4);
}
// Position to paste data in Google Docs
var body = DocumentApp.getActiveDocument().getBody();
var table = body.appendTable(values);
table.setBorderWidth(0);
colWidth.forEach(function(e, i) {table.setColumnWidth(i, e)}); // Added
for (var i = 0; i < table.getNumRows(); i++) {
for (var j = 0; j < table.getRow(i).getNumCells(); j++) {
var obj = {};
obj[DocumentApp.Attribute.BACKGROUND_COLOR] = backgroundColors[i][j];
obj[DocumentApp.Attribute.FONT_SIZE] = styles[i][j].getFontSize();
if (styles[i][j].isBold()) {
obj[DocumentApp.Attribute.BOLD] = true;
}
table.getRow(i).getCell(j).setAttributes(obj);
}
}
}
colWidth
的值。或者,请通过修改电子表格侧的列宽来调整文档侧的列宽。或者,请使用上面的脚本。这是由于我的技能差。我对此深表歉意。