超过Google Apps脚本的最长执行时间 [英] Exceeded maximum execution time in Google Apps Script

查看:170
本文介绍了超过Google Apps脚本的最长执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序脚本,经常得到这个错误。通过通知邮件判断,看起来时间限制是5分钟。有谁知道是否有办法延长这个时间限制?或者,有没有办法再次调用Apps脚本,并从停止的地方继续?任何帮助都是值得赞赏的。

解决方案

您可以做的一件事(这当然取决于您要完成的工作)是:


  1. 将必要的信息(例如循环计数器)存储在电子表格或其他永久性存储中(即ScriptProperties)。 b $ b
  2. 让脚本每隔五分钟左右终止一次。

  3. 设置时间驱动触发器,每5分钟运行一次脚本(或者使用脚本服务)。

  4. 每次运行时从永久存储区中读取保存的数据您已使用并继续从停止的位置运行该脚本。 这不是一个适合所有人的脚本解决方案,如果你发布你的代码,人们可以更好地帮助你。



    以下是我每天使用的脚本的简化代码片段:

      funct离子runMe(){
    var startTime =(new Date())。getTime();

    //在这里做一些工作

    var scriptProperties = PropertiesService.getScriptProperties();
    var startRow = scriptProperties.getProperty('start_row');
    for(var ii = startRow; ii <= size; ii ++){
    var currTime =(new Date())。getTime();
    if(currTime - startTime> = MAX_RUNNING_TIME){
    scriptProperties.setProperty(start_row,ii);
    ScriptApp.newTrigger(runMe)
    .timeBased()
    .at(new Date(currTime + REASONABLE_TIME_TO_WAIT))
    .create();
    休息;
    } else {
    doSomeWork();



    //在这里做更多的工作

    $ b $ / code $ REASONABLE_TIME_TO_WAIT
    应该足够大,以便触发新的触发器。 (我将它设置为5分钟,但我认为它可能会比这少)。



    注意#2: doSomeWork()必须是一个执行速度相对较快的函数(我会说小于1分钟)。



    注意#3:Google已弃用Script属性 ,并介绍了Properties Service 代替。该功能已被相应修改。


    I have an Apps Script that constantly gets this error. Judge by the notification email, it looks like the time limit is 5 minutes. Does anyone know if there is a way to extend this time limit? Or perhaps is there a way to call the Apps Script again and it pick up from where it left off? Any help is appreciated.

    解决方案

    One thing you could do (this of course depends on what you are trying to accomplish) is:

    1. Store the necessary information (i.e. like a loop counter) in a spreadsheet or another permanent store(i.e. ScriptProperties).
    2. Have your script terminate every five minutes or so.
    3. Set up a time driven trigger to run the script every five minutes(or create a trigger programmatically using the Script service).
    4. On each run read the saved data from the permanent store you've used and continue to run the script from where it left off.

    This is not a one-size-fit-all solution, if you post your code people would be able to better assist you.

    Here is a simplified code excerpt from a script that I use every day:

    function runMe() {
      var startTime= (new Date()).getTime();
    
      //do some work here
    
      var scriptProperties = PropertiesService.getScriptProperties();
      var startRow= scriptProperties.getProperty('start_row');
      for(var ii = startRow; ii <= size; ii++) {
        var currTime = (new Date()).getTime();
        if(currTime - startTime >= MAX_RUNNING_TIME) {
          scriptProperties.setProperty("start_row", ii);
          ScriptApp.newTrigger("runMe")
                   .timeBased()
                   .at(new Date(currTime+REASONABLE_TIME_TO_WAIT))
                   .create();
          break;
        } else {
          doSomeWork();
        }
      }
    
      //do some more work here
    
    }
    

    NOTE#1: The variable REASONABLE_TIME_TO_WAIT should be should be large enough for the new trigger to fire. (I set it to 5 minutes but I think it could be less than that).

    NOTE#2: doSomeWork() must be a function that executes relatively quick( I would say less than 1 minute ).

    NOTE#3 : Google has deprecated Script Properties, and introduced Properties Service in its stead. The function has been modified accordingly.

    这篇关于超过Google Apps脚本的最长执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆