title: Emailing Script after Sort Broke
body: <p>I have posted about this on here so I hope it's ok to ask in regards to a separate issue with this code.</p>
  <p>I have the script email when new submissions are sent in using a form.  It was working great but then I wanted to sort newest on top and now I get no emails.  What did I miss in this script?</p>
  <p>Sort Script</p>
  <pre><code>// Dirty but works
  var SORT_COLUMN_INDEX = 1;
  var ASCENDING = false;
  
  var NUMBER_OF_HEADER_ROWS = 1;
  
  
  var activeSheet;
  
  function autoSort(sheet) {
    var range = sheet.getDataRange();
  
    if (NUMBER_OF_HEADER_ROWS &gt; 0) {
  
      range = range.offset(NUMBER_OF_HEADER_ROWS, 0);
    }
  
    range.sort( {
      column: SORT_COLUMN_INDEX,
      ascending: ASCENDING
    } );
  }
  
  
  function onEdit(event) {
    var editedCell;
  
    activeSheet = SpreadsheetApp.getActiveSheet();
  
    editedCell = activeSheet.getActiveCell();
  
    if (editedCell.getColumn() == SORT_COLUMN_INDEX) {
      autoSort(activeSheet);
    }
  }
  
  
  function onOpen(event) {
    activeSheet = SpreadsheetApp.getActiveSheet();
    autoSort(activeSheet);
  }
  
  function onInstall(event) {
    onOpen(event);
  }
  
  
  </code></pre>
  <h2>Email Script</h2>
  <pre><code>function SendEmail() {
   var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
   var StartRow = 2;
   var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
   var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,19);
   var AllValues = WholeRange.getValues();
  
   var message = &quot;&quot;;
   for (i in AllValues) {
   var CurrentRow = AllValues[i];
  
   var EmailSent = CurrentRow[19];
  
   if (EmailSent == &quot;Sent&quot;) 
       continue;
  
    message =
        &quot;&lt;p&gt;&lt;b&gt;Email?: &lt;/b&gt;&quot; + CurrentRow[1] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;District: &lt;/b&gt;&quot; + CurrentRow[2] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Vendor Cost: &lt;/b&gt;&quot; + CurrentRow[3] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Vendor Cost: &lt;/b&gt;&quot; + CurrentRow[4] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Default Price: &lt;/b&gt;&quot; + CurrentRow[5] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Vendor: &lt;/b&gt;&quot; + CurrentRow[6] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Vendor SKU: &lt;/b&gt;&quot; + CurrentRow[7] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Manufacturer: &lt;/b&gt;&quot; + CurrentRow[8] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Manufacturer SKU: &lt;/b&gt;&quot; + CurrentRow[9] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Barcode: &lt;/b&gt;&quot; + CurrentRow[10] + &quot;&lt;/p&gt;&quot;+
        &quot;&lt;p&gt;&lt;b&gt;Category: &lt;/b&gt;&quot; + CurrentRow[11] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Device: &lt;/b&gt;&quot; + CurrentRow[12] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Model: &lt;/b&gt;&quot; + CurrentRow[13] + &quot;&lt;/p&gt;&quot;+
        &quot;&lt;p&gt;&lt;b&gt;Color: &lt;/b&gt;&quot; + CurrentRow[14] + &quot;&lt;/p&gt;&quot;;
  
  
    var setRow = parseInt(i) + StartRow;
  
  
    ActiveSheet.getRange(setRow, 19).setValue(&quot;Sent&quot;);
  }
  
  
   var SendTo = &quot;@vzwpix.com&quot;;
   var SendToEmail = &quot;davidm&quot;;
   
  
    var Subject = &quot;New Sku:&quot; +&quot; &quot;+ CurrentRow[13] +&quot; &quot; +&quot;From:&quot;+&quot; &quot;+CurrentRow[1];
  
    MailApp.sendEmail({
        to: SendTo,
        cc: &quot;&quot;,
        subject: Subject,
        htmlBody: message,
  });
    
     MailApp.sendEmail({
        to: SendToEmail,
        cc: &quot;&quot;,
        subject: Subject,
        htmlBody: message,
  });
    
  
  }
  </code></pre>
  <h2>Alternate Working Script</h2>
  <p>I adjusted my script to search for empty column and send mail
  Here is the Script:</p>
  <pre><code>function sendEmails() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var startRow = 3; // Start at second row because the first row contains the data labels
    var lastRow = sheet.getLastRow()
    var lastCol = sheet.getLastColumn()
  
    // Fetch the range of cells, in this case A2:T
    // Column B, row[2] = Email Address, Column C, row[1] = Name
    var dataRange = sheet.getRange(startRow, 1, lastRow, lastCol)
    
    // Fetch values for each row in the Range.
    var data = dataRange.getValues();
  
    for (var i = 0; i &lt; data.length-1; ++i) {
      var row = data[i];
     var emailAddress = row[1]; // 
      var message = 
        &quot;&lt;p&gt;&lt;b&gt;Email?: &lt;/b&gt;&quot; + row[1] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;District: &lt;/b&gt;&quot; + row[2] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Vendor Cost: &lt;/b&gt;&quot; + row[3] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Vendor Cost: &lt;/b&gt;&quot; + row[4] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Default Price: &lt;/b&gt;&quot; + row[5] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Vendor: &lt;/b&gt;&quot; + row[6] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Vendor SKU: &lt;/b&gt;&quot; + row[7] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Manufacturer: &lt;/b&gt;&quot; + row[8] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Manufacturer SKU: &lt;/b&gt;&quot; + row[9] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Barcode: &lt;/b&gt;&quot; + row[10] + &quot;&lt;/p&gt;&quot;+
        &quot;&lt;p&gt;&lt;b&gt;Category: &lt;/b&gt;&quot; + row[11] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Device: &lt;/b&gt;&quot; + row[12] + &quot;&lt;/p&gt;&quot; +
        &quot;&lt;p&gt;&lt;b&gt;Model: &lt;/b&gt;&quot; + row[13] + &quot;&lt;/p&gt;&quot;+
        &quot;&lt;p&gt;&lt;b&gt;Color: &lt;/b&gt;&quot; + row[14] + &quot;&lt;/p&gt;&quot;
      
      
      
      ; // Assemble the body text
          var cell = row[19]; // Last column
      if (cell == &quot;&quot;) { 
     var subject = &quot;Sending emails from a Spreadsheet&quot;;
    MailApp.sendEmail({
        to: &quot;PHONE@vzwpix.com&quot;,
        cc: &quot;&quot;,
        subject: subject,
        htmlBody: message,
  });
    }
  }
  }
  </code></pre>