使用 HtmlService 从电子表格创建电子邮件,但电子邮件为空
Using HtmlService to create email from spreadsheet, but email is empty
我正在尝试使用以下代码从 Google 电子表格生成电子邮件:
var SS = SpreadsheetApp.getActiveSheet();
var UI = SpreadsheetApp.getUi();
function onOpen() {
UI.createMenu('Notifications').addItem('New notification', 'createNotification')
.addToUi();
}
function createNotification() {
var me = Session.getActiveUser();
var output = HtmlService.createTemplateFromFile('email').evaluate();
MailApp.sendEmail({
to: me,
subject: 'Test email',
htmlbody: output.getContent()});
}
function getDate () {
return SS.getRange(SS.getLastRow(), 1).getValue();
}
function getEscape () {
return SS.getRange(SS.getLastRow(), 2).getValue();
}
使用以下 html 文件作为模板:
<div>
<h1>Escape Notification:</h1>
<p>Date: <?= getDate();?></p>
<p>Escape:</p>
<p> <?= getEscape();?></p>
</div>
代码运行,我收到了测试邮件。但是,当我打开邮件时,没有任何内容。
我已经尝试了几种不同的方法。我已经尝试使用 .createHtmlOutputFromFile()
函数以及上述方法将其作为 HtmlOutput
提供。
我担心我遗漏了一些非常基本的东西。我花了几个小时研究 Apps 脚本 API 以及它们的示例和教程。我在这里找到的任何内容都与我的问题完全不符。我注意到的一件事是所有示例都是针对 doPost()
或 doGet()
调用的。这只能通过用户界面或 Web 应用程序完成吗?
预先感谢您的帮助。
变化:
htmlbody: output.getContent()
至:
htmlBody: output.getContent()
使用大写"B"
我正在尝试使用以下代码从 Google 电子表格生成电子邮件:
var SS = SpreadsheetApp.getActiveSheet();
var UI = SpreadsheetApp.getUi();
function onOpen() {
UI.createMenu('Notifications').addItem('New notification', 'createNotification')
.addToUi();
}
function createNotification() {
var me = Session.getActiveUser();
var output = HtmlService.createTemplateFromFile('email').evaluate();
MailApp.sendEmail({
to: me,
subject: 'Test email',
htmlbody: output.getContent()});
}
function getDate () {
return SS.getRange(SS.getLastRow(), 1).getValue();
}
function getEscape () {
return SS.getRange(SS.getLastRow(), 2).getValue();
}
使用以下 html 文件作为模板:
<div>
<h1>Escape Notification:</h1>
<p>Date: <?= getDate();?></p>
<p>Escape:</p>
<p> <?= getEscape();?></p>
</div>
代码运行,我收到了测试邮件。但是,当我打开邮件时,没有任何内容。
我已经尝试了几种不同的方法。我已经尝试使用 .createHtmlOutputFromFile()
函数以及上述方法将其作为 HtmlOutput
提供。
我担心我遗漏了一些非常基本的东西。我花了几个小时研究 Apps 脚本 API 以及它们的示例和教程。我在这里找到的任何内容都与我的问题完全不符。我注意到的一件事是所有示例都是针对 doPost()
或 doGet()
调用的。这只能通过用户界面或 Web 应用程序完成吗?
预先感谢您的帮助。
变化:
htmlbody: output.getContent()
至:
htmlBody: output.getContent()
使用大写"B"