在邮件中发送 HTML 内容
在上面的示例中,如果我们要将 HTML 内容作为消息发送到电子邮件中,则通过转到文件 - >新建 - > HTML 文件来创建 HTML 文件
现在你可以看到除 gs 文件之外的 HTML 文件,如下所示:
现在,从上面的示例更新 getMessage()
方法,如下所示:
function getMessage(name, amount) {
var htmlOutput = HtmlService.createHtmlOutputFromFile('Message'); // Message is the name of the HTML file
var message = htmlOutput.getContent()
message = message.replace("%name", name);
message = message.replace("%amount", amount);
return message;
}
对 MailApp api 的调用也需要更改
MailApp.sendEmail(recipientEmail, subject, message, {htmlBody : message});
所以整个代码如下:
function getDataSheet() {
sheet = SpreadsheetApp.getActiveSheet();
startRow = 2; // First row of data to process
numRows = 100; // Number of rows to process
startCol = 1; //First column of data to process
numCols = 15; // Number of columns to process
var dataRange = sheet.getRange(startRow, startCol, numRows, numCols);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
return data;
}
function getMessage(name, amount) {
var htmlOutput = HtmlService.createHtmlOutputFromFile('Message');
var message = htmlOutput.getContent()
message = message.replace("%name", name);
message = message.replace("%amount", amount);
return message;
}
function sendEmail() {
var emailSent = "Yes";
var reimbursed = "Yes";
var emailCol = 5;
var data = getDataSheet();
for (var i = 0; i < data.length; i++) {
var row = data[i];
var isReimbursed = row[3];
var isEmailSent = row[4];
var name = row[0];
var amount = row[2];
if(isReimbursed == reimbursed && isEmailSent != emailSent) {
var subject = "Reimbursement details";
var message = getMessage(name, amount);
var recipientEmail = row[1];
MailApp.sendEmail(recipientEmail, subject, message, {htmlBody : message});
sheet.getRange(startRow + i, emailCol).setValue(emailSent);
}
}
}