Google apps script Google脚本-在HTML.index文件中使用动态Javascript变量

Google apps script Google脚本-在HTML.index文件中使用动态Javascript变量,google-apps-script,Google Apps Script,我正在使用Google脚本实现动态sendMail功能。突出显示一行后,相关数据点将填充到电子邮件中并继续发送。这封电子邮件发送是非常基本的(纯文本),但我想用HTML来美化它,看起来更专业。我在其中添加了一个HTML.index文件和相应的HTML代码,我引用了.gs(javascript)文件中的HTML文件,它可以工作并发送电子邮件,但是,我想知道如何将.gs(javascript)文件中的var(变量)直接合并到HTML.index文件中。变量是动态的,并且已经通过在.gs(javasc

我正在使用Google脚本实现动态sendMail功能。突出显示一行后,相关数据点将填充到电子邮件中并继续发送。这封电子邮件发送是非常基本的(纯文本),但我想用HTML来美化它,看起来更专业。我在其中添加了一个HTML.index文件和相应的HTML代码,我引用了.gs(javascript)文件中的HTML文件,它可以工作并发送电子邮件,但是,我想知道如何将.gs(javascript)文件中的var(变量)直接合并到HTML.index文件中。变量是动态的,并且已经通过在.gs(javascript)文件中编码确定。我所寻找的本质上是将这些动态值带到发送的外观更好的html电子邮件中

我在网上搜索了一下,找到了可以使用的

<?!= include('JavaScript'); ?>
index.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">

HTML CODING (not included as its 700+ lines)

Just a sample of my ideal outcome
Confirmation of your appointment at 'message4' (DATE FROM GS 
file) at 'message5' (TIME FROM GS FILE), etc.

MORE HTML CODING

</head>
<body>
</body>
</html>

HTML编码(不包括在700多行中)
只是我理想结果的一个样本
确认您在“message4”的约会(日期来自GS
文件)在“message5”(来自GS文件的时间)等处。
更多HTML编码

最简单的方法是在HTML中添加占位符,然后在发送电子邮件之前替换它们

因此,您的index.html文件需要以下内容,其中包含占位符
{message4}
{message5}

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Confirmation of your appointment at {message4} at {message5}. Again, {message4} at {message5}.
  </body>
</html>
如果执行了
Logger.log(message)
,您的输出将是:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Confirmation of your appointment at Date at Time. Again, Date at Time.
  </body>
</html>

确认您在时间日期的约会。同样,日期在时间。

我强烈建议您使用更具描述性的变量名。例如,不要叫它
message4
,只需叫它
date
appointmentDate

我已经审阅了另一篇文章并添加到.gs
var htmlTemplate=HtmlService.createTemplateFromFile('index')var m2html=message2 htmlTemplate.m2html=message2
中,我还添加到html文件
中,您不确定哪些步骤?请重新措辞你的帖子,使它成为一个实际的问题。您可以使用Google文档中的代码示例作为快速开始,验证它是否有效,然后相应地更改代码。查看您的代码片段,您似乎还没有阅读GAS文档中关于模板化HTML和scriptlet的部分。建议的代码不起作用。它显示一个可填充的文本框,在发送的电子邮件中预先填充“文本”。它不会提取所需的相关数据。我喜欢使用占位符的想法。但是,当我输入上面的代码时,我收到了以下错误:TypeError:在对象HtmlTemplate中找不到函数替换。我也不确定是否使用var message4=“Date”;因为message4是从电子表格中提取的动态数据,所以日期会根据电子表格中选择的行而有所不同。谢谢,这非常有效。我做了一些更改:
var-htmlOutput=HtmlService.createhtmloutput-fromfile('index')//htmlOutput-connections-index.html文件var-msg=htmlOutput.getContent()//从html文件var-message=msg.replace(//{PTname}/g,PTname)//将html文件的占位符替换为上面的变量
function sendConfirmationEmail() {
  var message4 = "Date";
  var message5 = "Time";

  var htmlTemplate = HtmlService.createHtmlOutputFromFile("index") // Generate the HTML
  .getContent(); // Convert it to a string

  var message = htmlTemplate.replace(/{message4}/g, message4) // Use regex with global flag to replace all instances
  .replace(/{message5}/g, message5); // You can chain the .replace() methods

  MailApp.sendEmail("email@example.com", "subject", plainTextBody, {"htmlBody": message});
}
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Confirmation of your appointment at Date at Time. Again, Date at Time.
  </body>
</html>