Home > other >  App Script Trigger setting different time in Google Triggers Dashboard
App Script Trigger setting different time in Google Triggers Dashboard

Time:01-14

I am having some difficulty in creating a trigger to execute each day at specifically 6:05am. (Australia/Melbourne time).

I have made sure to set the correct time-zone in spreadsheet setting, Project settings and even physically in my code.

The logs says it has the correct time, and it has all the necessary information to create the trigger for the current day at 6:05am

Log File:

12:33:55 PM Info Thu Jan 13 00:00:00 GMT 11:00 2022

12:33:55 PM Info [2022.0, 0.0, 13.0, 6.0, 5.0]

12:33:55 PM Info Thu Jan 13 06:05:00 GMT 11:00 2022

However when I go check the triggers dashboard (in Google App Script) it keeps creating the trigger for the current time:

Example the current time now is: 2022-01-13 12:34 <-- Which is the time the trigger was created for.

My Code:

function setTrigger() {
 deleteTriggers();  
  
var today_D = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "yyyy MM dd");
var newDate = new Date(today_D) 
Logger.log(newDate)
var year = newDate.getFullYear();
var month = newDate.getMonth();
var day = newDate.getDate();
pars = [year,month,day,06,05];
Logger.log(pars)

var scheduled_D = new Date(...pars);
Logger.log(scheduled_D)
ScriptApp.newTrigger("function_Triggered")
.timeBased()
.at(scheduled_D)
.create()
}

function deleteTriggers() {
  
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i  ) {
  if (   triggers[i].getHandlerFunction() == "function_Triggered") {
    ScriptApp.deleteTrigger(triggers[i]);
  }
}
}

function function_Triggered() {
 Gmail2GDriveVolume()
}

Any help would be greatly appreciated.

CodePudding user response:

From 12:33:55 PM Info Thu Jan 13 06:05:00 GMT 11:00 2022 and Example the current time now is: 2022-01-13 12:34 <-- Which is the time the trigger was created for., in this case, I thought that the time for setting as a time-driven trigger has already been over. I thought that this might be the reason for your issue.

So, for example, in order to check this, how about setting the time-driven trigger of tomorrow as follows?

From:

var day = newDate.getDate();

To:

var day = newDate.getDate()   1;

By this, the trigger will be installed at 06:05 tomorrow.

Reference:

  •  Tags:  
  • Related