【问题标题】:How to copy certain range in gsheet and paste them in gdocs template using GAS?如何使用 GAS 复制 gsheet 中的特定范围并将它们粘贴到 gdocs 模板中?
【发布时间】:2021-08-01 07:29:02
【问题描述】:

我的 gsheet 有一些格式化的文本和一个表格。我需要复制整个内容并将其粘贴到我创建的空白内容的 gdoc 模板中,除了页眉(包含徽标、图像和超链接)和页脚(公司地址),然后将完成的 gdoc 转换为 pdf。

最初我提到How to embed a range from GSHEETS in GDOCS with google-apps-scripts?,但那个是嵌入链接和对象,从答案中我知道现在不可能,但我希望在保留所有格式和表格的同时粘贴复制的内容仍然是可能的.

我尝试了下面的代码。没有错误,但全部粘贴为纯文本只是因为我只知道 replaceText 命令。任何人都可以更正我的代码吗?

function sheetTodocTopdf() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var source = sheet.getSheetByName("Import");
 
  //file is the gdoc template file
  var file = DriveApp.getFileById('13mFHFOQw8JTXzXLlgmlIRFBQA2_LL9uEZSOS2BumqeQ'); 
  
  //duplicate the template file in the same folder
  var folder = DriveApp.getFolderById('1Z9UWlwSyeklKUHOlCevMyKq56K1QwApC')
  var copy = file.makeCopy("Report", folder); 
  
  //Open the duplicated gdoc
  var doc = DocumentApp.openById(copy.getId());
  
  var lastRow = source.getLastRow();
  
  var srcRange = source.getRange("A1:G57").getValues();
      
  //Replacing the text in the gdoc with the copied range of gsheet
  doc.replaceText('{content}', srcRange); 
  
  doc.saveAndClose();

  var doc = DriveApp.getFileById(copy.getId()).getAs("application/pdf");
  var pdf = doc.setName("Report.pdf");

  folder.createFile(pdf); 
};

谢谢。 DZ

【问题讨论】:

  • 如果您最后想要 PDF,我建议您在电子表格中完成所有操作。您可以尝试在工作表上添加带有徽标和超链接等的标题,并将工作表另存为 PDF。
  • 您可以将二维数组作为表格粘贴到 GDoc developers.google.com/apps-script/reference/document/table 这没什么大不了的。但是保持单元格的格式可能是一项艰巨的任务。这取决于您的桌子的外观。
  • @YuriKhristich,我以前做过,但它不适用于页脚。这就是为什么我开始想到这个替代方案。 GAS 没有参数、语法或命令来传递我们在生成 pdf 时在 gsheet 中设置的自定义页脚。但是 gdocs 一切都很好。
  • 当然,如果您的数据恰好具有相同的行数和列数,您可以使用虚拟表格制作文档模板并逐个单元格替换其数据,而不是替换整个表。这样单元格格式将保持不变。也许您可以在用新数据填充其单元格之前向虚拟表添加新行(并保留前行的格式)。
  • 我想创建隐形表,但我没有尝试过,因为这听起来很麻烦。那么,有了您所有的解决方法建议,这是否意味着简而言之,没有办法使用简单的 GAS 语法/命令/参数来做到这一点?

标签: google-apps-script google-sheets google-docs


【解决方案1】:

假设您在 Google Docs 中有这样的虚拟表(格式正确):

您可以通过这种方式从电子表格向表格中添加行:

function add_rows_to_existed_table() {

  const doc = DocumentApp.openById(doc_ID);
  const body = doc.getBody();
  const table = body.getTables()[0];
  const bottom_row = table.getRow(2); // get the bottom row
  table.removeRow(2);                 // remove the bottom row
  const row = table.getRow(1);        // get second row

  for (let i=0; i<5; i++) {           // append second row five times
    let new_row = row.copy();
    table.appendTableRow(new_row);
  }
  
  table.appendTableRow(bottom_row);   // restore the bottom row
}

之后,您可以使用电子表格表格中的文本更改表格中每个单元格的内容。

更新

所以更多或不太完整的实现将是这样的:

const doc_ID = "###";

function main() {
  const doc  = DocumentApp.openById(doc_ID);
  const data = SpreadsheetApp.getActiveSheet().getDataRange().getValues();

  add_rows_to_template_table(data, doc);
  replace_data_in_table(data, doc);
}

function add_rows_to_template_table(data, doc) {

  const body  = doc.getBody();
  const table = body.getTables()[0];
  const bottom_row = table.getRow(2);  // get the bottom row
  table.removeRow(2);                  // remove the bottom row
  const row = table.getRow(1);         // get second row

  for (let i=1; i<data.length; i++) {  // append the second row several times
    let new_row = row.copy();
    table.appendTableRow(new_row);
  }
  
  table.appendTableRow(bottom_row);    // restore the bottom row
 
}

function replace_data_in_table(data, doc) {

  const body  = doc.getBody();
  const table = body.getTables()[0];

  for (let row_index=0; row_index<data.length; row_index++) {
    let row = table.getRow(row_index+1); // skip a table header
    for (let cell_index=0; cell_index<data[0].length; cell_index++) {
      let cell = row.getCell(cell_index);
      // get attributes from a first character of a first paragraph in the cell
      let font_attributes = cell.getChild(0).getChild(0).asText().getAttributes();
      cell.setText(data[row_index][cell_index]);
      // apply the attributes to a first paragraph
      cell.getChild(0).asParagraph().setAttributes(font_attributes);
    }
  }

}

它从我的电子表格中获取数据:

获取我的 Doc 模板:

并用数据填充模板中的表格(保持格式化):

该解决方案有效,但我相信它可以改进。

【讨论】:

  • 对 gdocs 上的 GAS 还是很陌生。需要一些时间来试试这个。应尽快更新。
  • 我已经用更完整的脚本变体更新了我的答案。
  • 我这里的页眉和页脚实际上不适用于表格。页眉和页脚应该用于 gdocs,就像信笺抬头一样,无论我们在那个 A4 大小的页面中有多少内容,徽标总是会放在顶部,而地址或一些文本会放在底部。
  • 而来自 gsheet 的数据实际上来自 gform GUI。因此,数据输入结构处于水平模式,因为 1 个用户的输入 = 工作表中的 1 行值。而 gdocs 表格中的输出应为垂直模式。我使用 replaceText 将工作表中提取的值更改为文档。这就是为什么我需要在完成迭代后删除空白行。
  • @dell 您应该接受这个答案,因为它完美地解决了您的问题。有关实施的细节,即完全适合您的需要的格式应该已经在原始问题中得到澄清。如果您仍然在苦苦挣扎,我建议您接受这个答案,然后提出一个新问题。
猜你喜欢
  • 1970-01-01
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-21
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
相关资源
最近更新 更多