title: Reference Error: Using a For-Loop to create & send Emails from HTML Template in Google Apps Scripts
body: <p><strong>Hi,</strong></p>
  <p>I'm relatively new to javascript, google apps scripts, &amp; coding in general. I'm currently working in the Google Apps Script [g-script] environment w/ google sheets [g-sheet]. Here's what I'm trying to accomplish:</p>
  <ol>
  <li>Use a g-sheet to store a data set for accessing w/ g-script</li>
  <li>Use g-script to draft &amp; send an email using an HTML template [stored within the code editor] populated w/ the unique data located in each row.</li>
  </ol>
  <p>I have a block of code that gets the data from the sheet. I then have a defined function to send the emails by using a for-loop. Within that for-loop, I am attempting to use HTMLService class to create a template from the file, populate the # of templates [in this case 3] w/ unique data from each row, &amp; store it as a var &quot;htmlBody&quot;. Then, use the MailApp class to send the uniquely created HTML templates.</p>
  <p><strong>The error I'm receiving:</strong></p>
  <p>When I run the code, I get a Reference Error that &quot;row is not defined&quot;. I've added a picture of the Execution Log. The console.log(boatData[i]) statement is an effort trouble shoot &amp; is the info logged to the console for the first row in the data set.</p>
  <p>How do I fix this?</p>
  <p><strong>The Code:</strong></p>
  <pre><code>  var sheet = SpreadsheetApp.getActive().getSheetByName('DB.charters');
    var startRow = 3 ; 
    var numRows = 3 ;
    var dataRange = sheet.getRange(startRow,1,numRows,15);
    var boatData = dataRange.getValues();
  
  function sendEmail() {
    for (var i in boatData) {
      var row = boatData[i] ;
  
      function getEmailHtml() { // gets the HTML template &amp; makes it useable
        var htmlTemplate = HtmlService.createTemplateFromFile(&quot;emailTempWorkOrder.html&quot;); // uses HtmlService class to create an HtmlTemplate object from the file in the code editor
    htmlTemplate.boats = boatData[i] ; //makes the var boatData avail within the template through the proeprty .boats
      console.log(boatData[i]); // logs the first row only
       var htmlBody = htmlTemplate.evaluate().getContent(); // converts the html file into raw binary content for HTTP responses
       return htmlBody;
      } // end of getEmailHtml()
   
    var htmlBody = getEmailHtml();
    
    MailApp.sendEmail({ // Sends the email formatted
      to: &quot;bmsbreaux@gmail.com&quot;,
      subject: `Vessel Charter: ${row[1]} -`, // ${row[3]} [Client] [Date @ Time]
      htmlBody: htmlBody,
  
    }); // end of MailApp action
    } // end of for-loop
  } // end of sendEmail()
  
  // Confirmation statement.
  console.log('Ran sendEmailWorkOrder(), sent emails');
  </code></pre>