title: Google Apps Script double deleting rows in spreadsheet
body: <pre><code>function onEdit() {
  var openRequests = SpreadsheetApp.getActive().getSheetByName('Open Requests');
  var lastRowOpen = openRequests.getLastRow();
  
  var closedRequests = SpreadsheetApp.getActive().getSheetByName('Closed Requests');
  var lastRowClose = closedRequests.getLastRow();
  
  var closed = openRequests.getRange(2,8,lastRowOpen,1).getValues();
  
  for (var i = 0; i &lt; lastRowOpen; i++)
  {
      if (closed[i][0].toString() == 'Yes')
      {
          var line = i+2;
          if (closedRequests.getLastRow() == 1)
          {
              openRequests.getRange(line,1,1,9).copyTo(closedRequests.getRange(2,1,1,9));
              closedRequests.getRange(2,9,1,1).setValue(new Date());
              openRequests.deleteRow(line);
          }
          else
          {
              openRequests.getRange(line,1,1,9).copyTo(closedRequests.getRange(lastRowClose+1,1,1,9));
              closedRequests.getRange(lastRowClose+1,9,1,1).setValue(new Date());
              openRequests.deleteRow(line);
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I have set up a trigger to run onEdit.  What it does is check a column called <code>Closed</code> to see if it says <code>Yes</code>.  The <code>Closed</code> column has a data validation drop down menu with the value <code>Yes</code> in it.</p>
  
  <p>So when I click on the drop down menu and select <code>Yes</code>, it should copy the whole row to another sheet called <code>Closed Requests</code> then delete that row from the spreadsheet called <code>Open Requests</code>.</p>
  
  <p>The issue I am having is that about 50% of the time, it deletes the row I select <code>Yes</code> to but it ALSO deletes the row below it (and about 50% of the time when this happens, only some times does the second deleted row show up in <code>Closed Requests</code>, the other times the whole row just disappears forever unless I undo).</p>
  
  <p>From what I can tell, the <code>deleteRow()</code> function deletes the whole row and shifts all rows below it up a row to fill in the blank.  So the row below the one meant to be deleted gets shifted up to the same row and also gets deleted.  I don't know why the function is getting called twice though.</p>
  
  <p>I tried adding some delays but it does not seem to be working.</p>