4

I'm iterating through a spreadsheet of events and modifying my calendar accordingly.

I can use setRecurrence to create multi-day events, but I can't figure out how to remove the class and turn it back into a single-day event.

It's important to know because evidently applying setRecurrence to an event more than once does not replace/redefine the existing recurrence, but keeps tacking additional classes onto it. This prevents simple workarounds, such as applying a recurrence of 0 days, etc.

I'm looking for the equivalent of event.removeRecurrence().

Rubén
  • 34,714
  • 9
  • 70
  • 166
harshaw61
  • 63
  • 9

3 Answers3

2

This code uses advanced Calendar service, you have to enable it in the script editor menu : ressources/advanced Google Services ***

function ChangeEvent(){
  var calendarId = 'primary';
  var eventId = 'omv6###########e8jbs';
  var event = Calendar.Events.get(calendarId, eventId);
  Logger.log('old recurrence = '+event.recurrence);
  event.recurrence = '';
  Calendar.Events.patch(event,calendarId,eventId);
  Logger.log('new recurrence = '+event.recurrence);
}

enter image description here


EDIT

following your comment,

Please note that the ID used by the advanced calendar API is slightly different as it does not include the @google.com. You should simply remove this last part before using it.

Example :

[16-02-19 07:22:59:739 CET] ba4a1dub73uqsvhld3abh15f38@google.com
[16-02-19 07:22:59:740 CET] ba4a1dub73uqsvhld3abh15f38

Use some string methods to get the result we need :

  Logger.log(event.getId());// event is the event you get using calendarApp
  var advancedID = event.getId().substring(0,event.getId().indexOf('@'));
  Logger.log(advancedID);// this ID is for advanced service
}

Working example :

First create an event with a 5 days recurrence using calendarApp (so we are in your real condition) using createEventRec()

Check on the calendar that the event is as expected

Then use changeEvent() and check the result

function createEventRec(){
  var cal = CalendarApp.getDefaultCalendar();
  var recurrence = CalendarApp.newRecurrence().addDailyRule().times(5);
  var event = cal.createEventSeries('Dinner with Mary', new Date(),new Date(new Date().getTime()+3600000), recurrence);
  Logger.log(event.getId());
  PropertiesService.getScriptProperties().setProperty('ID',event.getId());
}


function ChangeEventRecurrence(){
  var calendarId = 'primary';
  var ID = PropertiesService.getScriptProperties().getProperty('ID');
  var advancedID = ID.substring(0,ID.indexOf('@'));
  Logger.log(advancedID);
  var event = Calendar.Events.get(calendarId, advancedID);
  Logger.log('old recurrence = '+event.recurrence);
  event.recurrence = '';
  Calendar.Events.patch(event,calendarId,advancedID);
  Logger.log('new recurrence = '+event.recurrence);
}
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Thanks very much for your answer. I now have the advanced Calendar service running, but I'm getting "Error: Exception: Not Found" using my event IDs (which look like xxx@google.com). My calendar ID (xxx@group.calendar.google.com) works fine. – harshaw61 Feb 19 '16 at 04:05
  • See edit please. The Id is slightly different for advanced service. – Serge insas Feb 19 '16 at 06:31
  • Thank you again. I had tried that, but unfortunately I'm still getting an Error: Exception: Not Found. My event id now looks like: k7oh14d7**********lchssj2g. I've checked and the event does exist. – harshaw61 Feb 19 '16 at 10:11
  • I added a working demo so you can see how it works. For the example purpose I store the ID in scriptProperties but of course you can store it in a spreadsheet as well. – Serge insas Feb 19 '16 at 15:59
  • Thank you so much, I had no idea the advanced calendar API existed until today! – jazzwhistle Apr 28 '19 at 00:31
0
ev.removeAllReminders();
ev.deleteEvent();
cs95
  • 379,657
  • 97
  • 704
  • 746
0

This could would help you to delete events recursively from Jan 1st through 10th Jan.

function delete_rec_events()
{
  //take care: Date function starts at 0 for the month (January=0)
  var fromDate = new Date(2018,0,1,0,0,0); //This is January 1, 2018
  var toDate = new Date(2018,0,10,0,0,0);   //This is January 10, 2018 at 00h00'00"

  var calendarName = '<Name of the Calendar>';    
  var calendar = CalendarApp.getCalendarsByName(calendarName)[0];
  var events = calendar.getEvents(fromDate, toDate);

  for(var i=0; i<events.length;i++)
  {
    var ev = events[i];
    Logger.log('Checking Event Title === ' + calendar.getEventSeriesById(ev.getId()).getTitle());
    calendar.getEventSeriesById(ev.getId()).deleteEventSeries();
    Logger.log('Event Deleted');
  }
}