【发布时间】:2020-01-20 22:00:17
【问题描述】:
使用 Google Apps 脚本,是否可以在编辑 Google 表格文件时自动更新 Google 文档文件?
我有一个 Google DocumentApp 文件,其中包含一个从 Google SpreadsheetApp 文件中获取数据的脚本。我希望创建一个脚本,以便在编辑 SpreadsheetApp 文件时自动更新 DocumentApp 文件。
这是我目前正在使用的代码:
function updateDocumentOnEditTrigger() {
var ss = SpreadsheetApp.openById(SheetID);
ScriptApp.newTrigger('UpdateDocument')
.forSpreadsheet(ss)
.onEdit()
.create();
}
运行updateDocumentOnEditTrigger 函数似乎不会触发UpdateDocument 函数,它在手动运行时会正常工作。
【问题讨论】:
-
我想了解您的目标。但我无法理解
I want to trigger that DocumentApp script to run whenever the SpreadsheetApp is edited in any way.。我可以问你吗?顺便说一句,我认为openById不能与使用onEdit()的简单触发器一起使用。如果您想通过 OnEdit 事件触发器使用openById,请使用可安装的 OnEdit 事件触发器。这个怎么样? -
@Tanaike 我想为 DocumentApp 文件创建一个触发脚本,以便在编辑 SpreadsheetApp 文件时,更新 DocumentApp 文件。我已将脚本更改如下:
function updateDocumentOnEditTrigger() { var ss = SpreadsheetApp.openById(SheetID); ScriptApp.newTrigger('UpdateDocument') .forSpreadsheet(ss) .onEdit() .create(); }我将另一个函数包装在此函数中并将其放在外面,似乎都没有更新 DocumentApp 文件。 -
听起来您想在 SpreadsheetApp 上创建一个
onEdit触发器,然后更新 DocumentApp。因此,无论何时编辑 SpreadsheetApp,代码都会运行,然后编辑/更新 DocumentApp。对吗? -
@IMTheNachoMan 没错。我是谷歌脚本的新手。我已经按照我的“UpdateDocument”DocumentApp 脚本中的建议插入了上面安装的触发器。这是正确的还是我需要一个单独的 SpreadsheetApp 文件脚本?
标签: google-apps-script google-sheets google-docs