【问题标题】:Google sheets scripts function UrlFetchApp.fetch does not run from .onEdit(e) but works from editorGoogle 表格脚本函数 UrlFetchApp.fetch 不能从 .onEdit(e) 运行,但可以从编辑器运行
【发布时间】:2019-05-10 08:13:13
【问题描述】:

我创建了一个包含大量沙滩排球杯信息的 Google 表格,并且我想在选中此表格中的复选框时调用我创建的 API。

function onEdit(e){
  const ui = SpreadsheetApp.getUi();
  const spreadsheets = SpreadsheetApp.getActive();
  const configSheet = spreadsheets.getSheetByName("Config")
  var tourneyId = String(configSheet.getRange(2,4).getValue())
  var tourneyTitle = String(configSheet.getRange(2,5).getValue())
  var sheet = spreadsheets.getActiveSheet()
  if (sheet.getName() == "LiveScore"){
    var actRng = sheet.getActiveRange();
    var editColumn = actRng.getColumn();
    var rowIndex = actRng.getRowIndex();
    actRng = actRng.getCell(1, 1);
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
    if(editColumn == 7 && rowIndex != 1){
      onStartBroadcastClicked(actRng, ui, sheet, rowIndex, editColumn, tourneyTitle);
    }
  }
}

在我看来,这部分从来没有任何问题。但是当我进入 onStartBroadcastClicked 函数时:

function onStartBroadcastClicked(actRng, ui, sheet, rowIndex, editColumn, tourneyTitle){
  var homeTeam = String(sheet.getRange(rowIndex, 14).getValue());
... // more setting variables
  var endTime = new Date(startTime.getTime() + MILLIS_PER_MATCH);

  if(actRng.isChecked()){
    var response = ui.alert("You are about to start a new broadcast. Are you sure?" +
                            "\n Title: " + title, ui.ButtonSet.YES_NO);
    if (response == ui.Button.YES) {
      var httpRequest = "https://someUrl";
      var options =
          {
            'method':'POST',
            'contentType': 'application/json',
            'payload' : JSON.stringify({
              "title" : title,
                  ... // setting all variables
              "description" : description
            }),
            'muteHttpExceptions' : true,
            'headers' : {
              "Authorization": "Basic " + Utilities.base64Encode(USERNAME + ":" + PASSWORD)
            }
          };

      ui.alert("Waiting.......")
      var result = UrlFetchApp.fetch(httpRequest, options);
      ui.alert(result.getContentText())

问题是它总是到达ui.alert("Waiting.......") 行,但是当从复选框触发时,它永远不会成功 http POST 请求。如果我在编辑器中单击播放,它会成功并且我在警报框中得到响应。

可能是一些超时或一些自动保存问题?有谁知道在哪里继续寻找?我已经被困在这里一段时间了,如果有人能指出正确的方向,我会非常高兴。

【问题讨论】:

  • 在简单触发器上使用需要授权的方法时,会发生错误。那么在这种情况下,如何使用 OnEdit 事件触发器的可安装触发器呢? developers.google.com/apps-script/guides/triggers/installable 届时请将onEdit() 重命名为其他函数名,并将重命名的函数名安装为OnEdit 事件触发器。这样,可以防止onEdit() 的重复运行。通过这些设置,当单元格被编辑时,UrlFetchApp.fetch() 工作。如果这不是您问题的直接解决方案,我深表歉意。
  • 我们需要授权才能调用外部资源是正确的。如果您想添加您的评论作为您的答案,我会将其标记为正确
  • 感谢您的回复。我很高兴你的问题得到了解决。我发布了它,其中包括一些信息作为答案。你能确认一下吗?

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


【解决方案1】:

您的问题的修改点是使用 OnEdit 事件的可安装触发器。当在简单触发器上使用需要授权的方法时,会发生错误。这种情况让我们认为脚本似乎不起作用。

为避免此错误,请使用 OnEdit 事件触发器的可安装触发器。

作为重要的一点,在安装触发器之前,请将onEdit()的函数名称重命名为其他名称。并将重命名的函数名称安装为 OnEdit 事件触发器。这样,可以防止onEdit() 的重复运行。如果将onEdit() 函数安装为可安装触发器,则在编辑单元格时,该函数将运行2 次。 Ref

通过以上设置,当单元格被编辑时,UrlFetchApp.fetch() 工作。

参考资料:

【讨论】:

  • 非常感谢!你拯救了我的一天!
猜你喜欢
  • 1970-01-01
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-29
相关资源
最近更新 更多