Here is an example I use for creating time-based triggers. It accepts 3 parameters:
-
func
- the name of the function to be executed -
unit
- either ‘hours’ or ‘minutes’ -
interval
- how often to run it (based on the unit)
So for example, calling it as create_time_based_trigger('clear_old_data', 'hours', 6)
would create a trigger that calls the function clear_old_data
once every 6 hours.
function create_time_based_trigger(func, unit, interval) {
var ss = SpreadsheetApp.getActive();
switch (unit) {
case 'minutes':
ScriptApp.newTrigger(func)
.timeBased()
.everyMinutes(interval)
.create();
break;
case 'hours':
ScriptApp.newTrigger(func)
.timeBased()
.everyHours(interval)
.create();
break;
}
}