Scripts, scripts, scripts!

Here is an example I use for creating time-based triggers. It accepts 3 parameters:

  1. func - the name of the function to be executed
  2. unit - either ‘hours’ or ‘minutes’
  3. 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;
  }
}
5 Likes