title: Why is my emailSent field writing to row 20 instead of row 2?
body: <p>My code seems to be working properly, except for line 22, which is writing an "emailSent" value to line 20 of the Google Spreadsheet that the script runs against, instead of line 2 of that spreadsheet, which is the only row that has data. Any ideas?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function sendEmails2() {
    //trying to adopt code from this tutorial https://developers.google.com/apps-script/articles/sending_emails
    //-believe there is an issue with where I am declaring getRange to work-
    var sheet = SpreadsheetApp.getActiveSheet();
    var startRow = 2;  // First row of data to process
    var lastRow = 40;   // Last row of data to process
    // Fetch the range of cells A1:B10002
    var dataRange = sheet.getRange(startRow, 1, lastRow, 10)
    // Fetch values for each row in the Range.
    var data = dataRange.getValues();
    var i = data.length; 
    for (i in data) {
      var row = data[i];
      var emailAddress = row[7];  // raised an error at one point 
      var isEmailSent = row[9];     //
      var partTimeApproved = row[0]
      if (partTimeApproved != '') { // prevents sending emails to non-approved rows. for some reason = 'Y' doesn't work but !='' does //... or does it
      if (isEmailSent == '') {  // Prevents sending duplicates and sending emails to non-approved rows
        var subject = "Email Subject Test";
        var message = "Email body test";    // Standard string
        MailApp.sendEmail(emailAddress, subject, message);
        sheet.getRange(startRow + i, 10).setValue('emailSent'); //this didn't work: -replacing startRow + i with row-
        // Make sure the cell is updated right away in case the script is interrupted
        SpreadsheetApp.flush();
      }
    }
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I understand that while the code I am adopting from uses </p>
  
  <pre><code>getrange(startRow + i, 10).setValue('emailSent'); 
  </code></pre>
  
  <p>however, I do not understand how it could be finding row 20 and am not sure how to proceed in fixing it.</p>