我必须在我的谷歌电子表格中设置很多触发器来更新一些工作表。
为了避免一项痛苦的任务,我手动设置了一个触发器,该触发器以编程方式设置所需的其他触发器,根据任务(批处理等......)
好吧,这是我调用来创建这些触发器的函数
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]
它从何而来 ??为什么不是每次都发生??我能做些什么来避免这种情况?
谢谢 !