简体   繁体   English

触发google-apps-script中的“超出最长执行时间”

[英]Trigger to get around “Exceeded maximum execution time” in google-apps-script

I am trying to run the following script to get around Google script's maximum execution time. 我正在尝试运行以下脚本来绕过Google脚本的最长执行时间。 I have the function run for 4 minutes and set a trigger for it to run again in 5 minutes starting where it left off in processing. 我让该功能运行了4分钟并设置了一个触发器,使其在5分钟内再次运行,从处理中断。 The first time through it runs smoothly and sets the trigger. 第一次通过它运行平稳并设置触发器。 When the trigger runs the first time, it processes very slow and then does not run again. 当触发器第一次运行时,它处理速度非常慢,然后不再运行。 Could anyone tell me what I'm doing wrong and if there is a way around this? 谁能告诉我我做错了什么,如果有办法解决这个问题?

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);

    }

  };
}

How do you know it's running slowly? 你怎么知道它运行缓慢? Is the spreadsheet your working on the new version? 电子表格是否适用于新版本? It seems there's a lot of issues with it yet. 它似乎还有很多问题

Anyway, I'd not remove the trigger and re-set it every time. 无论如何,我不会移除触发器并且每次都重新设置它。 I'd just set it to run every 5 minutes and remove it when I detect the job is fully finished. 我只是将其设置为每5分钟运行一次,并在检测到作业完成后将其删除。 Also there's no need to call Utilities.sleep because of the "maximum execution time error". 此外,由于“最大执行时间错误”,因此无需调用Utilities.sleep It's only useful if you're hitting a "calls per second" type of error. 它仅在您遇到“每秒呼叫”类型的错误时才有用。

As I was finishing this answer I saw that your problem might on your ScriptProperties usage. 当我完成这个答案时,我发现你的问题可能在你的ScriptProperties用法上。 It can only save strings, and if you pass something else to it, it will just try to parse it to string. 它只能保存字符串,如果你传递其他内容,它只会尝试将其解析为字符串。 So, when you get your startRow back from ScriptProperties you should parseInt it back to a number. 因此,当您从ScriptProperties返回startRow ,您应该将其parseInt为一个数字。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM