user3392296
user3392296

Reputation: 644

Google Sheet Script time based Trigger is not working

I would like to execute a function time based. Always Monday to Friday, hourly, from 07:05 AM to 18:05 PM.

for this I have added the following code to the function, but unfortunately it does not trigger.

function createTrigger(){

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

[[7,05],[8,05],[9,05],[10,05],[11,05],[12,05],[13,05],[14,05],[15,05],[16,05],[17,05],[18,05]].forEach(createTrigger);

}

function myFunction(){

// code I want to execute

}

Upvotes: 0

Views: 101

Answers (1)

Iamblichus
Iamblichus

Reputation: 19309

You should provide only one weekday when using onWeekDay(day):

.onWeekDay(ScriptApp.WeekDay.MONDAY)

Otherwise, an exception is thrown. If you want to run this on different week days you should create multiple triggers.

Apart from this, the triggers should be created when you execute the function createTrigger.

Note:

You'll most likely reach the limit of triggers available per project. To avoid this, I'd suggest having a trigger that calls this every day of the week, and/or every hour of the day, and make the triggered function check the day/time before doing the desired actions, instead of installing so many triggers.

Upvotes: 1

Related Questions