Aaron
Aaron

Reputation: 3325

Deleting Events with Google Calendar API

Reference: https://developers.google.com/calendar/v3/reference/events/delete

Errors: Missing name after . operator. (line 131, file "Code")

function deleteEvent() {

  for (var i = 1; i < datalastRow; i++) {

    var calendarId = sheet.getRange('E2').getValue();

    var delete_eventid = sheet.getRange(i + 1, 3).getValue()

      var params = {
        calendarId: calendarId,
        eventId: delete_eventid,
      };

      calendar.events.delete(params, function(err) {
        if (err) {
          console.log('The API returned an error: ' + err);
          return;
        }
        console.log('Event deleted.');
      });

  }

}

Basically the line calendar.events.delete(params, function(err)) { is throwing an error when I attempt to save the code, I am unsure why this is happening as I can run calendar.events.insert and create events just fine, but it appears to not like calendar.events.delete.

Edit: I removed the extra ")" as referenced in the first answer

Upvotes: 1

Views: 6121

Answers (3)

Sukit Roongapinun
Sukit Roongapinun

Reputation: 1

First, get event from service.events().list() method.

Second, access the list above to reach "items". There will be the "id".

Third, use that "id" for eventId.

Fourth, use that eventId for delete. Function for event.delete is here

Most importantly, don't loop through the events to get the eventId. You must access it through the "items" to get the eventId.

Upvotes: 0

Elsa
Elsa

Reputation: 724

If you use Google Apps Script, how about this? You can delete events by deleteEvent(). deleteEvent() returns void. So I used try-catch for this.

function deleteEvent() {
  var calendarId = sheet.getRange('E2').getValue();
  for (var i = 1; i < datalastRow; i++) {
    var delete_eventid = sheet.getRange(i + 1, 3).getValue();
    try {
      CalendarApp.getCalendarById(calendarId).getEventById(delete_eventid).deleteEvent();
    } catch(e) {
      console.error("Error deleting event " + delete_eventid + " from calendar " + calendarId + ". Error: " + e);
    }
  }
}

Upvotes: 2

Joshua Nelson
Joshua Nelson

Reputation: 581

You have a syntax error on this line:

calendar.events.delete(params, function(err)) {

The second parameter to delete should be a function, but it should look like this:

calendar.events.delete(params, function(err) { ... })

The difference being that the second ) wraps the callback.

Upvotes: 0

Related Questions