问题
I have a spreadsheet that updates daily. values in Italic are being updated daily, Bold values are static labels.
Date: 9/20/2019
Number of cars in parking lot: 105
Last entry: 09:21 AM
Last Check out: Mr.X
I have setup a script to send an email with the updates to the users via googlespreadsheet. But the email that users receive is a plain text like this:
Date:,9/20/2019,Number of cars in parking lot:,105,Last entry:,09:21 AM,Last Check out:,Mr.X,,,,,
How can I make this code to send more userfriendly email with a table or colorful fonts, etc similar to a daily newsletter?
回答1:
So, given the requirement, this script would transform your sheet data into an HTML table and then send it as the message body over an email (you're free to further modify the CSS as required)
Assuming that your sheet looks something like this -
And that your desired output is something like this -
You could use the following code and make necessary changes as required -
var SheetID = "YourSheetIDGoesHere"; //replace with your Spreadsheet SheetID
var sheetName = "YourSheetNameGoesHere"; //replace with your Sheet Name
var sheet = SpreadsheetApp.openById(SheetID).getSheetByName(sheetName);
function sendUpdate() {
var range = sheet.getDataRange();
var values = range.getDisplayValues();
var weights = range.getFontWeights();
var rowDisplay = range.getLastRow();
var columnDisplay = range.getLastColumn();
var message = '';
message = message + "<table rules='all' border='3' style='border-color: #666;' cellpadding='5'>\n";
for (var i = 0; i < rowDisplay; i++ ) {
message = message + "<tr>\n";
for (var j = 0; j < columnDisplay; j++ ) {
message = message + "<td style='text-align: 'left'; font-weight: " + weights[i][j] + ";'>" + values[i][j] + "</td>\n";
} // ends the for loop for each column
message = message + "</tr>\n";
}
message = message + "</table>\n";
MailApp.sendEmail(
{
to: Session.getActiveUser().getEmail(), //this will send the email to whoever is running the script; replace it with the desired email IDs
subject: "Subject Goes Here",
htmlBody: message
}
);
}
Inspired by this article here.
来源:https://stackoverflow.com/questions/58031111/sending-html-newsletter-emails-via-google-spreadsheet