0

I am trying to create calendar events from google sheets where start and end time are given in two sells and the correct time zone is third.

As calendarID.createEvent(title, startDate, endDate) wants startDate and endDate to be Date() objects I cannot set the right Time zone for the events.

Start Time End Time Time Zone
01/01/2022 14:00 01/01/2022 15:00 Europe/Prague
01/01/2022 14:00 01/01/2022 15:00 Asia/Dubai

If I have these for example I want these events to be created in my calendar with the correct timezone so I can see them aligned to my calendar timezone which is for example Europe/Sofia.

I have tried :

var startDate = Utilities.formatDate(new Date(entry[0]), entry[2].valueOf(), 'MMMM dd, yyyy HH:mm Z'); var endDate = Utilities.formatDate(new Date(entry[1]), entry[2].valueOf(), 'MMMM dd, yyyy HH:mm Z');

but that creates strings and the createEvent() function gives error.

I also tried many different formats and conversions but in the end when when I try to make Date() variables they are always in -0500 timezone and so not correctly created in the calendar.

Any help will be much appreciated. Thank you.

  • https://stackoverflow.com/questions/53707902/working-with-different-timezones-in-javascript – Cooper Jan 26 '22 at 00:43

1 Answers1

0

When createEvent method is used, the timezone is your timezone. When you want to create the event by changing the timezone, how about using Calendar API? When the Calendar API is used, the timezone can be directly used. When this is reflected in a sample script, it becomes as follows.

Sample script:

In this case, the date is converted to the RFC3339 format. Before you use this script, please enable Calendar API at Advanced Google services.

var calendarId = "###"; // Please set your calendar ID.
var eventTitle = "###"; // Please set your event title.

var startDate = Utilities.formatDate(new Date(entry[0]), entry[2].valueOf(), "yyyy-MM-dd'T'HH:mm:ss'Z'");
var endDate = Utilities.formatDate(new Date(entry[1]), entry[2].valueOf(), "yyyy-MM-dd'T'HH:mm:ss'Z'");
Calendar.Events.insert({ end: { dateTime: endDate, timeZone: entry[2] }, start: { dateTime: startDate, timeZone: entry[2] }, summary: eventTitle }, calendarId);
  • When this script is run using your sample sheet, the timezones of created 2 events become Europe/Prague and Asia/Dubai.

Note:

  • You can also use the following sample script. But, in this case, the start time and end time use your timezone.

      var eventTitle = "###"; // Please set your event title.
      var startDate = Utilities.formatDate(new Date(entry[0]), entry[2].valueOf(), "yyyy-MM-dd'T'HH:mm:ss'Z'");
      var endDate = Utilities.formatDate(new Date(entry[1]), entry[2].valueOf(), "yyyy-MM-dd'T'HH:mm:ss'Z'");
      calendar.createEvent(eventTitle, new Date(startDate), new Date(endDate));
    

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Тhe simple script you noted is exactly what I did so far, but I got this error. `Exception: The parameters (String,String,String) don't match the method signature for CalendarApp.Calendar.createEvent.` The date is somehow converted to string and not suitable to the `createEvent` function. I haven't use Calendar API at Advanced Google services. I will try later and share the result. – Ivan Ivanov Jan 26 '22 at 07:09
  • @Ivan Ivanov Thank you for replying. I apologize for the inconvenience. When you had tested my bottom sample script, unfortunately, I cannot replicate your error of `The parameters (String,String,String) don't match the method signature for CalendarApp.Calendar.createEvent.`. Because the arguments of `createEvent` in my proposed script uses are `eventTitle, new Date(startDate), new Date(endDate)` which are a string, a date object, a date object. And, when I tested my proposed script, no error occurs. – Tanaike Jan 26 '22 at 07:22
  • @Ivan Ivanov So, unfortunately, I cannot replicate `The parameters (String,String,String)`. This is due to my poor skill. I deeply apologize for this. But I would like to support you. So can you provide the detailed flow for replicating your issue using my proposed script? By this, I would like to confirm it. If you can cooperate to resolve your current issue, I'm glad. Can you cooperate to do it? – Tanaike Jan 26 '22 at 07:22
  • Thank you for your support. I have tried `eventTitle, new Date(startDate), new Date(endDate)`, but the google converts the `startDate` and `endDate` to the default timezone -0500 and so the events are not correctly created. If you want to replicate the case just use the sample data in the table I posted in the question. The goal is to make these events in lets say your timezone. If the event I want to create is in Europe/Madrid timezone at 09:00 oclock I want to see it converted to my local timezone Europe/Sofia and see it in my calendar at 10:00 oclock. – Ivan Ivanov Jan 26 '22 at 13:47
  • @Ivan Ivanov Thank you for replying. I apologize for the inconvenience. From your replying, I understood that your initial error of `Exception: The parameters (String,String,String) don't match the method signature for CalendarApp.Calendar.createEvent.` was resolved. I'm glad for it. About your 2nd issue, it's yes. So I proposed to use Calendar API. At "Note:" section, I said `But, in this case, the start time and end time use your timezone.`. When you tested my upper script, what result did you obtain? – Tanaike Jan 27 '22 at 01:45
  • This is the error I get: `GoogleJsonResponseException: API call to calendar.events.insert failed with error: Not Found` – Ivan Ivanov Jan 28 '22 at 18:26
  • I've manage to overcome this error. The calendar ID was wrong. But the calendar creates the event in the default timezone no mather if a specific timezone is present or not. – Ivan Ivanov Jan 28 '22 at 19:34
  • @Ivan Ivanov Thank you for replying. I have to apologize for my poor skill. Unfortunately, when I tested my 1st script using Calendar API, the created event has a specific timezone. Unfortunately, I can not replicate your situation of `But the calendar creates the event in the default timezone no mather if a specific timezone is present or not.`. This is due to my poor skill. I deeply apologize for my poor skill again. – Tanaike Jan 29 '22 at 00:11
  • @Ivan Ivanov But I would like to support you. So can you provide the detailed flow for correctly replicating your current situation? By this, I would like to confirm it. If you can cooperate to resolve your current issue, I'm glad. Can you cooperate to do it? – Tanaike Jan 29 '22 at 00:12
  • Hi. I have managed to figure it out. So if the date passed to the `CalendarApp.Calendar.createEvent` containd a timezone, the provided time stamp as a parameter is ignored. In order to work the passed start and and dates must be without timezones. – Ivan Ivanov Feb 15 '22 at 07:45
  • @Ivan Ivanov Thank you for replying. I have to apologize for my poor English skill again. About `In order to work the passed start and and dates must be without timezones`, so, I had proposed to use Calendar API. When you tested my upper script, what result did you obtain? I deeply apologize that in order to achieve your goal, I had proposed to use Calendar API instead of Calendar service (CalendarApp). This is due to my poor skill. I deeply apologize for this. I would be grateful if you can forgive my poor English skill. – Tanaike Feb 15 '22 at 07:55