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.
Let's say you have a list of specific times (24H:mm) you want to schedule every day:
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. The trigger will have the time provided in times
and the date of today.
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
. 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.
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
}