【问题标题】:FormApp.openById() - No item with the given ID could be foundFormApp.openById() - 找不到具有给定 ID 的项目
【发布时间】:2016-01-26 00:24:09
【问题描述】:

我有一个 Google 表格触发器功能,它在表单提交时根据为我的一个问题选择的值将提交内容放在表格中。我正在尝试使用“作为附加组件测试”功能测试我的函数,但是当我提交表单时,我在我的 onSubmit 函数中收到一个错误,其中包含来自触发器 No item with the given ID could be found, or you do not have permission to access it. (line 9, file "Code") 的错误消息 formSubmit

这是错误所在的行,

  var form = FormApp.openById(ss.getFormUrl());

这让我觉得存在身份验证问题,但我不确定如何进一步调试

完整代码:

function onSubmit(e) {
  //Open Marketing - Discoveries and Changes - v1
  var sheet = e.range.getSheet();

  //Return Spreadsheet that contains this sheet
  var ss = sheet.getParent();

  //Open Marketing - Discoveries and Changes - v1
  var form = FormApp.openById(ss.getFormUrl());

  //Destination sheet based on "Location of Change"
  var destSheet = getSheet(ss, e.namedValue['Location of Change']);

  //Store response in destination sheet
  destSheet.appendRow(e.values);

  function getSheet( spreadsheet, sheetName, headings) {
    spreadsheet = SpreadsheetApp.getActive();
    var sheet = spreadsheet.getSheetByName(sheetName);
    if (sheet == null) {
      sheet = spreadsheet.insertSheet(sheetName);
      if (headings && headings.constructor === Array) {
        sheet.getRange(1,1,1, headings.length).setValues([headings]);
      }
    }
    return sheet;
  }

}

【问题讨论】:

    标签: javascript google-apps-script google-sheets


    【解决方案1】:

    openById()ss.getFormUrl() 的组合不正确。在Adding Tabs to Google Sheets Based on Form Response 的答案中,使用了openByUrl()

    var form = FormApp.openByUrl(ss.getFormUrl());
    

    如果您想使用openById(),您需要从 URL 中提取 ID。 (见Easiest way to get file ID from URL on Google Apps Script。)

    【讨论】:

    • 伟大的收获!我进行了更改并且不再出现错误,但似乎 namedValue 未定义。这是否表明表单或工作表信息存在问题? TypeError: Cannot read property "Location of Change" from undefined. (line 16, file "Code")
    • 会不会是我的触发功能不适用于Test as an add-on 功能?我知道它在脚本编辑器中不起作用,但不认为在使用 Test as an add-on 时这应该是一个问题
    • 如何安装触发器? (必须以编程方式。)
    • 我通过 UI 的“所有触发器”按钮添加了触发器。这在测试中不起作用?
    • 不-阅读Testing Details测试时当前不支持可安装触发器。依赖于可安装触发器的功能将无法测试。 您仍然可以在绑定脚本中测试您的触发器。
    猜你喜欢
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 2015-10-05
    • 2021-04-12
    相关资源
    最近更新 更多