Search code examples
google-sheetseventsduplicatescalendar

Script that puts google sheet to calendar duplicates events every time it is run


I created a script that copies events from a google sheet and ads it to the calendar. Every time the sheet is updated the calendar needs to be updated. The problem is that the events are duplicated every time the script runs, i don't want duplicates. Below the code:

function copyCalender() {
  var spreadsheet = SpreadsheetApp.getActiveSheet()
  var eventCal = CalendarApp.getCalendarById("xxxx@group.calendar.google.com");
  var signups = spreadsheet.getDataRange().getValues();

  for(x=0;x<signups.length;x++) {
    var notAvailable=signups[x]

    var busy = notAvailable[0]
    var startTime = notAvailable[1]
    var endTime = notAvailable [2]

    eventCal.createEvent(busy, startTime, endTime);
  }
}

i expect the code to not dublicate events and only copy new events.


Solution

  • Your events are being duplicated because you don't have any option in place to contrast whether the event has been created for the provided data or not. What I've done is use "helper columns" to store the EventID and use it as an identifier. This might not be the most elegant solution, but it's what I've done. Don't forget that you can hide the "Helper column". 

    function createEvents() {
      var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetX');
      var eventCal = CalendarApp.getCalendarById("xxxx@group.calendar.google.com");
      var signups = ss.getDataRange().getValues();
      console.log(signups)
    
      for (x = 1; x < signups.length; x++) {
        var notAvailable = signups[x];
    // Please make sure the column references matches your data
        var busy = notAvailable[1];
        var startTime = new Date(notAvailable[2]);
        var endTime = new Date(notAvailable[3]);
        var eventId = notAvailable[4];
        var helperColumn = notAvailable[5];
        if (!helperColumn) {
          if (eventId) {
            // Try to get the event by ID
            try {
              var event = eventCal.getEventById(eventId);
              if (event) {
                // Event already exists
                event.setTitle(busy);
                event.setTime(startTime, endTime);
              } else {
                // Create a new event if not found
                event = eventCal.createEvent(busy, startTime, endTime);
                ss.getRange(x + 1, 5).setValue(event.getId()); //Stores EventID 
              }
            } catch (e) {
              // Event not found, we need to create it
              event = eventCal.createEvent(busy, startTime, endTime);
              ss.getRange(x + 1, 4).setValue(event.getId()); 
            }
          } else {
            // Create a new event if no event ID
            var event = eventCal.createEvent(busy, startTime, endTime);
            ss.getRange(x + 1, 5).setValue(event.getId()); 
          }
          // Mark the row as processed in the helper column
          ss.getRange(x + 1, 6).setValue("Event Created");
        }
      }
    }
    
    

    This is how I had my data in the spreadsheet: enter image description here