Search code examples
google-apps-scripttriggersgoogle-docsgoogle-docs-api

Error running trigger at specific time using GAS


I get the error message (below) when I run the code. My intention is to create a trigger at exact time 2017-04-03 20:10

ScriptApp.newTrigger('myfunction').timeBased().atDate(2017,04,03).atHour(20).nearMinute(10).create();

Log

[17-04-02 20:16:53:455 IST] TriggerBuilder.timeBased() [0 seconds]
[17-04-02 20:16:53:456 IST] ClockTriggerBuilder.atDate([2017, 4, 3]) [0 seconds]
[17-04-02 20:16:53:456 IST] ClockTriggerBuilder.atHour([20]) [0 seconds]
[17-04-02 20:16:53:461 IST] Execution failed: Error: Already chosen a specific date time with at() or atDate(). (line 109, file "Code") [11.583 seconds total runtime]

Solution

  • You can create a new date object with a specific date and time then use new trigger().At(date), not to be confused with atDate(), function to create trigger at that date and time.

    var dt = new Date(2017,03,03,20,10)
    //Month index starts from 0 i.e. Jan =0,Feb=1... So on
     ScriptApp.newTrigger('myFunction').timeBased().at(dt).create()
    

    Hope that helps!