2

I have the following code to run the code every Monday at 9am. But I want to run the code every 30 minutes starting from 9 am until 12 pm

ScriptApp.newTrigger('myFunction').timeBased().everyWeeks(1).onWeekDay(ScriptApp.WeekDay.MONDAY).atHour(9).create()

Can someone help me how can I be doing this with trigger?

Marios
  • 26,333
  • 8
  • 32
  • 52

1 Answers1

3

Explanation:

You can use this answer to achieve your goal.

Create an array of the desired times and iteratively create a weekly trigger for each of the times for Monday.

Solution:

Execute once the createTrigger function to create the desired triggers for myFunction.

function createTrigger(){

const createTrigger = ([hour, minute])=>
  ScriptApp.newTrigger("myFunction")
  .timeBased()
  .atHour(hour)
  .nearMinute(minute)  
  .everyWeeks(1)
  .onWeekDay(ScriptApp.WeekDay.MONDAY)
  .create();

[[9,0],[9,30],[10,0],[10,30],[11,0],[11,30],[12,0]].forEach(createTrigger);

}

function myFunction(){

// code you want to execute

}
Marios
  • 26,333
  • 8
  • 32
  • 52