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

2024/2/27 7:37:41

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
}
Answer

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

http://en.ppmy.cn/q/41648.html

Related Q&A

Javascript to grab Javascript comments within head

I have a page that begins as follows:<html><head><!-- 12036,2011-11-29/11:02 --><title>Products & Services</title>I would like to grab the 12036 number here and to dis…

How does one use prompt and alert javascript commands in webstorm?

When i try to use prompt or alert to get user input in webstorm, I get an error: answers[i] = prompt(allQuestions[i].question)^ReferenceError: prompt is not definedAs far as I know, prompt and alert ar…

Easy to Program WebSocket Server

Im starting to read about websockets, but I cant find a good WebSocket server and easy to program some examples...Im a complete beginner, and I dont need a server that supports a big concurrency, I jus…

How to fetch data from a particular github txt file in html page via javascript

I want to fetch the data of this url https://github.com/ProjectSakura/OTA/blob/10/changelog/changelog_beryllium.txt#L2 I am trying to use fetch api to get the data but i am getting cors error Here is w…

React Native behavior different in simulator / on device / with or without Chrome debugging

Im building a React Native app (currently iOS only) and I found a very nasty bug that only manifests itself when 1.) running on a real device, and 2.) running without Chrome debugging. (Ouch, right?)I…

Change text selection highlight with JS

For standard browsers you can use something like this to change the coloring of selected text:div.txtArea::selection {background: transparent; }div.txtArea::-moz-selection {background: transparent; }di…

AngularJS bug in IE with style vs ng-style

Im using Angular JS v1.1.5, and found an interesting IE-related issue.In IE 9, 10, 11 and Edge, the following does not seem to work, even though it works fine in Chrome:<div style="width: {{pro…

React Router v6.0.0-alpha.5 history prop removal - navigating outside of react context

According to the latest v6.0.0-alpha.5 release of react router, the history prop has been removed: https://github.com/ReactTraining/react-router/releases/tag/v6.0.0-alpha.5Removed the <Router histor…

Can jQuery Parse HTML Stored in a Variable?

Im using PHP and an ajax command to take the entire HTML contents of an external web page (via the PHP file_get_contents() command) and passing that HTML into a javascript variable. Once I have the pag…

JS library best practice: Return undefined or throw error on bad function input?

When coding a library in JavaScript, what is the most standard (friendliest?) way to handle invalid input to a function? My gut tells me that returning undefined is perfectly fine, but is it actually…