title: Google Apps Script - How to change value in cell after sending an automatic email?
body: <p>I would like my code to send an email based on a condition and then update a cell when that email has been sent. For instance, because row 16 in column D contains &quot;&quot;, I want to send a particular email and then update that cell to &quot;&quot;.</p>
  <pre><code>var spreadsheet   = SpreadsheetApp.getActiveSpreadsheet();
  var sheet         = spreadsheet.getSheetByName('');
  var data          = spreadsheet.getRange('!A2:E').getValues();
  var columnToCheck = spreadsheet.getRange(&quot;D4:D&quot;).getValues();
  var lastRow       = getLastRow(columnToCheck);
  
  function eMail() {
    data.forEach((row, index) =&gt; {
      let [fName, sName, date, email, ] = row;
  
      if (date == &quot;-&quot; &amp;&amp; email == &quot;started&quot;) {
        var htmlBody  = HtmlService.createHtmlOutputFromFile('').getContent()
        .replace(&quot;#fName&quot;, fName)
        .replace(&quot;#sName&quot;, sName);
        
        MailApp.sendEmail (&quot;example@email.com&quot;, &quot;Hiya&quot;, sName, {htmlBody: htmlBody});
        sheet.getRange(2,4,lastRow).setValue(&quot;continuing&quot;);
      }
    })
  }
  
  </code></pre>
  <p>What the code does, however, is put &quot;continuing&quot; in every cell in column D (not just the ones that match the if-statement, and thus who has been emailed). What am I mssing?</p>