如何将变量传递到HTML正文中 [英] How to pass variables into an HTML body

查看:61
本文介绍了如何将变量传递到HTML正文中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了此脚本,该脚本使我可以选择某种食品和食用份量,然后计算营养价值,将其添加到数据库中,并在一天结束时将所有内容相加,然后向我发送一封电子邮件,其中包含概述.几乎所有方法都能正常工作,我只是很难将脚本中的某些变量传递到HTML格式的电子邮件中.

I have created this script that allows me to select a certain food item and the serving size and then it calculates nutritional value, adds it to a database, sums it all at the end of the day and sends me an email with an overview. It almost all works, I am just having trouble passing some variables from my script into the HTML formatted email.

在下面的代码部分中,totals是电子表格范围中的数据值.我希望检索到的caloriescaloriesFromFatpolyFat的值替换电子邮件htmlBody中的匹配文本.

In the portion of the code below, totals is the data values from a spreadsheet range. I expect the retrieved values of calories, caloriesFromFat, and polyFat to replace the matching text in the email's htmlBody.

 var calories = totals[0][25];
    var caloriesFromFat = totals[0][26];
    var polyFat = totals[0][27];
  }
//Sends email with summary
    MailApp.sendEmail({
     to: "user@example.com",
     subject: "Daily Intake Log",
     htmlBody:  <table style="width:100%">
  <tr>
    <td>Calories</td>
    <td>calories</td>       

  </tr>

  <tr>
    <td>Calories From Fat</td>
    <td>caloriesFromFat</td>        

  </tr>

  <tr>
    <td>PolyUnsaturated</td>
    <td>polyFat</td>        

  </tr>

</table>

   });

当我将变量caloriescaloriesFromFatpolyFat的内容输出到日志时,它们都包含应有的数字.但是当我收到电子邮件时,它不会插入变量的内容,而只留下变量的名称,例如calories.

When I output the contents of the variables calories, caloriesFromFat, and polyFat to the log they all contain the numbers they should. But when I get the email it doesn't insert the variables' contents, just leaves the name of the variables, e.g. calories.

如何将计算所得的值发送到我的电子邮件中?

How do I get the computed values into my email?

推荐答案

HTML必须是字符串(文本),并且您需要使用文本公式来构建HTML. JavaScript中的文本字符串被组合在一起(并串在一起),并带有加号:

The HTML must be a string (text), and you need to build the HTML with a text formula. Text strings in JavaScript are put together, (concatenated) with a plus sign:

var tableHTML = '<table style="width:100%">' + //Note single quotes on ends
   "<tr><td>Calories</td><td>" +
   calories + 
   "</td></tr>" +
   "<tr><td>Calories From Fat</td><td>" + 
   caloriesFromFat + 
   "</td></tr>" +
   "<tr><td>PolyUnsaturated</td><td>" + 
   polyFat + 
  "</td></tr>" +
   "</table>";

如果HTML属性中使用双引号,则在字符串的末尾使用单引号. 然后只需使用电子邮件HTML的变量名即可:

If double quotes are used in the HTML attribute, then use single quotes on the ends of the string. Then just use the variable name for the email HTML:

htmlBody: tableHTML

这篇关于如何将变量传递到HTML正文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆