【发布时间】:2021-03-04 15:29:47
【问题描述】:
我正在尝试制作一个脚本,通过电子邮件发送来自 Google 表格的表格。我将内容传递给 HTML 文件,然后毫无问题地从 HTML 文件中取回它。 (HTML 表格代码显示在控制台日志中。)MailApp 似乎运行良好。当我转到我的电子邮件时,一切看起来都正确(甚至是预览),但电子邮件本身没有显示任何内容......
有人可以向我解释发生了什么吗?
这是我的代码。原谅我的笔记,它们是为那些比我知道得更少的人准备的。
console.log(""); // The console log runs parallel to execution, for debugging. This is just a spacer, it makes the console log easier to read.
console.log("");
console.log("// BEGIN EXECUTION //"); // This is also a console log, it tells me that the script started running.
var ss=SpreadsheetApp.getActive(); // This script is "bound" to the spreadsheet, but we still need to tell the script what spreadsheet we're talking about
var sheet=ss.getSheetByName("Automated WeeklyScheduleToVolunteers"); // This directs the script to the right "tab" in the spreadsheet. This also means if you rename the tab, the script stops working.
console.log("Spreadsheet identified"); // See above, I'll stop commenting on the "just a notification"-type logs.
var target_date = sheet.getRange("G2").getDisplayValue(); // This gets the G2 value, which the spreadsheet produces for the next service.
var lr = sheet.getLastRow(); // This finds the last row of the sheet with data in it.
var tableRangeValues = sheet.getRange(5,4,lr - 4,4).getDisplayValues(); // This gets the values from D5 to the last row (minus 4, so we skip the header), and four columns (the whole table).
var htmlTemplate = HtmlService.createTemplateFromFile("Email"); // This tells the script we're going to use the file Email.html (which calls variables from this script)
htmlTemplate.tableRangeValues = tableRangeValues; // This "passes" the values from the script to the HTML template.
console.log("Content passed to HTML");
var htmlForEmail = htmlTemplate.evaluate().getContent(); // This "passes" the values back from the HTML template.
console.log("Content passed back from HTML");
console.log("htmlForEmail = " + htmlForEmail); // This logs the content of the htmlForEmail variable.
// NOTE TO ADAM: MAKE THIS SECTION DYNAMIC
var volunteer_emails=sheet.getRange(2,2,44,1).getDisplayValues(); // this gets the values from B2:B45 (the email addresses)
console.log("Emails fetched");
console.log("volunteer_emails are " + volunteer_emails);
var sent_value = sheet.getRange("G4").getDisplayValue(); // This sees whether this week's email has been sent or not
/*
This next section is a little intimidating. But, it's just using a IF-THEN-ELSE logic. If something is true, do X action. If it's not true (the "else") do Y action.
*/
if (sent_value === "Email_Sent"){ // If the email HAS ALREADY been sent...
var ui = SpreadsheetApp.getUi(); // (Open the user interface)
var result = ui.alert( // ... send an alert,
"ERROR: Weekly Schedule for " + target_date + " is already marked 'Sent.'", // ... with this text,
"Are you sure you would like to send it again?",
ui.ButtonSet.YES_NO); // ... and a "Yes" "No" option.
if (result == ui.Button.YES) { // If the user clicks "Yes"...
MailApp.sendEmail({to: volunteer_emails.toString(),subject: target_date + ' - CPP Team Schedule',htmlBody: htmlForEmail}); //... send the email and
});
ui.alert("SUCCESS: Weekly Schedule for " + target_date + " has been sent."); // ... send a confirmation alert.
} else { // If the user clicks "No"...
ui.alert('NOTE: No reminder was sent.'); // ... send a cancellation alert.
} // This closes the if-else statement
} else { // If the email HAS NOT YET been sent...
MailApp.sendEmail({to: volunteer_emails.toString(),subject: target_date + ' - CPP Team Schedule',htmlBody: htmlForEmail}); //... send the email and
var ui = SpreadsheetApp.getUi(); // (Open the user interface)
ui.alert("SUCCESS: Weekly Schedule for " + target_date + " has been sent."); // ... give a notification to the user, and
var rng = SpreadsheetApp.getActive().getSheetByName("Sunday Team Schedule Current").getRange("A3:4"); // (This gets the first two lines for the Sunday Team Schedule Current.)
var vs = rng.getDisplayValues(); // (This gets the values from the range.)
for (var j = 0; j < rng.getLastColumn(); j++) { // (This searches the range for the target date)
if (vs[0][j] == target_date) { // ... if a column = target_date, then...
SpreadsheetApp.getActive().getSheetByName("Sunday Team Schedule Current").getRange(4,1+j).setValue("Email_Sent"); // ... put "Email_Sent" in that column, "correcting" for the zero index.
} // This closes the if.
} // This closes the for.
} // This closes the if (email sent)-else.
} // This closes the function.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div>
<div style="height:4px">
<table>
<tbody>
<? tableRangeValues.forEach(r => { ?>
<tr>
<td><?= r[0] ?></td><td><?= r[1] ?></td><td><?= r[2] ?></td><td><?= r[3] ?></td>
</tr>
<? }) ?>
</tbody>
</table>
</div>
</div>
</body>
</html>
【问题讨论】:
-
好的,我已经了解了有关此问题的更多信息。我的手机毫不费力地显示同样的信息。但备用电子邮件帐户和备用浏览器仍然不显示该消息。也许我分类错误,它是桌面 Gmail 的原生内容?
标签: html google-apps-script google-sheets gmail