Great idea! I was looking for something like this to create a reminder bot, thanks a lot! The problem I already have is related to too many time-based triggers from this complement
Hi Henk-Jan Zweers! Great question! There's two ways of doing this. If you just want to create a trigger that fires a function once every 6 hours (plus or minus a few minutes) you can use the .everyHours() method, like this: ScriptApp.newTrigger('functionToRun') .timeBased() .everyHours(6) .create(); Or if you know the exact times every six hours you want to fire off your function, just like the tutorial above shows, I'd recommend just changing your mutableTrigger function to look something like this: function mutableTrigger() { const today = new Date(); const year = today.getFullYear(); const month = today.getMonth(); const day = today.getDate(); const functionName = 'sendEmail'; ScriptApp.getProjectTriggers().forEach(trigger => { trigger.getHandlerFunction() === functionName ? ScriptApp.deleteTrigger(trigger) : 0; }); ScriptApp.newTrigger(functionName) .timeBased() .at(new Date(year, month, day, 0, 0)) .create(); ScriptApp.newTrigger(functionName) .timeBased() .at(new Date(year, month, day, 6, 0)) .create(); ScriptApp.newTrigger(functionName) .timeBased() .at(new Date(year, month, day, 12, 0)) .create(); ScriptApp.newTrigger(functionName) .timeBased() .at(new Date(year, month, day, 18, 0)) .create(); } The mutableTrigger will create 4 triggers on the day which will fire every six hours at the exact time you specified. Does that help? If you still have questions, feel free to reply to this thread!
@@DavidWeissProgramming Hi David, thanks for that Idid it this way which is basically the same as you suggested :) function immutableTrigger() { ScriptApp.newTrigger('mutableTrigger') .timeBased() .everyDays(1) .create(); } function mutableTrigger() { const today = new Date(); const year = today.getFullYear(); const month = today.getMonth(); const day = today.getDate(); const functionName = 'scriptToRun'; ScriptApp.getProjectTriggers().forEach(trigger => { trigger.getHandlerFunction() === functionName ? ScriptApp.deleteTrigger(trigger) : 0; }); const createTrigger = ([year,month,day,hour,minute])=> ScriptApp.newTrigger(functionName) .timeBased() .at(new Date(year,month,day,hour,minute)) .create(); [[year,month,day,02,00],[year,month,day,08,00],[year,month,day,14,00],[year,month,day,20,00]].forEach(createTrigger) }
Hi David, very smart solution! However working with new Date causes that the trigger with the FunctionName fires 6 hours later. new Date is by default working in GMT-5 (Los Angelos). I am in GMT+1, so 6 hours time difference. The setting of the spreadsheet is GMT+1. Please advise.
Thanks for the great video David, I do however have a question. I don't quite understand how the immutable trigger works in conjunction to the mutable trigger since the first would run at a random ±15 minute time. If for example I wanted to run the function at exactly 12 midnight and if the immutable trigger ran at 12:10, wouldn't it trigger the mutable one with a 10 minute delay? I think I'm missing something 🤔
Hi, I try your code, and it works to ceate the triggers, but they dont execute/run the "send mail" function, after the specific time trigger it says This trigger has been disabled for an unknown reason. and dont do anything, can ypu help me?
Great idea! I was looking for something like this to create a reminder bot, thanks a lot! The problem I already have is related to too many time-based triggers from this complement
Hi Sir,
I want to run script every hour in office working hours only please help
Hi David, great video but how would you for example trigger the script every 6 hours instead of once a day?
Hi Henk-Jan Zweers! Great question! There's two ways of doing this. If you just want to create a trigger that fires a function once every 6 hours (plus or minus a few minutes) you can use the .everyHours() method, like this:
ScriptApp.newTrigger('functionToRun')
.timeBased()
.everyHours(6)
.create();
Or if you know the exact times every six hours you want to fire off your function, just like the tutorial above shows, I'd recommend just changing your mutableTrigger function to look something like this:
function mutableTrigger() {
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth();
const day = today.getDate();
const functionName = 'sendEmail';
ScriptApp.getProjectTriggers().forEach(trigger => {
trigger.getHandlerFunction() === functionName ? ScriptApp.deleteTrigger(trigger) : 0;
});
ScriptApp.newTrigger(functionName)
.timeBased()
.at(new Date(year, month, day, 0, 0))
.create();
ScriptApp.newTrigger(functionName)
.timeBased()
.at(new Date(year, month, day, 6, 0))
.create();
ScriptApp.newTrigger(functionName)
.timeBased()
.at(new Date(year, month, day, 12, 0))
.create();
ScriptApp.newTrigger(functionName)
.timeBased()
.at(new Date(year, month, day, 18, 0))
.create();
}
The mutableTrigger will create 4 triggers on the day which will fire every six hours at the exact time you specified.
Does that help? If you still have questions, feel free to reply to this thread!
@@DavidWeissProgramming Hi David, thanks for that Idid it this way which is basically the same as you suggested :)
function immutableTrigger() {
ScriptApp.newTrigger('mutableTrigger')
.timeBased()
.everyDays(1)
.create();
}
function mutableTrigger() {
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth();
const day = today.getDate();
const functionName = 'scriptToRun';
ScriptApp.getProjectTriggers().forEach(trigger => {
trigger.getHandlerFunction() === functionName ? ScriptApp.deleteTrigger(trigger) : 0;
});
const createTrigger = ([year,month,day,hour,minute])=>
ScriptApp.newTrigger(functionName)
.timeBased()
.at(new Date(year,month,day,hour,minute))
.create();
[[year,month,day,02,00],[year,month,day,08,00],[year,month,day,14,00],[year,month,day,20,00]].forEach(createTrigger)
}
Hi David, very smart solution! However working with new Date causes that the trigger with the FunctionName fires 6 hours later. new Date is by default working in GMT-5 (Los Angelos). I am in GMT+1, so 6 hours time difference. The setting of the spreadsheet is GMT+1. Please advise.
Hi David,
How can we delete the trigger after 5/required executions? Do you have any video/suggestion for this?
How to setup Failure notification settings? Thanks sir
Thanks for the great video David, I do however have a question. I don't quite understand how the immutable trigger works in conjunction to the mutable trigger since the first would run at a random ±15 minute time. If for example I wanted to run the function at exactly 12 midnight and if the immutable trigger ran at 12:10, wouldn't it trigger the mutable one with a 10 minute delay? I think I'm missing something 🤔
Hi, I try your code, and it works to ceate the triggers, but they dont execute/run the "send mail" function, after the specific time trigger it says
This trigger has been disabled for an unknown reason. and dont do anything, can ypu help me?
Hi Carlos, I think the date should +1 as it will send email in the next day, not today and it will generate a loop for that (send email everyday).