我正在尝试使用谷歌工作表中的所有数据填充谷歌文档。我要做的是根据工作表中的数据生成报告。
目前我已经找到了在谷歌文档中填充数据的方法,但它将为每一行数据生成一个新的谷歌文档。
这是我找到的代码
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
答案 0 :(得分:0)
由于您调用了 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();
}
如果这不是您的意思,那么也许您可以共享包含示例数据的示例电子表格,以及您想要的结果。