0

[UPDATED]: To create a google calendar event using AppsScript we can use the simple method of createEvent(eventName, startTime, endTime). I have worked with this and did not have any issues. But the only date format I have used was the long date/time format in the google sheets (e.g. 3/9/2021 13:30:00).

Using the code below I want to combine date and time columns into a single Date object to feed into createEvent() method but get and error of Invalid Date.

snapshot of used sheet

function calendarSyncNew() {
  var dataSheet = mySheet;
  var calendarId = calendarId;
  var eventCal = CalendarApp.getCalendarById(calendarId);
  var eventArray = dataSheet.getRange('A2:E').getValues();

  for (x = 0; x < eventArray.length; x++) {

    var event = eventArray[x];
    var eventName = event[3];
    var eventDate = event[0];
    var timeZone = event[4];
    var startTime = new Date(`${eventDate} ${event[1]} ${timeZone}`); 
    var endTime = new Date(`${eventDate} ${event[2]} ${timeZone}`);
    eventCal.createEvent(eventName, startTime, endTime);
    Logger.log(`startTime is: ${startTime}`);
    Logger.log(`endTime is: ${endTime}`);
  }
    
}
    
2
  • Can you provide a sample of your 3 different objects @Ari?
    – NightEye
    Commented Jan 11, 2022 at 16:03
  • If you could provide the data you are working on, that would be great.
    – NightEye
    Commented Jan 11, 2022 at 16:19

1 Answer 1

2

I assume this question is related to your previous question? If that's the case, kindly see the sample data I used and the script below:

Sample Data:

sample

Script:

  // use getDisplayValues for getting the string equivalent of the data instead
  // filter out rows without values in column A
  var eventArray = dataSheet.getRange('A2:I')
                            .getDisplayValues()
                            .filter(x => x[0] != '');

  eventArray.forEach(event => {
    var eventName = event[4];
    var startTime = new Date(`${event[0]} ${event[1]} ${event[3]}`);
    var endTime = new Date(`${event[0]} ${event[2]} ${event[3]}`);
    eventCal.createEvent(eventName, startTime, endTime);
  });

Output:

output

Output was adjusted to my timezone so it doesn't reflect the dates from the sample data properly but it checks out.

8
  • I wrote this solution using for loop instead and get an error of invalid date. Please see the Edits to the question.
    – user11899141
    Commented Jan 11, 2022 at 22:06
  • @Ari, I dont see any recent edits to your question
    – NightEye
    Commented Jan 11, 2022 at 22:11
  • 1
    @Ari, Most probably, columns B and C are being read as date so appending them directly won't work. You need to get the string values of them to properly concatenate them. Also, you are getting column E value but your range is only until D. Instead of var eventArray = dataSheet.getRange('A2:D').getValues();, can you try and use var eventArray = dataSheet.getRange('A2:E').getDisplayValues();?
    – NightEye
    Commented Jan 11, 2022 at 22:28
  • 1
    @Ari, maybe you didn't notice it but my answer above uses getDisplayValues to get the string equivalent for easier string manipulation. Columns B and C are most likely being read as date objects when using getValues so you better use getDisplayValues.
    – NightEye
    Commented Jan 11, 2022 at 22:30
  • 1
    It actually worked! Also thanks for the heads up on column E I fixed it. Thanks a bunch!
    – user11899141
    Commented Jan 11, 2022 at 22:32