首页 > 解决方案 > 是否有逆时针计算未读电子邮件的代码?

问题描述

我正在构建一个附加组件,它可以自动回复第一次向用户发送电子邮件的人,其中包含预设消息和收件箱中当前未读电子邮件的数量。

我打算使用电子表格作为数据库,如何创建一个代码来为每个人反向计算未读电子邮件?

标签: google-apps-scriptgoogle-sheets

解决方案


只需获取所有未读消息并按日期排序

function getUnreadMessages() {
  var oA=['Date','From','Id','PlainBody'];
  const threads=GmailApp.getInboxThreads();
  threads.forEach(function(t,i){
   let messages=threads[i].getMessages()
   messages.forEach(function(m,j){
     if(m.isUnread()) {
       oA.push([m.getDate(),m.getFrom(),m.getId(),m.getPlainBody()]);
     }
   });
  });
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('UnReadMessages');
  sh.clearContents()'
  sh.getRange(1,1,oA.length,oA[0].length).setValues(oA).sort({column:1,ascending:false});
}

推荐阅读