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

Google Script技术求助:如何获取包含所有未读邮件ID的单个数组

解决未读邮件ID数组问题

你的代码本身已经能生成包含所有未读邮件ID的单个数组,之所以会看到每次push操作生成元素递增的数组,是因为你在forEach循环内部每次执行push后就打印了数组,日志会依次输出添加了1个、2个……元素的数组状态,但实际emailIdArr在循环结束后就是包含所有未读ID的完整数组。

如果你想直接得到最终的完整数组,只需把日志打印语句移到循环外面:

function pushID()  {
  const threads = GmailApp.getInboxThreads();
  const emailIdArr = [];
  
  threads.forEach((thread)=>{      
    if(thread.isUnread() === true){
      emailIdArr.push(thread.getId())
    }
  })
  // 循环结束后打印完整数组
  Logger.log(emailIdArr)
}

另外,还可以用更简洁的filter+map写法直接生成目标数组,避免手动循环push:

function pushID()  {
  const emailIdArr = GmailApp.getInboxThreads()
    .filter(thread => thread.isUnread())
    .map(thread => thread.getId());
  
  Logger.log(emailIdArr)
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 03:47:22