基于LINK,我学习了如何在超过5分钟的最大运行时间时延长Google Apps脚本的脚本运行时间。
它可以工作,但是我不确定我是否以正确的方式做到了。我面临如下所述的一些问题。
从main函数中移出变量“ t”作为循环的起点,然后调用函数Foto_function。 如果时间超过了240秒的限制,则会触发。
if (isTimeUp(today)) {
// schedule a trigger for a different function
SpreadsheetApp.activeSheet.getRange("A1").setValue(t);
ScriptApp.newTrigger("Foto_function_Repeat")
.timeBased()
.everyMinutes(1)
.create();
break;
}
.....
function isTimeUp(today) {
var now = new Date();
return now.getTime() - today.getTime() > 240000;
// 30000 = 30 seconds; this is the threshold limit
// you are free to setup your own threshold limit
}
超过时间时,将启动Foto_function的克隆(称为“ Foto_function_Releat”)。变量“ t”将被移交给克隆函数。时间从头开始计算,如果已达到240秒,它将切换回原始的Foto_Function。
循环结束时,触发器将被删除。...
if (t == lastrow_Fotos + 1) {
stopp_triggers();
break;
}
.....
function stopp_triggers(){
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
sendmail();
}
}
但是:我收到大约8到12封内容完全相同的电子邮件,但我不知道为什么?我的代码有问题吗?
....这里是完整的代码....,并且会向我发送一封电子邮件,所以我知道脚本已完全完成。
function mainfunction(){
//do some stuff;
SpreadsheetApp.activeSheet.getRange("A1").setValue(2); // handover the variable "t" to the Foto_function
Foto_function();
}
function Foto_function(){
var today = new Date();
var Geodata = getSheetById(1702838837);
var lastrow_XX = Geodata.getRange(1, 1).getDataRegion(SpreadsheetApp.Dimension.ROWS).getLastRow(); //Last row in column A of sheet GEODATA
value_t = SpreadsheetApp.activeSheet.getRange("A1").getValue();
for (var t = value_t; t <= lastrow_XX + 2; t++){
if (t == lastrow_Fotos + 1) {
stopp_triggers();
break;
}
if (isTimeUp(today)) {
// schedule a trigger for a different function
SpreadsheetApp.activeSheet.getRange("A1").setValue(t);
ScriptApp.newTrigger("Foto_function_Repeat")
.timeBased()
.everyMinutes(1)
.create();
break;
}
else {
//do some stuff;
}
}
}
function Foto_function_Repeat(){
var today = new Date();
var Geodata = getSheetById(1702838837);
var lastrow_XX = Geodata.getRange(1, 1).getDataRegion(SpreadsheetApp.Dimension.ROWS).getLastRow(); //Last row in column A of sheet GEODATA
value_t = SpreadsheetApp.activeSheet.getRange("A1").getValue();
for (var t = value_t; t <= lastrow_XX + 2; t++){
if (t == lastrow_Fotos + 1) {
stopp_triggers();
break;
}
if (isTimeUp(today)) {
// schedule a trigger for a different function
SpreadsheetApp.activeSheet.getRange("A1").setValue(t);
ScriptApp.newTrigger("Foto_function")
.timeBased()
.everyMinutes(1)
.create();
break;
}
else {
do some stuff;
}
}
}
function stopp_triggers(){
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
sendmail();
}
}
function sendmail(){
var user = Session.getActiveUser().getEmail();
var mailname = user.split('@');
mailname = mailname[0];
MailApp.sendEmail({
to: user,
subject: 'GAS-Task is finished',
htmlBody: 'Hello, the task is finished!!!'
});
}
function isTimeUp(today) {
var now = new Date();
return now.getTime() - today.getTime() > 240000;
// 30000 = 30 seconds; this is the threshold limit
// you are free to setup your own threshold limit
}
答案 0 :(得分:1)
代码块
ScriptApp.newTrigger("Foto_function_Repeat")
.timeBased()
.everyMinutes(1)
.create();
可让您创建一个触发器,每分钟触发一次function Foto_function_Repeat()
。
因此,每分钟var t
将重新设置为= value_t
,并将在for
循环内递增,直到if (t == lastrow_Fotos + 1)
条件被满足为止。
function stopp_triggers()
将删除所有触发器,但不会杀死在第一次运行repeatFunction()
到{{1}之间的时间段内已经触发的repeatFunction()
执行。 }}已满。
t == lastrow_Fotos + 1
增加到例如.everyMinutes(1)
。此外,我建议您在代码的关键位置实施带有计数器变量的日志,以便您可以监视其执行并验证不同功能的调用的安全性是否符合预期。
Script properties对于在脚本运行之间存储值很有用。您可以存储和检索计数器变量,因此不必每次都从零开始循环。