Using Apps Script’s time-driven triggers programmatically (via ScriptApp.newTrigger().timeBased()...) provides significantly more control and flexibility than manually setting up triggers through the Apps Script editor's UI. While the editor offers only basic intervals like "every hour" or "every day," scripted time-based triggers let you:
Choose more precise times of day for execution (e.g., at 8:00 AM +/- 15 minutes)
Run at regular intervals (e.g., every 10 minutes, every 2 hours, every weekday, etc)
Dynamically create, remove, or update triggers as needed based on script logic
This makes scripted time-driven triggers ideal for production-level workflows where fine-grained scheduling or adaptability is required.
ATTENTION: Trigger builders can only create triggers in the apps script editor where the trigger builder function is run.
PREREQUISITES: The triggered function must be in the same apps script editor as the trigger builder function.
Time-driven Methods for the ClockTriggerBuilder Class
at(date) // On the morning of the given date, near midnight (+/- 15 minutes).
atDate(year, month, day) // On the morning of the given date, near midnight (+/- 15 minutes).
atHour(hour) // At the nth hour of the day.
everyDays(n) // Every n days.
everyHours(n) // Every n hours.
everyMinutes(n) // Every n minutes. n must be 1, 5, 10, 15 or 30.
everyWeeks(n) // Every n weeks.
nearMinute(minute) // Near the nth minute (+/- 15 minutes). If nearMinute() is not called, a random minute value is used.
onMonthDay(n) // On the nth day of the month. Not suitable for dates of 29, 30, or 31 of every month.
onWeekDay(day) // On the day name of the week.
Every 3 weeks, on Monday, at 16:15 (+/- 15 minutes)
ScriptApp.newTrigger("nWeeks").timeBased().everyWeeks(3).onWeekDay(ScriptApp.WeekDay.MONDAY).atHour(16).nearMinute(15).create();
On the 3rd day of the month, at 16:15 (+/- 15 minutes)
ScriptApp.newTrigger("nthOfMonth").timeBased().onMonthDay(3).atHour(16).nearMinute(15).create();
Every 3 days, at 16:15 (+/- 15 minutes)
ScriptApp.newTrigger("nDays").timeBased().everyDays(3).atHour(16).nearMinute(15).create();
On the morning of 2025-12-25 (Christmas day), near midnight (+/- 15 minutes)
const triggerDate = new Date(2025, 11, 25); // The Date constructor uses a zero-based index for the month (ie. 0-11).
ScriptApp.newTrigger('onDateNearMidnight').timeBased().at(triggerDate).create();
On the morning of 2025-12-25 (Christmas day), near midnight (+/- 15 minutes)
ScriptApp.newTrigger('onDateNearMidnight').timeBased().atDate(2025, 12, 25).create();
Open the Apps Script editor
Go to Triggers (The clock icon on the LEFT side of the Apps Script Editor)
Click on Add Trigger (Bottom Right)
Make selections to configure the trigger