有时以编程方式创建触发器失败

时间:2012-05-09 20:51:38

标签: google-apps-script

我必须在google电子表格中设置大量触发器来更新某些表格。

为了避免一个痛苦的任务,我手动设置了一个触发器,它根据任务(批处理等等)以编程方式设置所需的其他触发器。

好吧,这是我的函数调用来创建那些触发器

function createTriggers(sheet, hour){
  var nbTrigger = sheet.getLastColumn() * sheet.getLastRow() / 1100 + 1 ;
  var buffHour = new Date().getHours();
  var min = new Date().getMinutes()+10;
  if(min >=60){
    min = min % 60;
    buffHour++;
    if(buffHour>=24){
      buffHour = buffHour % 24;
    }
  }
  //Browser.msgBox(buffHour+"h"+min);
  var ok = false;
  var cpt = 0;
  do {
    try {
     ScriptApp.newTrigger("triggered").timeBased().atHour(new Date().getHours()).nearMinute(Math.abs(1)).everyDays(1).create();
      ok = true;
    } catch(e) {
      Logger.log(e);
      Utilities.sleep(1000);
      cpt++;
    } 
  }while(!ok && cpt<10);

  if(cpt>10){
    throw "Was unable to create a trigger...";
  }

  for(var i = 0; i<nbTrigger; i++){
    ok = false;
    cpt = 0;
    do {
      try {
        var trigger = ScriptApp.newTrigger("update").timeBased().atHour(buffHour).nearMinute(Math.abs(min)).everyDays(1).create();
        ok = true;
      } catch(e) {
        Logger.log(e);
        Utilities.sleep(1000);
        cpt++;
      }
    } while(!ok && cpt<10);

    if(cpt>10){
      throw "Was unable to create a trigger...";
    }

    Utilities.sleep(10000);
    min +=7;
    if(min >= 60){
      buffHour++;
      min = 0;
      if(buffHour == 24){
        buffHour = 0;
      }
    }
  }
}

我要将触发器的创建放入try catch块中,因为:

Wed May 09 13:36:41 PDT 2012 INFO: Exception: Invalid value -2 for field MINUTE, should be between [Range:0, 59]
Wed May 09 13:36:42 PDT 2012 INFO: Exception: Invalid value -8 for field MINUTE, should be between [Range:0, 59]
Wed May 09 13:36:43 PDT 2012 INFO: Exception: Invalid value -7 for field MINUTE, should be between [Range:0, 59]
Wed May 09 13:36:44 PDT 2012 INFO: Exception: Invalid value -7 for field MINUTE, should be between [Range:0, 59]

它来自哪里?为什么不是每次都发生?我该怎么做才能避免这种情况?

谢谢!

1 个答案:

答案 0 :(得分:0)

感谢您提出此问题 - 这是ClockTriggerBuilder.nearMinute()方法中的一个错误。我已经在问题跟踪器中为您提出了这个问题:http://code.google.com/p/google-apps-script-issues/issues/detail?id=1320。如果你加注星标,你将会更新它的进度。

在此期间作为解决方法,将分钟值传递给nearMinute()方法,该值介于8和52之间(包括8和52)。