google-apps-script - 如何增加 Google Apps 脚本中的 6 分钟执行限制?

标签 google-apps-script limit execution-time

有什么办法可以增加 Google Apps 脚本的 6 分钟执行时间限制?我认为答案可能是 G Business Suite 的抢先体验计划。如果我能加入 Early Access 计划,我可能愿意每月为 G Business Suite 支付 10 美元,以便将执行限制从 6 分钟增加到 30 分钟。但是 G Suite 帮助论坛的一位顾问说抢先体验是一个有限的计划,这意味着我不能保证我能得到它。

有没有其他方法可以增加 6 分钟的执行限制?

请注意,在这个问题中,我不是在询问如何优化我的脚本以适应 6 分钟限制的想法。 (我将来可能会问,如果当前问题的答案是“不,没有其他办法。”)

因此,对当前问题的适当答案是:

  • “不,没有其他方法可以增加 Google App Scripts 的 6 分钟执行限制。”
  • “是的,还有其他增加 6 分钟执行限制的方法,这些方法是...”
  • 最佳答案

    我有几十个作业需要 > 6 分钟才能运行。我使用了多年来一直在调整的“外环”脚本(如下)。无论需要多长时间,它都会运行每个作业直到完成。

    设置(只是一个指南 - 您需要将其应用于您自己的特定场景):

  • 换线 var thingies =到你想要处理的任何东西。理想情况下,它应该是一个数组。
  • //do our work here 后面添加您自己的代码线路
  • 在名为 outerLoop() 的函数上设置触发器每 x 小时/天运行一次。可以将其重命名为对您有意义的名称,例如 doProcessWidgets() .

  • 代码:
    //automatically invoked from outerLoop()'s creation of a new trigger if required to get work done
    function outerLoopRepeating() {
      outerLoop();
    }
    // trigger this function
    function outerLoop() {
      try {
        var processingMessage = 'Initialising', isOverMaxRuntime = false, startTime = new Date(), // calc elapsed time
            functionName = arguments.callee.name, repeatingFunctionName = functionName + 'Repeating'; //for logging, triggering
    
        // Deletes all occurrences of the Repeating trigger we don't end up with undeleted time based triggers all over the place
        //add library GASRetry MGJu3PS2ZYnANtJ9kyn2vnlLDhaBgl_dE
        GASRetry.call(function(){ScriptApp.getProjectTriggers().forEach(function(i) {
          if (i.getHandlerFunction() === repeatingFunctionName) {ScriptApp.deleteTrigger(i);}
        });});
    
        Logger.log('========== Starting the "%s" function ==========', functionName);
    
        // Handle max execution times in our outer loop
        // Get start index if we hit max execution time last run
        var start = parseInt(PropertiesService.getScriptProperties().getProperty(functionName + "-start")) || 0;
    
        var thingies = ['stuff to process', 'in an Array',,,,]; //
        for (var i = start ; i < thingies.length; i++) {
          if (Math.round((new Date() - startTime)/1000) > 300) { //360 seconds is Google Apps Script max run time
            //We've hit max runtime. 
            isOverMaxRuntime = true;
            break;
          }
          //do our work here
          Logger.log('Inside the for loop that does the xyz work. i is currently: %d', i);
          var processingMessage = Utilities.formatString('%d of %d thingies: %s <%s>',  i+1, thingies.length, thingyName, thingyId);
    
          //do our work above here
        }
        if (isOverMaxRuntime) {
          //save state in user/project prop if required
          PropertiesService.getScriptProperties().setProperty(functionName + '-start', i);
          //create another trigger
          GASRetry.call(function(){ScriptApp.newTrigger(repeatingFunctionName).timeBased().everyMinutes(10).create();});
          Logger.log('Hit max run time - last iteration completed was i=%s', i-1);
        } else {
          Logger.log('Done all the work and all iterations');
          PropertiesService.getScriptProperties().deleteProperty(functionName + '-start');
          Logger.log('Completed processing all %s things with the "%s" function', thingies.length, functionName);
        }
      } catch (e) {
        Logger.log('%s. While processing %s', JSON.stringify(e, null, 2), processingMessage);
        throw e;
      }
    }
    

    关于google-apps-script - 如何增加 Google Apps 脚本中的 6 分钟执行限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41971806/

    相关文章:

    java - 如何用Java测量执行时间

    php - 如何提高PHPFox开发的 'Facebook'这样的社交网站的执行速度并减少执行时间?

    javascript - 谷歌电子表格循环器

    mysql - 在 MySQL 的 LIMIT 子句中使用变量

    facebook - 使用字段扩展计数的 Facebook Graph API 调用是否与批处理调用不同

    php - 如何在时间限制内禁用上传

    java - 用于计算未正确输出的算法的比较和执行时间的代码

    javascript - JavaScript HTML 谷歌图表

    javascript - 使用谷歌工作表脚本移动过滤值(复制和粘贴值),同时忽略列标题/标题

    javascript - 日期时间错误 21-未定义-2014