I'm trying to email a range as a table with the same format as in the sheet, I got it where it sends a table but it will display any blank space as the word "None" and no format is being added.
I've been looking for a correct solution on this, and there are a few answers that worked well but without the correct formatting to the table, any suggestions?
Below my code:
function sendMail(){
var shh = SpreadsheetApp.getActiveSpreadsheet();
var sh = shh.getSheetByName('Email');
var data = sh.getRange("A1:J34").getValues();
//var htmltable =[];
var TABLEFORMAT = 'cellspacing="2" cellpadding="2" 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:center;text-decoration:none;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('example@gmail.com', 'Daily report','' ,{htmlBody: htmltable})
}
Based on my replication, the HTML formatting is correct, so only the hardcoded "None" value needs to be replaced. You can put a single white space like this:
EDIT: You would need to use
getBackgrounds
on the range then copy the array elements into the HTML string in the loop.
function sendMail(){
var shh = SpreadsheetApp.getActiveSpreadsheet();
var sh = shh.getSheetByName('Email');
var r = sh.getRange('A1:J34');
var data = r.getValues();
var bgcolors = r.getBackgrounds();
...
for (col = 0 ;col<data[row].length; col++){
// put white space when empty
if (data[row][col] === "" || 0) {htmltable += '<td style="background-color:' + bgcolors[row][col] + ';">' + ' ' + '</td>';}
else
if (row === 0) {
htmltable += '<th style="background-color:' + bgcolors[row][col] + ';">' + data[row][col] + '</th>';
}
else {htmltable += '<td style="background-color:' + bgcolors[row][col] + ';">' + data[row][col] + '</td>';}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.