admin管理员组

文章数量:1278795

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 ?

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 ?

Share Improve this question edited Jun 6, 2013 at 8:27 j0k 22.8k28 gold badges81 silver badges90 bronze badges asked Dec 12, 2012 at 2:54 Mohamed HeibaMohamed Heiba 1,8335 gold badges38 silver badges67 bronze badges 2
  • 1 method getInboxThreads(start, max) allows you to page through all the threads – DavidF Commented Dec 12, 2012 at 9:21
  • Could you please clarify your answer ? how can I page through all the 4000 threads I have in my gmail ? – Mohamed Heiba Commented Dec 14, 2012 at 14:05
Add a ment  | 

1 Answer 1

Reset to default 10

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);
  } 

本文标签: javascriptHow to use Google App Scripts to retrieve Gmail emails in a customised wayStack Overflow