【发布时间】:2019-05-29 15:17:51
【问题描述】:
我有 30 多个用户,每个用户都有自己的电子表格。每个用户的绑定脚本都引用了一个自定义库脚本,我用它来保持代码集中且易于更新。
我正在尝试使用应用程序脚本来创建在特定时间运行特定功能的触发器。但是,我不知道如何指定时钟触发器安装在哪个脚本文档上。
这是我首先尝试的:
createTimeTrigger("sendTimesheetsToHeads",1440);
function createTimeTrigger(functionToRun,minutesToWait) {
ScriptApp.newTrigger(functionToRun)
.timeBased()
.at(new Date(new Date().getTime()+minutesToWait*60*1000))
.create();
}
上述代码在库文件中,但由于用户正在运行代码,因此它将触发器安装在用户的容器绑定脚本文件上,而不是库文件上。当触发器运行时,它会失败,因为触发器使用了库中用户没有直接引用的函数。
我需要能够以编程方式在独立库脚本文件上安装触发器,就像我可以手动执行的操作一样。因此,在查看了应用程序脚本文档 (https://developers.google.com/apps-script/reference/script/trigger-builder#forDocument(String)) 后,我找到了触发器构建器的 forDocument() 函数并尝试了这个:
createTimeTrigger("sendTimesheetsToHeads",1440);
function createTimeTrigger(functionToRun,minutesToWait) {
ScriptApp.newTrigger(functionToRun)
.forDocument(<library script id here>)
.timeBased()
.at(new Date(new Date().getTime()+minutesToWait*60*1000))
.create();
}
但上面的代码会产生以下错误:“TypeError: Cannot find function timeBased in object DocumentTriggerBuilder.”
然后我尝试切换顺序:
function createTimeTrigger(functionToRun,minutesToWait) {
ScriptApp.newTrigger(functionToRun)
.timeBased()
.at(new Date(new Date().getTime()+minutesToWait*60*1000))
.forDocument(<library script id here>)
.create();
}
并收到此错误:“TypeError: Cannot find function forDocument in object ClockTriggerBuilder。”看起来 forDocument() 和 timeBased() 函数在触发器构建器中不兼容?
是否可以使用应用程序脚本执行我的要求?
【问题讨论】:
标签: google-apps-script document eventtrigger