使用 Google 脚本和电子表格创建时间触发器

Create time trigger with Google Script and spreadsheet

我想使用 Google 脚本为我的 sheet 创建一个简单的时间触发器,我按照 docs 中的描述进行操作,但是当我调用函数时出现错误:

Google Apps: You do not have permission to call newTrigger

我尝试在互联网上寻找解决方案,但没有得到任何结果。有人可以帮我吗?谢谢!

P.S。我的代码:

 ScriptApp.newTrigger("consolius")
   .timeBased()
   .everyMinutes(1)
   .create();

function consolius() {
   SpreadsheetApp.getUi().alert('Hello, world!');
}

评论中提到的限制不适用于您的用例,它们适用于 add-ons only. The right info is here

但即使您实际上可以每分钟为 运行 函数创建一个计时器触发器,您也不能使用您尝试的方法,因为计时器触发器无法与 Ui 交互。

如果您查看错误通知,您会看到以下消息:

Details:
Start   Function            Error Message                                            Trigger    End
7/3/15 4:08 PM  consolius   Cannot call SpreadsheetApp.getUi() from this context. (line 26, file "clean sheet") time-based  7/3/15 4:08 PM

另一个兴趣点:您应该在函数中包含创建触发器的脚本,否则每次 运行 ANY 其他函数时都会创建一个新触发器在那个项目中,包括触发器调用的函数,这将很快变得非常混乱! (每次调用任何函数时都会执行 "global" 的所有内容)。

最后但同样重要的是:您不能在自定义函数中使用此类代码,它必须位于您通过菜单或按钮 运行 的脚本中。