Home > OS >  Is there a way to find if one or more events within a recurring series has been deleted?
Is there a way to find if one or more events within a recurring series has been deleted?

Time:12-07

I'm using the following code to check if there are cancelled events in a Google Calendar. It works fine for regular, non-recurring events. However, I run into issue when the user has deleted a single event in a recurring event series.

  function checkForCancelledEvents(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Data Source");
  ss.setActiveSheet(sheet);

  //sort by date added
  var lastRow = sheet.getLastRow();
  var range = sheet.getRange(1,2,lastRow, 5)
  var values = range.getValues();
  //in the array the 3 position is the eventId, the 4 position is the calendar ID, I'm putting more into the array since I was playing around with also using the start date or time to solve the problem

  //loop through all using Calendar.Events.get
  for (i=1;i<values.length;i  ) {
    var splitEventID = values[i][3].toString().split("@")[0] //remove the calendarID from the eventID

    if (Calendar.Events.get(values[i][4],splitEventID).status === "cancelled") {
      //the below clears the content of the row that contains the cancelled events
      //the range has to start at i   1 since the values include the header row, but the for loop starts at 1 instead of 0 to prevent the API from calling with data from header row (which produces an error). So i   1 gives the accurate row number for which the cancelled event lives
      var clearRange = sheet.getRange(i   1,1,1,7) 
      clearRange.clearContent()
    } else {
      //Logger.log("this is NOT cancelled")
    }
  }
}

The issue is that recurring events all contain the same eventID and calendarID. They have the same iCalUID as well. Recurring events do have a different id but non-recurring events don't have the same id format. I tried to use Calendar.event.list and add the timeMin of each event within the recurring series, however the event was still listed as confirmed even though it was deleted. Is there a way to find if a single event within a recurring series has been deleted?

CodePudding user response:

I believe your goal is as follows.

  • From Is there a way to find if a single event within a recurring series has been deleted?, you want to know whether even one of the events from the recurring events is removed using Google Apps Script.

In this case, I thought that when the method of "Events: instances" is used, your goal might be able to be achieved. The sample script is as follows.

Sample script:

Before you use this script, please enable Calendar API at Advanced Google services.

var calendarId = "###"; // Please set the calendar ID.
var eventId = "###"; // Please set the event ID.

var ar = [];
var pageToken = "";
do {
  var res = Calendar.Events.instances(calendarId, eventId, {maxResults: 2500, showDeleted: true, pageToken});
  if (res.items.length > 0) ar = [...ar, ...res.items.filter(({status}) => status == "cancelled")];
  pageToken = res.nextPageToken;
} while (pageToken);
if (ar.length > 0) {
  // When "ar" has the values, the events are deleted from the recurring events.

  // do something.

}
  • In the above script, you can see the information of the deleted events at ar.

Note:

  • This is a simple sample script. So please modify this for your actual situation.

Reference:

  • Related