Issue in creating calendar events in user timezone using google apps script?

  Kiến thức lập trình

I have a script in a google sheet that creates events in a calendar. The Google sheet time zone is GMT-5 while in the script editor timezone is GMT-4, now the issue is my timezone is GMT+5 when I run this event creation script it does not create an event based on my timezone rather it does it according to google sheet/script timezone.

I made a copy of this Google sheet hoping that the new copy would have my timezone but to no effect. I tried everything but was not able to get my timezone. Here is the sample script:

 function run() {

  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var customizeSheet =spreadsheet.getSheetByName("✨ CUSTOMIZE PACE");
  
  var calendarData = customizeSheet.getRange("A2:I2").getDisplayValues()[0];
  var calendar = CalendarApp.getCalendarById("calenderID");

  var eventDate = new Date(calendarData[1]);
  createEventP(calendarData[0],eventDate, calendarData[2], calendarData[4], calendarData[6],calendar)
}

function createEventP(eventName, eventDate, startTime, endTime, eventDescription, calendar) {
 
  var startDate = Utilities.formatDate(eventDate, Session.getScriptTimeZone(), 'yyyy-MM-dd') + ' ' + startTime;
  var endDate = Utilities.formatDate(eventDate, Session.getScriptTimeZone(), 'yyyy-MM-dd') + ' ' + endTime;
  var options = {
    description: eventDescription
  };
  calendar.createEvent(eventName, startDate, endDate, options);
}

Any help to resolve this issue would be much appreciated.

LEAVE A COMMENT