Sending HTML newsletter emails via google spreadsheet

萝らか妹 提交于 2019-12-10 16:12:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!