简体   繁体   English

在 Google Apps 脚本 HTML 变量中使用换行符

[英]Using Line Breaks in Google Apps Script HTML Variables

I'm trying to send an e-mail from Google Apps Script that uses variables.我正在尝试从使用变量的 Google Apps 脚本发送电子邮件。 I have a template HTML set up with a "Header" and a "Message" variable.我有一个模板 HTML 设置了一个“Header”和一个“Message”变量。 Whenever I pass html code to the "message" variable, it doesn't render as HTML but instead renders literally.每当我将 html 代码传递给“消息”变量时,它不会呈现为 HTML 而是按字面意思呈现。 I'd love to know how to get this working.我很想知道如何让它工作。

code.gs代码.gs

function sendEmail() {
var fileNo = '12345';
HtmlService.createTemplateFromFile('emailTemplate');
emailHTML.header = "New File Opened";
emailHTML.message =
    'A new file has been opened. <br> File Information: <br>' +
    'File Number: ' + fileNo + '<br>';
MailApp.sendEmail(me@me.com, 'New File', "No HTML Client", {htmlBody: emailHTML.evaluate().getContent()});
};

emailTemplate.html emailTemplate.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <div style="text-align:center; font-family:Cambria;">
      <h2> <?= header ?> </h2>
      </div>
    <div style="background:#ececec; font-size:130%; color:black; margin-right:auto; margin-left:auto; padding:10px;">
      <?= message ?> <br> <br>
    </div>
  </body> 
</html>

Output Body: Output 正文:

"A new file has been opened. <br> File Information: <br>File Number: 12345<br>"

You are using a printing scriptlet.您正在使用打印脚本。 From the documentation :文档中:

Printing scriptlets, which use the syntax <?=... ?> , output the results of their code into the page using contextual escaping .使用语法<?=... ?>打印 scriptlet, output 使用上下文 escaping将其代码的结果放入页面中。 Contextual escaping means that Apps Script keeps track of the output's context on the page — inside an HTML attribute, inside a client-side script tag, or anywhere else — and automatically adds escape characters to protect against cross-site scripting (XSS) attacks.上下文 escaping 意味着 Apps 脚本会跟踪页面上的输出上下文 - 在 HTML 属性内、客户端脚本标记内或其他任何地方 - 并自动添加转义字符以防止跨站点脚本 (XSS) 攻击。

To make it work, use a force-printing scriptlet <?.=..? ?>要使其工作,请使用强制打印脚本<?.=..? ?> <?.=..? ?> . <?.=..? ?>

As a side note, the email address should in quotes:作为旁注,email 地址应该用引号引起来:

  MailApp.sendEmail('me@me.com', 'New File', 'No HTML Client', { htmlBody: emailHTML.evaluate().getContent() });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM