Time-driven triggers are how most overnight syncs stay hands-off. This helper creates a once-per-day trigger for a named function around the hour you pick (project timezone).
What you'll need
- A top-level function name as a string (the handler must exist in the project)
- Rough hour of day (
0–23) in the script's timezone
How to use this snippet
function createDailyTrigger(handlerName, hour) {
hour = (hour == null) ? 6 : hour;
ScriptApp.newTrigger(handlerName)
.timeBased()
.everyDays(1)
.atHour(hour)
.create();
}
function installTriggers() {
// Avoid duplicates — delete existing handlers first (see deleteTriggersByHandler)
createDailyTrigger('runNightlySync', 2);
}
function runNightlySync() {
// your sync logic
}
Tips:
- Calling this repeatedly stacks triggers — pair with
deleteTriggersByHandlerbefore install. atHouris approximate; Apps Script runs in a window around that hour.
Happy Coding!
