Bob Smith
Bob Smith

Reputation: 262

Google Apps Script Trigger Timezone Will Not Change

I am trying to set up a trigger in a Google Apps Script project that runs once per day at the same time. To avoid issues with daylight savings, I set the timezone for the spreadsheet to be GMT-0 and I set it as GMT-0 under the project settings for the Apps Script project. Nevertheless, this is what I am presented with when I create a trigger:

Timezone that triggers will not change from (GMT-6)

GMT-6. This is my actual timezone. I saw other people were having issues with this and so I tried the solution from this answer. It did not work. This is the exact code I used:

function addTrigger() {
  // main() will be called weekly on Monday at 16:00 in the specified time zone
  var everyWeek = ScriptApp.newTrigger("setTrigger")
      .timeBased()
      .everyWeeks(1)
      .onWeekDay(ScriptApp.WeekDay.MONDAY)
      .inTimezone("Etc/UTC")
      .atHour(16)
      .create();
}

All this did was create a trigger at 16:00 in GMT-6. It completely ignored the .inTimezone("Etc/UTC").

I have tried using a VPN to spoof my location and see if that allows the timezone to change from GMT-6, it didn't. Is there some place in my Google account that this is pulling from and getting GMT-6? I checked in my Google Calandar and that had GMT-0 as my default, it made no difference.

When I get the current date in a script I get a date and time in GMT-0. So there is bascially a constant 6 hour offset between what I do in a script and when the triggers operate. It seems that everything follows GMT-0 except the triggers which are somehow holding onto GMT-6.

Please someone just tell me how I can change the timezone for triggers. I have wasted far too much time on such a stupid issue.

My appsscript.json file looks like the following:

{
  "timeZone": "Etc/UTC",
  "dependencies": {},
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}

Upvotes: 0

Views: 1092

Answers (3)

doubleunary
doubleunary

Reputation: 18784

If the script project is bound to a spreadsheet, it may be easiest to use the timezone of the spreadsheet, because it is easy to check and set in the spreadsheet's File > Settings. To use the spreadsheet's timezone, replace 'Etc/UTC' with SpreadsheetApp.getActive().getSpreadsheetTimeZone().

To make the code easier to read, set the timezone as the last thing before .create(), like this:

function addTrigger() {
  const timezone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
  const weeklyTrigger = ScriptApp.newTrigger('myFunctionToRunWeekly')
      .timeBased()
      .everyWeeks(1)
      .onWeekDay(ScriptApp.WeekDay.MONDAY)
      .atHour(16)
      .inTimezone(timezone)
      .create();
}

Upvotes: 0

TheMaster
TheMaster

Reputation: 50452

All this did was create a trigger at 16:00 in GMT-6. It completely ignored the .inTimezone("Etc/UTC").

I believe this is inaccurate. After testing, I found that a trigger was created at 16:00 in UTC. Even though the UI dashboard may still show GMT-6 the actual trigger is created in the specified timezone. I believe this is just a UI bug.

Upvotes: 1

Louis Teo
Louis Teo

Reputation: 24

It may be the way you are formatting your trigger?

This is something that I would do when creating one.

function setUpTrigger() {

  ScriptApp.newTrigger('setTrigger')
  .timeBased()
  .everyWeeks(1)
  .onWeekDay(ScriptApp.WeekDay.MONDAY)
  .atHour(16)
  .nearMinute(0)
  .inTimezone('Etc/UTC')
  .create();
}

function setTrigger() {
  //Your stuff that happens every monday at 1600
}

Also note that if you use atHour the time would be anytime from 1600-1700.

If you include something like nearMinute(0) there would be a 15 minutes +/- interval for the trigger to run as well.

Upvotes: 0

Related Questions