【问题标题】:Trigger to get around "Exceeded maximum execution time" in google-apps-script触发以绕过 google-apps-script 中的“超过最大执行时间”
【发布时间】:2014-01-03 04:58:40
【问题描述】:

我正在尝试运行以下脚本来绕过 Google 脚本的最长执行时间。我让函数运行了 4 分钟,并设置了一个触发器,让它在 5 分钟内再次运行,从它停止处理的地方开始。第一次通过它运行平稳并设置触发器。当触发器第一次运行时,它的处理速度非常慢,然后不再运行。谁能告诉我我做错了什么以及是否有办法解决这个问题?

function updateAll() {

  var startTime= (new Date()).getTime();
  var startRow = ScriptProperties.getProperty("start_row");

  //Start on row 2 if null
  if (!startRow) {
    startRow = "2";
  }

  // This code deletes all the triggers
  var triggers = ScriptApp.getProjectTriggers();
  for(var i in triggers) {
    ScriptApp.deleteTrigger(triggers[i]);
  }

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  for (var i = startRow; i < 3000; i++) {
    var currTime = (new Date()).getTime();

    if(currTime - startTime >= 240000) {

      ScriptProperties.setProperty("start_row", i)
      //Set new trigger to run in 5 minutes
      ScriptApp.newTrigger("updateAll")
               .timeBased()
               .at(new Date(currTime+300000))
               .create();
      break;

    } else {

      //Do logic and set values in spreadsheet
      //Run sleep to avoid maximum script execution error
      Utilities.sleep(100);

    }

  };
}

【问题讨论】:

  • 顺便说一句,Date.now() 是你的(new Date()).getTime()

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


【解决方案1】:

你怎么知道它运行缓慢?您的电子表格是否正在使用新版本?好像还没有there's a lot of issues with it

无论如何,我不会每次都移除触发器并重新设置它。我只是将它设置为每 5 分钟运行一次,并在我检测到作业完全完成时将其删除。由于“最大执行时间错误”,也无需调用Utilities.sleep。它仅在您遇到“每秒调用次数”类型的错误时才有用。

当我完成这个答案时,我发现您的问题可能与您的 ScriptProperties 使用有关。它只能保存字符串,如果您向它传递其他内容,它只会尝试将其解析为字符串。因此,当您从 ScriptProperties 恢复您的 startRow 时,您应该将 parseInt 恢复为一个数字。

var startRow = parseInt(ScriptProperties.getProperty("start_row"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    相关资源
    最近更新 更多