title: Event ID in wrong row, cannot call method deleteEvent
body: <p>I'm working on a spreadsheet for my department to create, edit, and delete work assignments to a google calendar. It's been quite a while since I did any actual coding, so I've been doing a lot of trial and error and referring to the Google App Script and Calendar references. I've finally gotten to the point where I can create the events; however, when I am testing my function to delete events, I've discovered that the event on row two, deletes the event on row three, and when I try to delete the event on row three, I get a "cannot call method deleteEvent" error. I've tried looking through various sites for similar situations and have not been successful. Any help walking me through the error in my code issue would be greatly appreciated. I'm also attempting to attach a link for a video of the spreadsheet as the script is running <a href="https://drive.google.com/file/d/1P2y_Jn8s3dO9IK9cdSJEcbh7wfmoOan9/view?usp=sharing" rel="nofollow noreferrer">here</a></p>
  
  <pre><code>// Adds the custom menu to the active spreadsheet.
  function onOpen() {
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var menuEntries = [
      {
        name: "Create New Events",
        functionName: "createCalEvent"
      }, {
        name: "Update Existing Events",
        functionName: "updateCalEvent"
      }, {
        name: "Delete Existing Events",
        functionName: "deleteCalEvent"
      }
    ];
    spreadsheet.addMenu('Calendar Options', menuEntries);
  }
  // Event Status --&gt; Assist in Triggering Update/Delete Functions
  var delConfirmed = 'Event Created';
  var updateConfirmed = 'Event Updated';
  var eventCreate = 'Event Deleted';
  function createCalEvent(){
    var calSheet = SpreadsheetApp.getActiveSheet();
    var dataRange = calSheet.getRange('$A2:$J');
    var data = dataRange.getValues();
    for (var i = 0; i &lt; data.length; i++) {
      var row = data[i];
      var calDate = row[1]; // COL A
      var calTitle = row[5]; // COL F
      var calGuests = row[6]; // COL G
      var calEType = row[7]; // COL H
      var calID = row[8]; // COL I
      var calStatus = row[9]; // COL J
  // If there is a date and both the Event Type (Adjust/Delete) and Status are blank then Create the Calendar Event
      if(calDate !=='' &amp;&amp; calEType == '' &amp;&amp; calStatus == ''){
      var calEvent = CalendarApp
      .getCalendarById('envysion.com_kmfeb8mqmlv4j9k34l37q7fv3k@group.calendar.google.com')
      .createAllDayEvent(calTitle, new Date(calDate),{guests:calGuests});
        // Update the Status Column
      SpreadsheetApp.getActiveSheet().getRange(i+2, 10).setValue('Event Created');
      var newEvent = calEvent.getId();
        // Add the Event ID to the Event ID Column
      SpreadsheetApp.getActiveSheet().getRange(i+2,9).setValue(newEvent);
  
      Logger.log('Event ID: ' + newEvent + ' Title: ' + calEvent.getTitle() + 'Guests: ' + calEvent.getGuestList());
      }
    }
  }
  function deleteCalEvent()
  {
    // Get range of dates to delete
    var fromDate = new Date(2019,3,1);
    var toDate = new Date(2019,4,1);
  // Get Calendar Events
    var calendar = CalendarApp.getCalendarById('envysion.com_kmfeb8mqmlv4j9k34l37q7fv3k@group.calendar.google.com')
    var events = calendar.getEvents(fromDate, toDate);
  // Get Spreadsheet Details
    var calSheet = SpreadsheetApp.getActiveSheet();
    var dataRange = calSheet.getRange('$A2:$J');
    var data = dataRange.getValues();
    for (var i = 0; i &lt; data.length; i++) {
      var row = data[i];
      var calDate = row[1];
      var calTitle = row[5];
      var calGuests = row[6];
      var calEType = row[7];
      var calID = row[8];
      var calStatus = row[9];
  // Check if the Type column is set to Delete and if the event hasn't already been deleted
      if(calEType == 'Delete' &amp;&amp; calStatus !== 'Event Deleted'){
        events[i].deleteEvent();
        SpreadsheetApp.getActiveSheet().getRange(i+2, 10).setValue('Event Deleted');
        SpreadsheetApp.getActiveSheet().getRange(i+2, 9).setValue('');
      }
    }
  }
  </code></pre>