Exact Time Trigger - Episode 3.1.1 | Apps Script ~ Script Service
Вставка
- Опубліковано 8 лют 2025
- Hey Coders! The time-based triggers we learned in the last episode will allow us to run our functions within a ±15 minute interval. If we need to specify our trigger to run at an exact minute, we can use the at() method. However, what if you run into the case when you need to execute a certain function every day at a specific time, and it can't be 15 minutes early/late? This video will showcase a technique of setting up 2 triggers to get the job done.
Check out the source code on GitHub:
github.com/dav...
Apps Script Documentation: developers.goo...
----------------------------------------------------------------------------------------------
Other playlists:
Utilities Service:
• Apps Script - Season 1...
Properties Service:
• Apps Script - Season 1...
URL Fetch Service:
• Apps Script - Season 1...
Drive Service:
• Apps Script - Season 1...
Forms Service:
• Apps Script - Season 9...
Lock Service:
• Apps Script - Season 8...
HTML Service:
• Apps Script - Season 7...
Document Service:
• Apps Script - Season 6...
Slides Service:
• Apps Script - Season 5...
Calendar Service:
• Apps Script - Season 4...
Script Service:
• Apps Script - Season 3...
Gmail Service:
• Apps Script - Season 2...
Spreadsheet Service:
• Apps Script - Season 1...
Apps Script Orientation:
• Apps Script - Season 0...
----------------------------------------------------------------------------------------------
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?
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 🤔
How to setup Failure notification settings? Thanks sir
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).