Reputation: 27
I'm trying to gather all my email threads from All Mail in my Gmail account. The issue is that my program gathers the first 500 and then stops. I do not get an exception error, but it does seem like there may be either a quota or something's wrong with my code.
function getEmails() {
var generalThreads,inboxThreads;
//inboxThreads = GmailApp.getInboxThreads();
generalThreads = GmailApp.search('in:anywhere');
for (var i = 0; i < generalThreads.length; i++) {
for (var j = 0; j < generalThreads[i].getMessages().length; j++) {
var message = generalThreads[i].getMessages()[j],
label = generalThreads[i].getLabels().map(l=>{return l.getName()}).join(','),
ident = message.getId(),
emailfrom = message.getFrom(),
str = emailfrom.split("<").pop();
str = str.replace(">", '');
}
Logger.log(i+" "+str+" "+ label +" "+ ident);
}
}
Upvotes: 0
Views: 44
Reputation: 21910
Instead of using search(query)
, use search(query, start, max)
, which lets you fetch up to 500 results at a time.
See here for the documentation.
For example:
search('in:anywhere', 1, 500)
followed by
search('in:anywhere', 501, 500)
and so on, in a loop, until you get an empty array by checking generalThreads.length
.
At that point you have finished.
500 is the default batch size if you only use search(query)
.
Here is one approach:
var batchStart = 1;
var batchSize = 5; // for my small test
function getAllMailThreads() {
var generalThreads = nextBatch(batchStart);
while ( generalThreads.length > 0 ) {
processBatch(generalThreads);
generalThreads = nextBatch(batchStart += batchSize);
}
}
function nextBatch(batchStart) {
return GmailApp.search('in:anywhere', batchStart, batchSize);
}
function processBatch(generalThreads) {
// your logic for each batch goes here - my
// logic is just to log the thread IDs:
generalThreads.forEach((generalThread) => {
console.log( generalThread.getId() );
} )
console.log( '----' ); // end of a batch
}
Upvotes: 2