使用谷歌工作表中的所有数据填充单个谷歌文档 修改后的脚本参考

问题描述

我正在尝试使用谷歌工作表中的所有数据填充谷歌文档。我要做的是根据工作表中的数据生成报告。

  1. 我将使用谷歌表单提交每日报告。
  2. 报告将提交到 Google 表格中。
  3. 随后将自动填充工作表中记录的报告(只会填充一个 google 文档)。

目前我已经找到了在谷歌文档中填充数据的方法,但它将为每一行数据生成一个新的谷歌文档。

这是我找到的代码

function autofill(e) {
 var timestamp = e.values[0];
 var name = e.values[1];
 var report = e.values[2];

 var templateFile = DriveApp.getFileById("Google Doc ID");
 var templateResponseFolder = DriveApp.getFolderById("Folder for Google Doc");

 var copy = templateFile.makecopy(name + ',' + report,templateResponseFolder);

 var doc  = DocumentApp.openById(copy.getId());

 var body  = doc.getBody();

 body.replaceText("{{Timestamp}}",timestamp);
 body.replaceText("{{Name}}",name);
 body.replaceText("{{Report}}",report);

 doc.saveAndClose();
}
Record from Google Sheet

+-----------------+----------+----------+
|Timestamp        |Name      |Position  |
+-----------------+----------+----------+
|3/2/2021 14:12:13|John Doe 1|Position 1|
+-----------------+----------+----------+
|3/2/2021 14:15:13|John Doe 2|Position 2|
+-----------------+----------+----------+

This is the ouput of the above code.

+------------------------+
| John Doe 1             |
| Position 1             |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |            
|                        |
+------------------------+
John Doe 1,Position 1.docs

+------------------------+
| John Doe 2             |
| Position 2             |
|                        |
|                        |
|                        |
|                        |
|                        |            
|                        |
+------------------------+
John Doe 2,Position 2.docs

It will make a new Google Docs document every time a new data is recorded in Google Sheet.

This is what I am trying to achieve

+------------------------+
| John Doe 1             |
| Position 1             |
|                        |
|                        |
|                        |
|                        |
|                        |            
|                 Page 1 |
+------------------------+
| John Doe 2             |
| Position 2             |
|                        |
|                        |
|                        |
|                        |
|                        |            
|                 Page 2 |
+------------------------+
       Report.docs

I am trying to populate all the data recorded in Google Sheet in a single Google Docs which is Report.docs

解决方法

修改后的脚本

由于您调用了 var copy = templateFile.makeCopy(name + ',' + report,templateResponseFolder);,因此正在制作文件的副本。

通过此修改,templateFile 将每次更新。

function autofill(e) {
 var timestamp = e.values[0];
 var name = e.values[1];
 var report = e.values[2];
 
 var templateFile = DriveApp.getFileById("Google Doc ID");

 var body  = templateFile.getBody();

 body.replaceText("{{Timestamp}}",timestamp);
 body.replaceText("{{Name}}",name);
 body.replaceText("{{Report}}",report);

 doc.saveAndClose();
}

如果这不是您的意思,那么也许您可以共享包含示例数据的示例电子表格,以及您想要的结果。

参考