Question

So, based on lots of internet sleuthing I've managed to cobble together a script that uses a Google Sheet to create all-day calendar entries. There's probably ~450 total rows of content that I would like to create events for, but every time I run the script it never gets to the end, and it never creates an EventId (to address duplicates), rather, it gives me an "Exceeded Maximum Execution Time" error.

    $ function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Export Events",
functionName : "exportEvents"
}];
sheet.addMenu("Calendar Actions", entries);
};

function exportEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var headerRows = 0;  // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getValues();
var calId = "evsck12.com_fp4p25esvij964uhmfs51o9ar8@group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
if (i < headerRows) continue; // Skip header row(s)
var row = data[i];
var date = new Date(row[0]);  // First column
var title = row[1];           // Second column
var id = row[3];              // Sixth column == eventId
// Check if event already exists, update it if it does
try {
  var event = cal.getEventSeriesById(id);
}
catch (e) {
  // do nothing - we just want to avoid the exception when event doesn't exist
}
if (!event) {
  var newEvent = cal.createAllDayEvent(title, date).getId();
  row[3] = newEvent;  // Update the data array with event ID
}
else {
  event.setTitle(title);
range.setValues(data);
}
debugger;
}


}

My questions are these:

  • How can I avoid this error? If I split the data into several sheets will that work?
  • For the life of me I can't get the eventiD section to populate... I managed to do it on a test sheet but I can't do it anymore... any help would be fantastic!

For reference my sheet is set up as Date / Title / eventId

THANKS!!!!

Was it helpful?

Solution

first a few errors in your code...

  1. You update row[3] but forgot to update data with its corresponding row value, I simplified by using only data as variable.
  2. you used row[3] for third column, it is row[2] (now data[i][2])
  3. You updated the sheet at a wrong place in the loop

The working code is below (I changed the cal ID to one I own for test purpose... restore yours of course)

As for the time limit, you should proceed by limited batch of events, take a look at the execution transcript to see how long it takes for 1 and multiply...to get a value well below 5 minutes which is the maximum execution time in Google Script. Then you should keep somewhere the value of the row index (i) and using a time trigger proceed with the next batch starting at this index.

The documentation on trigger creation will help you and the scriptProperties is the ideal place to keep the index value in a key/value schema. See doc as well.

If you have issues with that don't hesitate to come back with a new question .

function exportEvents() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var headerRows = 1;  // Number of rows of header info (to skip)
  var range = sheet.getDataRange();
  var data = range.getValues();
  var calId = "h22nevo15tm0nojb6ul4hu7ft8@group.calendar.google.com";
  var cal = CalendarApp.getCalendarById(calId);
  for (i in data) {
    if (i < headerRows) continue; // Skip header row(s)
    var date = new Date(data[i][0]);  // First column
    var title = data[i][1];           // Second column
    var id = data[i][2];              // third column == eventId
    // Check if event already exists, update it if it does
    try {
      var event = cal.getEventSeriesById(id);
    }
    catch (e) {
      // do nothing - we just want to avoid the exception when event doesn't exist
}
    if (!event) {
      Logger.log(event);
      var newEvent = cal.createAllDayEvent(title, date);
      data[i][2] = newEvent.getId();  // Update the data array with event ID
    }
    else {
      event.setTitle(title);
    }
  }
  Logger.log(data);
  sheet.getRange(1,1,data.length,data[0].length).setValues(data);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top