简体   繁体   中英

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

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM