Google script get out "wrong" timezone

989 views
Skip to first unread message

Dinolinho Knallgas

unread,
Apr 4, 2022, 4:46:29 AM4/4/22
to Google Apps Script Community
Hello together,

im living in Germany, but my Google-Script shows only the timezone for Nordamerican-Eastcoast. Is there maybe a way, to get the right timezone, in this case for Berlin?

A second problem is, that i can only see the start-times of a "meeting". Is it possible to get also the end-time here?

Thank you in advance!





Script:

Thank you in advance.

function doGet(e) {

  var calendars = CalendarApp.getAllCalendars();

  
  var calendars = CalendarApp.getAllCalendars();
  
  if (calendars == undefined) {
    Logger.log("No data");
    return ContentService.createTextOutput("no access to calendar hubba");
  }

  var calendars_selected = [];
  
  for (var ii = 0; ii < calendars.length; ii++) {
    if (calendars[ii].isSelected()) {
      calendars_selected.push(calendars[ii]);
      Logger.log(calendars[ii].getName());
    }
  }
  
  Logger.log("Old: " + calendars.length + " New: " + calendars_selected.length);

  const now = new Date();
  var start = new Date(); start.setHours(0, 0, 0);  // start at midnight
  const oneday = 24*3600000; // [msec]
  const stop = new Date(start.getTime() + 7 * oneday); //get appointments for the next 14 days
  
  var events = mergeCalendarEvents(calendars_selected, start, stop); //pull start/stop time
  
  
  var str = '';
  for (var ii = 0; ii < events.length; ii++) {

    var event=events[ii];    
    var myStatus = event.getMyStatus();
    
    
    // Define valid entryStatus to populate array
    switch(myStatus) {
      case CalendarApp.GuestStatus.OWNER:
      case CalendarApp.GuestStatus.YES:
      case CalendarApp.GuestStatus.NO:  
      case CalendarApp.GuestStatus.INVITED:
      case CalendarApp.GuestStatus.MAYBE:
      default:
        break;
    }
    
    // Show just every entry regardless of GuestStatus to also get events from shared calendars where you haven't set up the appointment on your own
    str += event.getStartTime() + ';' +
    event.getTitle() +';' + 
    event.isAllDayEvent() + ';';
  }
  
  return ContentService.createTextOutput(str);
}

function mergeCalendarEvents(calendars, startTime, endTime) {

  var params = { start:startTime, end:endTime, uniqueIds:[] };

  return calendars.map(toUniqueEvents_, params)
                  .reduce(toSingleArray_)
                  .sort(byStart_);
}

function toUniqueEvents_ (calendar) {
  return calendar.getEvents(this.start, this.end)
                 .filter(onlyUniqueEvents_, this.uniqueIds);
}

function onlyUniqueEvents_(event) {
  var eventId = event.getId();
  var uniqueEvent = this.indexOf(eventId) < 0;
  if(uniqueEvent) this.push(eventId);
  return uniqueEvent;
}

function toSingleArray_(a, b) { return a.concat(b) }

function byStart_(a, b) {
  return a.getStartTime().getTime() - b.getStartTime().getTime();
}

Clark Lind

unread,
Apr 4, 2022, 8:42:11 AM4/4/22
to Google Apps Script Community
Hello! 
For the first issue, I would probably use the built-in date formatter to define the time zone you want.
If you wrote the script in Germany, you can also substitute your script timezone in the above like:

var formattedDate = Utilities.formatDate(new Date(),  Session.getScriptTimeZone()  , "yyyy-MM-dd HH:mm'" );

For the second issue, you have defined the endTime or used it as a parameter, but I don't see you actually calling it anywhere in your code. You will have to explicitly call it:  "event.getEndTime()".

I hope that helps a little! 

Dinolinho Knallgas

unread,
Apr 4, 2022, 10:08:14 AM4/4/22
to Google Apps Script Community
So, im a completely Newbie at programming.

Do i have only to paste it in, in my programm-code?

Thank you!

Br,
Dino

Zack Reynolds

unread,
Apr 4, 2022, 11:45:44 AM4/4/22
to Google Apps Script Community
Here's a quick youtube tutorial that walks you through how to update the timezone for your script: https://www.youtube.com/watch?v=n2btT-LMAvY

Dinolinho Knallgas

unread,
Apr 4, 2022, 6:28:12 PM4/4/22
to Google Apps Script Community
Thank you! Ive tried it now. But the timezone is still wrong.

I have changed the timezone in my "appsscript.json" to Europe/Berlin, with the same result in the end.

sc00000.png

Reply all
Reply to author
Forward
0 new messages