javascriptgoogle-apps-scriptgoogle-calendar-apigoogle-calendar-recurring-events

Google Calendar script: remove recurrence


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().


Solution

  • 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);
    }