【问题标题】:Adding Spaces to HTML email with apps script using reduce使用 reduce 使用应用脚本将空格添加到 HTML 电子邮件
【发布时间】:2020-07-27 20:53:54
【问题描述】:

我正在使用应用程序脚本。我有一个对象数组“sendableRows”,我想将其转换为 json,在电子邮件正文的每一行之间添加一个空格(我想出了一个
标签,因为正文是 html)。一个对象看起来像:

{Phone Number=14444444444, , Index=4816.0, completed=, Lot Size=0.74, Power or water=, campaign=, absoluteRow=84.0 , ....}

我的代码:

const json = sendableRows.reduce(row => JSON.stringify(row), "")

Logger.log(json);

MailApp.sendEmail({
to: 'xxxx@gmail.com',
subject: todayString,
htmlBody: json
});

不幸的是,'json' 被输出为(抱歉,属性不完全匹配,因为我为对象和输出截断了不同的属性):

 "\"{\\\"Index\\\":4877,\\\"Email\\\":\\\"ccccc@yahoo.com\\\",\\\"Phone Number\\\":\\\"1234567890\\\",Asking\\\":14651.13,\\\"New Asking\\\":\\\"\\\",\\\"Assessed\\\":28890,\\\"campaign\\\":\\\"\\\",\\\"completed\\\":\\\"\\\",\\\"last_contacted\\\":\\\"2020-07-27T16:20:31.898Z\\\",\\\"relativeRow\\\":64,\\\"absoluteRow\\\":67}<br>\"<br>"<br>

我做错了什么?

【问题讨论】:

  • 您期望的输出类似于['json_string_for_one_element' + '&lt;br&gt;' + 'json_string_for_one_element' + '&lt;br&gt;' + ...] ?
  • 反正sendableRows.reduce(row =&gt; JSON.stringify(row), "")的用法是错误的,因为reducer函数的第一个参数是Accumulator,不是当前元素,应该是sendableRows.reduce((pre, row) =&gt; pre +JSON.stringify(row), "")跨度>
  • 这能回答你的问题吗? Unexpected output using reduce to create json
  • 谢谢 - 我用过 var json = sendableRows.reduce((pre, row) => pre +JSON.stringify(row)+"

    ", "") - 仍然习惯了箭头符号,因为应用程序脚本现在支持它。

标签: javascript google-apps-script mapreduce


【解决方案1】:

尝试使用:

MailApp.sendEmail({
to: 'xxxx@gmail.com',
subject: todayString,
htmlBody: JSON.stringify(json, null, 2)
});

【讨论】:

  • 谢谢,我最终使用了 var json = sendableRows.reduce((pre, row) => pre +JSON.stringify(row)+"

    ", "")
  • @user1592380 出于文档目的,如果可以的话,请接受对您有帮助的答案 (✓) - 它可以帮助将来遇到相同问题的其他人也找到解决方案 :)跨度>
  • 我想接受@Sphinx 的回答,但它没有作为答案输入,只是一个评论。
猜你喜欢
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 2018-01-04
相关资源
最近更新 更多