You want to take a range of data from a Google sheet, convert it to an html table, and include it in an email. I have assumed that you know how to send an email - both html and non-html; the following answer focuses on creating the html table from the spreadsheet. The key challenge is that the css must be included inline.
It is worth noting that there are two date fields "9/20" and "9/21". If the standard getValues()
is used, these values are retrieved as full timestamp values. However, by using getDisplayValues()
the values are returned as displayed, and they can be included in the table without having to apply date formats.
The code generate a variable messagetable
. This can be includes in the email as the message, or it can be incorporated into the message.
function SO5722046102() {
// setup spreadheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// define table
var tablerange = sheet.getRange(4,1,4,3); // get the range
//Logger.log("the range is "+tablerange.getA1Notation());
// get the values - Display Values
var tabledata = tablerange.getDisplayValues();
// build the opening element of the html table
var messagetable = '<table style="border-collapse:collapse;border: 1px solid black;"><tbody>';
// Logger.log("messagetable = "+messagetable);
// loop through the rows and build up each row of the table
for (i= 0; i< 4;i++){
messagetable+='<tr style="border: 1px solid black;"><td style="border: 1px solid black;">'+tabledata[i][0]+"</td>"+'<td style="border: 1px solid black;">'+tabledata[i][1]+"</td>"+'<td style="border: 1px solid black;">'+tabledata[i][2]+"</td></tr>";
}
// add the closing html
messagetable=messagetable+"</tbody></table>";
// Logger.log("DEBUG: the table = "+messagetable);
// add code for sending email and include the table as either the message, or incorporate it into the message
}
<table style="border-collapse:collapse;border: 1px solid black;"><tbody>
<tr style="border: 1px solid black;"><td style="border: 1px solid black;">DATE</td><td style="border: 1px solid black;">09/20</td><td style="border: 1px solid black;">09/21</td></tr>
<tr style="border: 1px solid black;"><td style="border: 1px solid black;">AMOUNT</td><td style="border: 1px solid black;">115</td><td style="border: 1px solid black;">115</td></tr>
<tr style="border: 1px solid black;"><td style="border: 1px solid black;">NOTES</td><td style="border: 1px solid black;"></td><td style="border: 1px solid black;"></td></tr>
<tr style="border: 1px solid black;"><td style="border: 1px solid black;">TOTAL</td><td style="border: 1px solid black;"></td><td style="border: 1px solid black;">230</td></tr>
</tbody></table>
Spreadsheet screenshot
Table screenshot