[英]Run a Gmail Google Apps Script daily at 8:00, 12:30, 17:00
I need to run a Google Apps script three times a day: at 8:00, 12:30, 17:00.我需要每天运行 Google Apps 脚本三次:8:00、12:30、17:00。
How to do this?这该怎么做?
I have already looked at Triggers
, and more specifically Time driven
:我已经看过Triggers
,更具体地说是Time driven
:
Hour timer, but Every hour
, Every 2 hours
, Every 4 hours
are not adapted here小时计时器,但这里不适应Every hour
、 Every 2 hours
、 Every 4 hours
Day timer, but then 8am to 9am
is not very precise, I would prefer something more precise, and also 12:30 is not possible日间计时器,但是8am to 9am
不是很精确,我更喜欢更精确的时间,而且 12:30 也是不可能的
Specific time, but then YYYY-MM-DD HH:MM
is not adapted to run it daily特定时间,但随后YYYY-MM-DD HH:MM
不适合每天运行
From calendar
triggers does not seem adapted either. From calendar
触发器似乎也没有适应。
Use nearMinute() and atHour() :使用NearMinute() 和 atHour() :
const createTrigger = ([hour, minute])=>
ScriptApp.newTrigger("myFunction")
.timeBased()
.atHour(hour)
.nearMinute(minute)
.everyDays(1)
.create();
[[8,0],[12,30],[17,0]].forEach(createTrigger)
Although the TheMaster's answer is correct I would like to elaborate a little bit on an alternative approach which might be useful for future readers and different projects.尽管 TheMaster 的回答是正确的,但我想详细说明一种可能对未来读者和不同项目有用的替代方法。
Let's say you have a list of specific times (24H:mm) you want to schedule every day:假设您有一个想要每天安排的特定时间 (24H:mm) 的列表:
var times = [[9,30],[9,45],[10,00],[12,00]] // 9:30 am, 9:45 am, 10:00 am 12:00pm
which can be completely random , following any sequence you like.这可以是完全随机的,遵循您喜欢的任何顺序。
As you can see in the code below, you can run the setTrigger()
function , to generate a scheduled trigger of another function function_Triggered()
for each element in the aforementioned list.正如您在下面的代码中看到的,您可以运行setTrigger()
函数,为上述列表中的每个元素生成另一个函数function_Triggered()
的预定触发器。 The trigger will have the time provided in times
and the date of today .触发器将具有时间提供的times
和今天的日期。
If you want to perform this task everyday , then you just need to create a daily trigger for setTrigger()
and run it before the earliest time in times
.如果要执行此任务每天,那么你只需要创建一个每天触发setTrigger()
并在最早的时间之前运行times
。 In this case, you are generating triggers everyday, and therefore deleteTriggers()
is used to delete the previous (disabled) triggers for function_Triggered()
that are attached to your project.在这种情况下,您每天都在生成触发器,因此deleteTriggers()
用于删除附加到项目的function_Triggered()
的先前(禁用)触发器。
function setTrigger() {
deleteTriggers();
var times = [[9,30],[9,45],[10,00],[12,00]]; // 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
}
soMario and TheMaster have working solutions, but unfortunately for my use case I needed some tighter tolerances than the + or - 15 minutes!! soMario 和 TheMaster 有可行的解决方案,但不幸的是,对于我的用例,我需要比 + 或 - 15 分钟更严格的容差!! Which the google app script scheduler 'works' upon.谷歌应用程序脚本调度程序“工作”。
I used their very inaccurate scheduler to run the below function every 5 minutes.我使用他们非常不准确的调度程序每 5 分钟运行一次以下功能。 This function will check current time very accurately, it seems so far, and only run the next 'core' function within your desired hours and minutes.此功能将非常准确地检查当前时间,似乎到目前为止,并且仅在您想要的小时和分钟内运行下一个“核心”功能。 A simple if condition addition, using getDay() will add a day of week 'filter'.一个简单的 if 条件添加,使用 getDay() 将添加一周中的一天“过滤器”。
function timeCheck() {
var nowH=new Date().getHours();
var nowM=new Date().getMinutes();
Logger.log('Hour: '+nowH+' Minute: '+nowM)
if (nowH = 1 && nowM < 12) { getStatus() }
if (nowH = 10 && (nowM > 15 && nowM < 27)) { getStatus() }
return
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.