inkychris
inkychris

Reputation: 1449

Google Apps Script: How to check if a calendar event has been marked as deleted manually

I've written a Google Apps Script which adds events kept in a spreadsheet to a google calendar. Each event stores the calendar event's ID so that it can update it if the event in the spreadsheet gets modified. The event can still be deleted manually in the calendar so I would like the script to detect this and recreate the event. Unfortunately, I cannot simply check for null when calling getEventById(...) on a calendar object because even when deleted, this function seems to be returning a valid calendar object.

The CalendarEvent API documentation doesn't seem to point to a way to check if the event has been deleted via the UI/manually. Is there a way of accessing this property through the Google Apps API?

Even permanently deleting trashed events does not fix the problem, although it then prevents modifications to the returned event object.

What can I use to check if the event (which still exists) has been "deleted"? The only thing I can think might be of use is the get/setTag() methods for the CalendarEvent object.

Alternatively, this question looks similar, but it feels like there should be a better way than searching through a list of trashed events, and I'm not sure this would get around the issue of having ID strings return valid events that have been "permanently" deleted.

Example

var calendarId = "[email protected]"
var calendar = CalendarApp.getCalendarById(calendarId);

var event = calendar.createEvent(...);

// Event is then deleted manually in calendar but 
// ID has been saved in spreadsheet (or elsewhere)

if (calendar.getEventById("the-deleted-event-id") == null) {
    // Create the event (This is never reached)
} else {
    // Update the event
}

Upvotes: 5

Views: 3652

Answers (4)

pumpkin man
pumpkin man

Reputation: 1

Another solution would be to search only events that have not yet been deleted, which will cause deleted events to not be found.

let result = Calendar.Events.list(<Calendar_ID>, {showDeleted: false});

Upvotes: 0

Ronny Kurniawan
Ronny Kurniawan

Reputation: 1

I found the Simpler Way is check if the Return is null or Not. Null = event not found

https://developers.google.com/apps-script/reference/calendar/calendar#geteventbyidicalid

CalendarEvent — the event with the given ID, or null if the event doesn't exist or the user cannot access it.

Upvotes: -1

Harel
Harel

Reputation: 1337

I found that the best way to do this is to check whether the event in question is included when asking the calendar for events during the time period of event in question:

var event = calendar.getEventById(eventId);
var events = calendar.getEvents(event.getStartTime(),event.getEndTime());
var isEventDeleted = events.some((event) => event.getId() == eventId);

Methods used:

https://developers.google.com/apps-script/reference/calendar/calendar#getEventById(String) https://developers.google.com/apps-script/reference/calendar/calendar#geteventsstarttime,-endtime https://developers.google.com/apps-script/reference/calendar/calendar-event#getstarttime https://developers.google.com/apps-script/reference/calendar/calendar-event#getendtime https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Upvotes: 2

Jindřich Širůček
Jindřich Širůček

Reputation: 344

I have spent quite a time solving this and I cant udnerstand why there is no better way how to solve it, directly by CalendarApp. What you need is this:

var eventId = "[email protected]".split("@")[0];
if(Calendar.Events.get("yourCalendarId", eventId).status === "cancelled")
        CODE://what to do if event was deleted (manually)

Notes:

  1. As API v3.0 uses as eventid only the part before @, so you need to split (strip rest)
  2. You need Calendar API v3.0 first activate.

You will find more info about in this question: Checking a Google Calendar Event's Existence

Upvotes: 2

Related Questions