GoogleScript Mail Parser - 仅检索过去X天的电子邮件

时间:2017-01-05 12:43:28

标签: google-apps-script gmail gmail-api

构建了一个基本的电子邮件解析器,以便我可以在gSheet中获取我的电子邮件,但是,我有> 50k电子邮件,我只需要在过去7天内检索它们,每次我运行解析器。环顾四周,但无法找到任何类似的问题

这是我的脚本

var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

function getEmails() {
  var label = GmailApp.getUserLabelByName("HOUSE_PHONELEAD");
  var inc = 100;
  var start = 0;
  var row = 2;
  var threads = label.getThreads();
  for (var i = 0; i < threads.length; i++) {
    var messages=threads[i].getMessages();
    for (var m=0; m < messages.length; m++) {   
      sheet.getRange(row,1).setValue(messages[m].getPlainBody());
      row++;
      Utilities.sleep(1000);
    }
  }
}

function onOpen() {
  var menuEntries = [ {name: "Load Emails", functionName: "getEmails"} ];
  spreadsheet.addMenu("Email", menuEntries);
}

1 个答案:

答案 0 :(得分:1)

尝试使用

  

var threads = GmailApp.search('newer_than:7d');

而不是

  

var threads = label.getThreads();

搜索方法允许传递字符串查询: https://developers.google.com/apps-script/reference/gmail/gmail-app#searchquery

查询字符串必须是特定于gmail的运算符:

https://support.google.com/vault/answer/2474474?hl=en