简体   繁体   English

将变量传递给 email 正文中的 href 标记

[英]Pass variable to href tag within body of an email

First time posting, although I have spent many a moon lurking around the site.第一次发帖,虽然我花了很多时间在网站上潜伏。 I was unable to find if anyone had a similar post and thus I am posting to see if someone can answer this question for me.我无法找到是否有人有类似的帖子,因此我发帖看看是否有人可以为我回答这个问题。

I am trying to embed a hyperlink into the body of an email.我正在尝试将超链接嵌入到 email 的正文中。 I am taking data from a specific cell from a sheet我正在从工作表中的特定单元格获取数据

var n = ws.getRange("A2").getDisplayValue();
var data1 = ws.getRange("L2").getDisplayValue();

and want to pass that variable, data1 , to an html template to embed into a specific href tag URL within the template to mail out.并希望将该变量data1传递给 html 模板,以嵌入到模板中的特定 href 标记 URL 以邮寄出去。

var temp= HtmlService.createTemplateFromFile("xyz");

temp.name = n;

var htmlMessage = temp.evaluate().getContent();
  GmailApp.sendEmail(
      Email, 
      Subject,"Your email doesn't support HTML.", 
      {name: "AD", htmlBody: htmlMessage}
    );

Example html file "xyz":示例 html 文件“xyz”:

<p>Hi, <?= name ?>,</p>

<p>Please see attached <a href="data1">link</a></p>

How would I go about passing the variable into the tag?我将如何 go 将变量传递到标签中? Thanks in advance!提前致谢!

I believe your goal as follows.我相信你的目标如下。

  • You want to retrieve the values from Spreadsheet and want to put the retrieved values to the HTML template.您想从电子表格中检索值并将检索到的值放入 HTML 模板。 And, you want to use the HTML data at the HTML body of an email.并且,您想在 email 的 HTML 主体中使用 HTML 数据。
  • In your question, you want to put a value of data1 to data1 of <p>Please see attached <a href="data1">link</a></p> .在您的问题中,您希望将data1的值放入<p>Please see attached <a href="data1">link</a></p>data1中。

I think that in your situation, the scriptlets of <?=... ?> and <?.=..? ?>我认为在您的情况下, <?=... ?><?.=..? ?>的小脚本<?.=..? ?> can be used. <?.=..? ?>可以使用。 When this is reflected to your script, it becomes as follows.当这反映到您的脚本中时,它变成如下。

HTML side: xyz.html HTML侧: xyz.html

<p>Hi, <?= name ?>,</p>

<p>Please see attached <a href="<?= data1 ?>">link</a></p>
  • In this case, <?= data1?> or <??= data1 ?> can be used.在这种情况下,可以使用<?= data1?><??= data1 ?>

Google Apps Script side: Code.gs Google Apps 脚本端: Code.gs

var n = ws.getRange("A2").getDisplayValue();
var data1 = ws.getRange("L2").getDisplayValue();

var temp= HtmlService.createTemplateFromFile("xyz");
temp.name = n;
temp.data1 = data1;
var htmlMessage = temp.evaluate().getContent();

GmailApp.sendEmail(
  Email, 
  Subject,"Your email doesn't support HTML.",
  {name: "AD", htmlBody: htmlMessage}
);

References:参考:

Simply add a link in href="" like below只需在href=""中添加一个链接,如下所示

<p>Please see attached <a id="dataLink1" href="#">link</a></p>
<script>
var data1 = ws.getRange("L2").getDisplayValue();
document.getElementById("dataLink1").href=data1; 
</script>

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

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