【问题标题】:Installable trigger onEdit in addon - Error - document not being used插件中的可安装触发器 onEdit - 错误 - 未使用文档
【发布时间】:2016-03-15 16:40:43
【问题描述】:

本文中包含的代码在插件中使用了可安装的“编辑时”触发器。当我在 Google Apps 脚本中将文档 ID 作为参数传递给 .forSpreadsheet(ssss) 时,控制台中会出现一条错误消息:

此插件正在尝试在当前未使用的文档上创建触发器。

插件似乎无法正确识别活动工作表。函数应该放在哪里? onInstall(), onOpen()?

function createSpreadsheetEditTrigger() {  
      var ssss = SpreadsheetApp.getActive().getId();

  ScriptApp.newTrigger('onEditTrigger')
    .forSpreadsheet(ssss)
    .onEdit()
    .create();
}

function onEditTrigger(e){
    isSheetName('SheetName') && sendingEmailFunction(e);  
}

【问题讨论】:

    标签: events google-apps-script triggers google-sheets protected


    【解决方案1】:

    不要使用 ID。

    目前:

    var ssss = SpreadsheetApp.getActive().getId();
    

    应该是:

    var ssss = SpreadsheetApp.getActive();
    

    .forSpreadsheet(ss) 的参数ss 必须是电子表格对象。您正在输入一个字符串。 getId() 返回一个字符串。始终查看返回的数据类型以及所需的数据类型。

    Google Documentation

    【讨论】:

    • 项目是绑定到电子表格还是独立脚本?如果它是一个独立的脚本,并且您从“运行”按钮运行代码,则没有活动的电子表格。您要么需要在测试模式下运行代码,要么从已发布的附加组件中运行代码。您可以将插件发布为“未列出”,而无需经过审批流程。
    【解决方案2】:

    来自https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_programmatically

    function createSpreadsheetEditTrigger() {
      var ss = SpreadsheetApp.getActive();
      ScriptApp.newTrigger('myFunction')
          .forSpreadsheet(ss)
          .onOpen()
          .create();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 2021-10-16
      相关资源
      最近更新 更多