gpt4 book ai didi

google-apps-script - 过期的基于时间的触发器是自动删除的,还是应该手动删除?

转载 作者:行者123 更新时间:2023-12-02 17:48:55 24 4
gpt4 key购买 nike

我在 Google Apps 脚本中以编程方式设置触发器,以便在其他地方处理作业时将获取数据延迟几分钟。

我注意到在脚本编辑器的“查看当前脚本的触发器”中,旧的(过期的)触发器仍然存在 - 我怀疑它们也计入触发器的最大数量限制。

Google Apps 脚本会删除这些过期的触发器,还是我必须浏览触发器列表并删除过期的触发器?如果是,有人想分享一个代码示例吗? (这也应该包含在文档中,我建议)。

我使用 .at 创建触发器

   var d = new Date();
d.setMinutes(d.getMinutes() + 5);

try {
ScriptApp.newTrigger("retrieveOrder")
.timeBased()
.at(d)
.create();
} catch(e) {
sendErrorMail(e + " \n \n" + "Date: " + d);
return false;
}

最佳答案

如果你知道它从哪里被调用,那么你可以通过处理函数删除...调用函数“updateGmailPhotoFromDriveBatcher”然后调用自身

// this is called by a daily trigger. I don't want it deleted
function runPhotoUpdates() {
// NEEDS to be a separate fnc so it doesn't get deleted
updateGmailPhotoFromDriveBatcher()
}

// this function does a job, then reschedules itself until a blank is returned
function updateGmailPhotoFromDriveBatcher() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); //fetches the spreadsheet that the script is running in
var sheet = ss.getSheetByName("Groups");


var sgfrom = sheet.getRange(10, 21).getValue();
var npt = sheet.getRange(11, 21).getValue();

var oneJob = oneBatch(npt,sgfrom)
var currTime = (new Date()).getTime();
// WRITE THE new values; oneJob[0] is blank when completed; npt is a next page token of something that times out if you run the whole user base
sheet.getRange(10, 21).setValue(oneJob[1]);
sheet.getRange(11, 21).setValue(oneJob[0]);
sheet.getRange(12, 21).setValue(oneJob[2]);
sheet.getRange(13, 21).setValue(currTime)


SpreadsheetApp.flush();

if (oneJob[0] != "") {
// SCHEDULE NEW JOB

ScriptApp.newTrigger("updateGmailPhotoFromDriveBatcher")
.timeBased()
.at(new Date(currTime+(20*1000))) // 20 seconds from NOW
.create();
} else { // FINISHED
// KILL TRIGGERS
deleteTriggers()
}

}

function deleteTriggers() { // cleans up the triggers made above
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
var thf = triggers[i].getHandlerFunction() //String Returns the function that will be called when the trigger fires.
// if it's the one I kept making above
if (thf == "updateGmailPhotoFromDriveBatcher") {
ScriptApp.deleteTrigger(triggers[i]);
}

}

}

关于google-apps-script - 过期的基于时间的触发器是自动删除的,还是应该手动删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11031155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com