0

Issue: Receiving error when running google app script in google sheets: "Exception: You have been creating or deleting too many calendars or calendar events in a short time. Please try again later."

I have a google apps script that integrates with google calendar. It creates an event when a row to the spreadsheet is added. If anything within that row is edited, it updates the original event (tied to the same event ID).

Help would be greatly apprecaited.

function exportEvents() {
  var space = " - "
  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 = "ID";
  var cal = CalendarApp.getCalendarById(calId);
  for (i=0; i<data.length; i++) {
    if (i < headerRows) continue; // Skip header row(s)
    var row = data[i];
    var date = new Date(row[5]);  // Scheduled  Publication Date
    var numb = row[2];
    var title = numb +space+ row[0];
    var loc = row[3];
    var desc = "Content 1: "+row[1] +"\n" +
               "\nContent 2 "+row[6]+ "\n" +
               "\nContent 3: "+row[9]+"\n" +
               "\nContent 4: "+row[7]+"\n" + 
               "\nContent 5 (If Drafted): "+row[4]+"\n" +
               "\nRunning List"
               
                  //Further code can be added to allow for additional records
    var id = row[8];              // eventId Hidden
        // 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) {
      //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
      var newEvent = cal.createAllDayEvent(title, date, {location:loc}).getId();
      row[9] = newEvent;  // Update the data array with event ID
    }
    else {
      event.setTitle(title);
      event.setLocation(loc);
      event.setDescription(desc);
      // event.setTime(tstart, tstop); // cannot setTime on eventSeries.
      // ... but we CAN set recurrence!
      var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
      event.setRecurrence(recurrence, date);
    }

  }
  // Record all event IDs to spreadsheet
  range.setValues(data);
 
}
whydah
  • 1
  • When you want to create a lot of events in a Google Calendar, how about using the batch request? So, is this thread useful for your situation? https://stackoverflow.com/q/65802629 – Tanaike Jan 21 '21 at 01:28
  • How many times is the spreadsheet updated in a day? The [create events quota](https://developers.google.com/apps-script/guides/services/quotas#current_quotas) states that for a free gmail you have 5000 calendar events per day. Are you surpassing that or are you creating multiple events at once? – Kessy Jan 21 '21 at 10:31

0 Answers0