You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何迭代JavaScript对象生成HTML用于Appscript发送的邮件内容

解决方案

你当前直接在模板字符串中插入dictionary对象,默认会调用对象的toString()方法输出[object Object],无法展示具体的键值内容。可以按你的需求选择纯文本或HTML格式的实现方案:


方案1:纯文本邮件(无需HTML)

直接遍历对象的键值对,拼接为带换行的纯文本内容即可,修改后的完整代码如下:

let dictionary = {}
const gratitude = dataRange.filter(function(row) {
  return row[0] >= result && row[10] != 'Gratitude' && row[10] != 'Notes' && row[10] != ''
}).map(function(row) {
  return dictionary[row[0]] = row[10]
})

// 遍历字典拼接纯文本内容
const contentList = Object.entries(dictionary)
  .map(([key, value]) => `${key}:${value}`)
  .join('\n')

var message = {
  to: 'person1@gmail.com',
  subject: 'Weekly Summary',
  body: 
`
This week you completed:

${contentList}`,
}
MailApp.sendEmail(message);

方案2:HTML格式邮件(样式更美观)

如果需要更规整的展示效果,可以拼接为HTML字符串,同时配置htmlBody参数实现富文本展示:

let dictionary = {}
const gratitude = dataRange.filter(function(row) {
  return row[0] >= result && row[10] != 'Gratitude' && row[10] != 'Notes' && row[10] != ''
}).map(function(row) {
  return dictionary[row[0]] = row[10]
})

// 遍历字典拼接HTML列表
const htmlList = Object.entries(dictionary)
  .map(([key, value]) => `<li><b>${key}:</b>${value}</li>`)
  .join('')

var message = {
  to: 'person1@gmail.com',
  subject: 'Weekly Summary',
  // 纯文本降级内容,供不支持HTML的客户端展示
  body: '请使用支持HTML格式的邮件客户端查看本次周报',
  htmlBody: 
`
<p>This week you completed:</p>
<ul>
${htmlList}
</ul>
`
}
MailApp.sendEmail(message);

内容的提问来源于stack exchange,提问作者lmcc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 23:57:05