0

I am new to Google Apps Scripts, please help me how to get last filled row in this code. My actual data on the sheet is A1:F26 (F26 is variable as data is coming from other source sheets using query).
I want to send that data using email, Code is sending emails but picking all empty rows too. So i need to adjust, please help thanks in advance.

function sendMail(){
 var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email");
 var data = sh.getRange("A1:f").getValues();
 //var htmltable =[];

var TABLEFORMAT = 'cellspacing="Auto" cellpadding="Auto" dir="ltr" border="1" style="width:100%;table-layout:fixed;font-size:10pt;font-family:arial,sans,sans-serif;border-collapse:collapse;border:1px solid #ccc;font-weight:normal;color:black;background-color:white;text-align:left;text-decoration:auto;font-style:normal;'
var htmltable = '<table ' + TABLEFORMAT +' ">';

for (row = 0; row<data.length; row++){

htmltable += '<tr>';

for (col = 0 ;col<data[row].length; col++){
  if (data[row][col] === "" || 0) {htmltable += '<td>' + 'None' + '</td>';} 
  else
    if (row === 0)  {
      htmltable += '<th>' + data[row][col] + '</th>';
    }

  else {htmltable += '<td>' + data[row][col] + '</td>';}
}

     htmltable += '</tr>';
}

     htmltable += '</table>';
     Logger.log(data);
     Logger.log(htmltable);
MailApp.sendEmail(Session.getActiveUser().getEmail(), 'Daily report','' ,{htmlBody: htmltable})
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Replace `sh.getRange("A1:f").getValues();` to `sh.getRange("A1:F"+sh.getLastRow()).getValues();` – Marios Mar 03 '21 at 21:47
  • Thank you Mario, but its showing following error: `ReferenceError: data is not defined sendMail @ sendEmails.gs:30` – Atif Qayyum Mar 03 '21 at 22:14

0 Answers0