Programmatically creating triggers sometimes fails

I need to set many triggers in my google spreadsheet to update some sheets.

To avoid a painful task, I manually installed one trigger, which programmatically set other necessary functions in the task function (batch processing, etc.)

Well, here my function called for the creation of these triggers

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

I have to add triggers to the catch try block because of this:

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]

Where is he from? Why is this not happening every time? What could I do to avoid this?

Thank!

+3
source share
1 answer

, - ClockTriggerBuilder.nearMinute(). : http://code.google.com/p/google-apps-script-issues/issues/detail?id=1320. , .

nearMinute(), 8 52 .

0

All Articles