gpt4 book ai didi

google-apps-script - 每 15 分钟在准确时间触发 GAS

转载 作者:行者123 更新时间:2023-12-01 23:17:36 31 4
gpt4 key购买 nike

我的目标是在 Google App Script 中创建一个触发时基,从特定分钟开始每 15 分钟运行一个函数,例如:11:01 - 11:16 - 11:31 - 11:46 --> 所以11:01 开始,然后每天每 15 分钟一类。

我知道这不是一个新问题,但我搜索了很多,只找到了这些答案:

It's possible run Google Sheets script Exact Time Everyday?

Run a Gmail Google Apps Script daily at 8:00, 12:30, 17:00

https://github.com/davidtheweiss/Apps-Script-Season-3-Script-Service/blob/master/Episode%201.1.gs

不幸的是,它们都不起作用,特别是我的尝试是:

function setTrigger() {

deleteTriggers();
var times = [[11,01],[11,16],[11,31],[11,46],[12,01],[12,16],[12,31],[12,46],[13,01],[13,16],
[13,31],[13,46],[14,01],[14,16],[14,31],[14,46],[15,01],[15,16],[15,31],[15,46],[16,01],[16,16],
[16,31],[16,46],[17,01],[17,16],[17,31],[17,46],[18,01],
[18,16],[18,31],[18,46],[19,01],[19,16],[19,31],[19,46],[20,01],[20,16],
[20,31],[20,46],[21,01][10,36],[10,46],[11,01],[11,16],[11,31],[11,46],[12,01]]; // 9:30 am, 9:45 am, 10:00 am 12:00pm
times.forEach(t_el => scheduledTrigger(t_el[0],t_el[1]));
}

function scheduledTrigger(hours,minutes){

var today_D = new Date();
var year = today_D.getFullYear();
var month = today_D.getMonth();
var day = today_D.getDate();

pars = [year,month,day,hours,minutes];

var scheduled_D = new Date(...pars);
var hours_remain=Math.abs(scheduled_D - today_D) / 36e5;
ScriptApp.newTrigger("function_Triggered")
.timeBased()
.after(hours_remain * 60 *60 * 1000)
.create()
}

function deleteTriggers() {

var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
if ( triggers[i].getHandlerFunction() == "function_Triggered") {
ScriptApp.deleteTrigger(triggers[i]);
}
}
}

function function_Triggered() {
// your function code here
}

但问题是它同时创建了太多的触发器,因此它给了我错误

最佳答案

如果您不关心触发精度(+- 1 分钟左右的所需时间):使用 everyMinutes(n)触发器和内部回调函数检查现在是哪一分钟:

  • 如果不是你需要的那一刻,跳过执行
  • 如果您需要准确的时间 - 做您的事

如果您确实关心精度(比如它应该是您需要的精确分钟,而不是上一个或下一个)在您的情况下 Apps 脚本根本没有帮助,您可能需要使用一些解决方法,例如:

  1. 设置一些简单的 Node.js App Engine cron 作业(精度很高)。
  2. 在 cron 函数内调用您的 Apps 脚本(部署为 Web App)。

这将使您能够在您需要的准确时间执行您的 Apps 脚本函数。

关于google-apps-script - 每 15 分钟在准确时间触发 GAS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68618928/

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