Home > Net >  Time Trigger in Google Apps Script on date and time
Time Trigger in Google Apps Script on date and time

Time:01-22

In Google apps script it is possible to create timetrigger on a specific date and time. Excactly on that moment the trigger fires. Great!

The question is what is the code in apps script of this trigger?

Following code will fire somewhere between 9:00 and 10

 ScriptApp.newTrigger("function")
.timeBased()
.onWeekDay(ScriptApp.WeekDay.MONDAY)
.atHour(9)
.create();

Following code fires somewhere between 9:00 and 10 too.

 ScriptApp.newTrigger("function")
.timeBased()
.onWeekDay(ScriptApp.WeekDay.MONDAY)
.atHour(9)
.nearMinute(12)
.create();

.atMinute does not exist in apps script.

So who knows the code of the manual trigger on specific date and time?

CodePudding user response:

Since you want it to be on a specific date and time, you need to use at instead.

Script:

function myFunction() {
  Logger.log(new Date());
}

function trigger() {
  ScriptApp.newTrigger("myFunction")
  .timeBased()
  // month starts at 0 (e.g. Jan - 0, Feb - 1, etc)
  .at(new Date(2022, 1, 19, 0, 15))
  .create();
}

Trigger created:

trigger

  •  Tags:  
  • Related