How to use Google App Scripts to retrieve Gmail em

2019-02-10 23:33发布

Here's a neat javascript code I wrote to get all emails from my Gmail and put the list of sender name in a google spreadsheet.

function doGet()
{
  var myspreadsheet = SpreadsheetApp.openById("0Atg1TJnn4dFdGbjNGSrMGJRdGc");
  var mysheet = myspreadsheet.getSheets()[0];
  var threads = GmailApp.getInboxThreads();
  var messages = GmailApp.getMessagesForThreads(threads); 
  var froms = [];

  for(var i = 0; i < threads.length; i++)
  {
    froms.push([messages[i][0].getFrom(),i]);
  }

  mysheet.getRange(1,1,threads.length,2).setValues(froms);

}

It works great but there're 2 issues

  1. The GetInboxThreads method only gets the first 500 emails whatever you try. The question is does someone know how to get more than 500 ? How about get the last 500 rather than the first 500 emails ?

  2. It's a bit slow, although I put loads of effort to make it efficient, can someone suggest how to retrieve the sender name from the emails and put that list of sender names on a spreadsheet in a quick way ?

1条回答
成全新的幸福
2楼-- · 2019-02-11 00:02

Looks like about 10secs /100 messages this way. Can't think of anything faster in GAS.

function getMail(){
    var inc = 100;
    var start = 0;
    do  {
        var now = new Date();
      var thread = GmailApp.getInboxThreads(start, inc);

      start += inc;
      var messages = GmailApp.getMessagesForThreads(thread);
      Logger.log("# threads"+thread.length+"# of messages" + messages.length+" time :"+(new Date()-now));

      }  while (thread.length == inc);
  } 
查看更多
登录 后发表回答