异常:无法从此上下文调用 FormApp.getUi()

Exception: Cannot call FormApp.getUi() from this context

我是 google 脚本的新手,我一直收到错误 Exception: Cannot call FormApp.getUi() from this context。我尝试在编辑器和预览模式下打开表单,并已将代码转移到 onOpen() 函数。这是给出错误的代码:

function onOpen(e){
  FormApp.getUi().createAddonMenu().addItem('Settings','show').addToUi()
  ScriptApp.newTrigger('submit').forForm(FormApp.getActiveForm().getId()).onFormSubmit().create()
}
function show(){
  ...
}
function submit(){
  ...
}

要使其再次运行,我所要做的就是删除代码,运行 项目,然后将其重新添加。

永远不要在 onOpen 方法中创建触发器,因为它是一个简单的触发器,对于未验证脚本的用户来说将会失败。

如果脚本经过身份验证,它会在每次在 Google 表单编辑器中打开表单时尝试创建触发器,这可能会导致您的脚本失败。