Search code examples
javascriptgoogle-apps-scripttriggersgoogle-apps

Run a Gmail Google Apps Script daily at 8:00, 12:30, 17:00


I need to run a Google Apps script three times a day: at 8:00, 12:30, 17:00.

How to do this?

I have already looked at Triggers, and more specifically Time driven:

  • Hour timer, but Every hour, Every 2 hours, Every 4 hours are not adapted here

  • Day timer, but then 8am to 9am is not very precise, I would prefer something more precise, and also 12:30 is not possible

  • Specific time, but then YYYY-MM-DD HH:MM is not adapted to run it daily

From calendar triggers does not seem adapted either.


Solution

  • Use nearMinute() and atHour():

    const createTrigger = ([hour, minute])=>
      ScriptApp.newTrigger("myFunction")
      .timeBased()
      .atHour(hour)
      .nearMinute(minute)  
      .everyDays(1) 
      .create();
    
    [[8,0],[12,30],[17,0]].forEach(createTrigger)