我正在尝试将我的code.gs中的变量(我在单元格A1中的名称)传递给我的Index.html并通过电子邮件发送。
有人可以告诉我我做错了什么或者指出了我正确的方向。
Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function sendEmail() {
var ss = SpreadsheetApp.openById('MY_SPREADSHEET_ID');
var sheet = ss.getSheetByName('Sheet1');
var myName = sheet.getRange(1,1).getValue();
var template = HtmlService.createTemplateFromFile('Index');
var body = template.evaluate().getContent();
// SEND THE EMAIL
MailApp.sendEmail({
subject:"Test Email",
to:"example@domain.com",
htmlBody: body,
});
return myName;
的index.html
<!DOCTYPE html>
<html>
<body>
<p><? var data = sendEmail(); ?><?= data ?></p>
</body>
</html>
答案 0 :(得分:2)
阅读本文pushing variables to templates
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function sendEmail() {
var ss = SpreadsheetApp.openById('MY_SPREADSHEET_ID');
var sheet = ss.getSheetByName('Sheet1');
var myName = sheet.getRange(1,1).getValue();
var template = HtmlService.createTemplateFromFile('Index');
template.data = myName;
var body = template.evaluate().getContent();
// SEND THE EMAIL
MailApp.sendEmail({
subject:"Test Email",
to:"example@domain.com",
htmlBody: body,
});
<强>的index.html 强>
<!DOCTYPE html>
<html>
<body>
<p><strong><?= data ?></strong></p>
</body>
</html>
答案 1 :(得分:0)
当我找到它并且我遇到类似的问题时问题仍然是开放的,所以这是我的解决方案(没有doGet()),基于Wim den Herder和Rubén的有用评论(以及{{中的示例) 3}}):
Code.gs
function sendEmail() {
var recipient = 'test@mail.de';
var subject = 'Test Email';
var message = HtmlService.createTemplateFromFile('Index').evaluate().getContent();;
// SEND THE EMAIL
GmailApp.sendEmail(
recipient,
subject,
message, { // body
htmlBody: message // advanced options
});
}
function getData(){
var ss = SpreadsheetApp.openById('MySpreadSheetId');
var sheet = ss.getSheetByName('Sheet1');
// Data is in cell A1
var myName = sheet.getRange(1,1).getValue();
return myName
}
的index.html
<!DOCTYPE html>
<html>
<body>
<p><strong><? var data = getData() ?><?= data ?></strong></p>
</body>
</html>