gpt4 book ai didi

google-apps-script - 使用 Google Apps 脚本从 Google 表格数据表复制到 Google 文档表

转载 作者:行者123 更新时间:2023-12-02 03:02:32 29 4
gpt4 key购买 nike

有没有办法在 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 .

最佳答案

  • 您想将数据范围从 Google 电子表格复制到 Google 文档作为表格。
    • 您想将表格附加到 Google 文档。
    • 在这种情况下,您不想将工作表底部的空行包含到值中。
    • 在您的情况下,您不需要将原始电子表格链接到文档表格。
  • 您想使用 Google Apps 脚本实现这一目标。

我可以像上面那样理解。如果我的理解是正确的,那么这个答案呢?请将此视为几个可能的答案之一。

流程:

此示例脚本的流程如下。

  1. 从 Google 电子表格中检索数据范围。
  2. 使用范围检索值、背景和文本样式。
  3. 使用值将新表附加到 Google 文档。
  4. 设置单元格格式。

示例脚本:

根据您问题中的脚本,它假设该脚本是 Google 文档的容器绑定(bind)脚本。因此,为了测试脚本,请将以下脚本放入您共享的 Google Document 的容器绑定(bind)脚本中。

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);
}
}
}

备注:

  • 在此示例脚本中,单元格“B4:F”的值是从您共享的电子表格中检索的。所以如果你想改变这个范围,请修改上面的脚本。

引用资料:

添加:

将列宽从电子表格反射(reflect)到文档的问题:

可以反射(reflect)列宽。但是当列宽设置为Google Document时,即使单位从Spreadsheet转换为Document,结果似乎与直接复制粘贴表格的结果不同。

在您共享的电子表格中,“B”至“F”列的宽度分别为 21、100、100、100 和 100 像素。但是发现手动将表格从Spreadsheet复制到Document时,每一列的宽度都从原来的大小改变了。不幸的是,通过脚本复制表的列宽时,无法复制手动复制的结果。

示例脚本:

在下面的示例脚本中,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();

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的值。或者请通过修改电子表格端的列宽来调整文档端的列宽。或者请使用上面的脚本。这是我手艺不好的缘故。对此我深表歉意。

关于google-apps-script - 使用 Google Apps 脚本从 Google 表格数据表复制到 Google 文档表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59709400/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com