我正在撰寫一個電子郵件功能,該功能應該在單擊按鈕后從電子表格中的所有范圍資料發送電子郵件,問題是我無法收到一封包含所有資料的電子郵件,而是通過回圈單獨發送每封電子郵件,有沒有一種方法可以將所有資料作為 HTML 資料發送到一封電子郵件中?當我放置 var htmlMessage=emailTemp.evaluate().getContent(); 在它觸發的回圈之外 BarcodeT 未定義。
function email(){
var Barcode=0;
var Name=1;
var Added=2;
var Price=3;
var emailTemp=HtmlService.createTemplateFromFile("Email");
var ws=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Refill Request");
var data = ws.getRange("A3:D" ws.getLastRow()).getValues();
let BarcodeT="";
let NameT="";
let AddedT="";
let PriceT="";
data.forEach(function(row){
BarcodeT =emailTemp.ItemCode=row[Barcode];
NameT =emailTemp.ItemName=row[Name];
AddedT =emailTemp.QuantityNeeded=row[Added];
var htmlMessage=emailTemp.evaluate().getContent();
})
var address = "[email protected]";
MailApp.sendEmail(address,"Refill Request","Your email doesn't support HTML.",
{name: "Email App", htmlBody: htmlMessage})
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<table>
<tr>
<th>Item Code</th>
<th>Item Name</th>
<th>Quantity Needed</th>
</tr>
<tr>
<td><?=BarcodeT ?></td>
<td><?=ItemName ?></td>
<td><?=QuantityNeeded ?></td>
</tr>
</table>
</body>
</html>
uj5u.com熱心網友回復:
在您的腳本中,如何進行以下修改?
修改后的腳本:
function email() {
var Barcode = 0;
var Name = 1;
var Added = 2;
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Refill Request");
var data = ws.getRange("A3:D" ws.getLastRow()).getValues();
var htmlMessage = "<table><tr><th>Item Code</th><th>Item Name</th><th>Quantity Needed</th></tr>";
data.forEach(function (row) {
htmlMessage = `<tr><td>${row[Barcode]}</td><td>${row[Name]}</td><td>${row[Added]}</td></tr>`;
});
htmlMessage = "</table>";
var address = "[email protected]";
MailApp.sendEmail(address, "Refill Request", "Your email doesn't support HTML.", { name: "Email App", htmlBody: htmlMessage });
}
- 在此修改中,HTML 表直接在 Google Apps Script 中創建,并用作 HTML 正文。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/439999.html
