title: Prevent duplicate calendar events from Apps Script
body: <p>I've read through a number of posts on SO and elsewhere as to how to prevent a script from continuing to add duplicate calendar events each time it's run. I've been unsuccessful so far in stopping the duplicates.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>function onOpen() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
    var menuEntries = [ {name: "Push to Calendar", functionName: "sendCalendarEvents"} ];
    ss.addMenu("Custom Menu", menuEntries);
  
  }
  
  function sendCalendarEvents() {
    var spreadsheet = SpreadsheetApp.getActiveSheet();
    var calendarId = spreadsheet.getRange('G1').getValue();
    var eventCal = CalendarApp.getCalendarById(calendarId);
    var lastRow = spreadsheet.getLastRow();
    var count = spreadsheet.getRange("A3:E"+lastRow+"").getValues();//five columns
    var minutesBefore = 462
    for (x=0; x&lt;count.length; x++) {
        var row = count[x];
        var title = row[0];
        var startTime = row[1];
        var endTime = row[2];
        var guests = row[3];
        var description = row[4];
        var location = row[5];      
        var id = row[7];no row[7]
  
      var options = {
            'location': location,
            'description': description,
            'guests':guests +',',
            'sendInvites': 'True',
        }
  
     if(!id) {
  
     var event = eventCal.createAllDayEvent(title, startTime, options); 
      var newEventId = event.getId();
       spreadsheet.getRange(x+3,7).setValue('yes');
       spreadsheet.getRange(x+3,8).setValue(newEventId);
  
      event.addEmailReminder(minutesBefore);
  
       Logger.log('Event ID: ' + event.getId());
  
  </code></pre>
  
  <p>and here's my spreadsheet (with 'test' data)</p>
  
  <p><a href="https://i.stack.imgur.com/EsMsJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EsMsJ.png" alt="enter image description here"></a></p>
  
  <p>I've also tried variations of the 'if' statement (<em>like if (row[x][7] != 'yes')...create the event</em>) but that hasn't worked either.</p>
  
  <p>Any help? After the duplicate issue is resolved, I then want to be able to have a user edit the date or title or such of an event in the spreadsheet and have the existing event deleted and then a new event (with the updated title/date) be created...if that's possible.</p>
  
  <p>Thanks for any help you can provide!</p>