How to get the client's time in a gmail add-on?


How to get the client's time in a gmail add-on?



I would like to know the timezone of the user who runs the gmail add-on.



Trying the following code did not comply to the client's time zone :




var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
Logger.log(formattedDate);



I basically want to filter my response and just show the upcoming events to the user.





An Apps Script project has it's own time zone. When you deploy an Apps Script project, it does not take on the time zone of the user. Whatever you set the time zone to in the project, that will be the time zone in the project. By default, an Apps Script project is set to your time zone. And it stays that way even if you deploy the script in an add-on. You can't get the users time zone from the script (server side) code. If you had access to their calendar, or spreadsheet, then you could get the time zone of either of those and it probably would be their time zone. Your only option may
– Sandy Good
Jun 29 at 13:20





be to get the timezone of the users browser from the user interface, and hope that the time zone in the users browser is their time zone. Which probably is the case. And you may need to give the user the option to set what their time zone is, just to make sure.
– Sandy Good
Jun 29 at 13:21





Oh. ok . i will try to access the calendar then.
– Hari Balaji
Jun 29 at 13:24





An event that is "upcoming" is in the future no matter what timezone it is in.
– tehhowch
Jun 29 at 19:11




1 Answer
1



There's the official documented way to retrieve timezone info in Gmail add-ons. Add the following scope to your manifest file:


"oauthScopes":["https://www.googleapis.com/auth/script.locale", ....]



Set the 'gmail.userLocaleFromApp' property to 'true':


"gmail": {"useLocaleFromApp": true}



Set the "onTriggerFunction" property to whatever function you are using to start up the add-on. The code below displays the card showing timezone details for the user running the add-on.


function loadAddon(e){

var accessToken = e.messageMetadata.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);

var userLocale = e.userLocale;
var offset = e.userTimezone.offSet;
var timezoneId = e.userTimezone.id;

var message ="User locale: " + userLocale +
"nUTC offset: " + offset +
" nTimezone Id: " + timezoneId;

var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader()
.setTitle("User timezone info"))
.addSection(CardService.newCardSection()
.setHeader(Session.getActiveUser().getEmail())
.addWidget(CardService.newTextParagraph()
.setText(message)))
.build();

return [card];

}



UPDATE
There seems to be the error in the documentation that some of the editors pointed out:



https://developers.google.com/gmail/add-ons/how-tos/access-user-locale



The UTC offset is supposed to be e.userTimezone.offset, but is actually e.userTimezone.offSet.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API