How to use Google App Scripts to retrieve Gmail emails in a customised way?
google-apps-script, javascript
Solution
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);
}
Problem
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 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 ? 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 ?