user2946433
user2946433

Reputation: 125

How to get all emails?

I want to get all emails from my account Google Apps for Work and fill a Google Sheets with the columns ID (column A) and Subject (Column B).

I did it with Google Apps Script, but for limits I can only retrieve 500 registers. I can't get more.

Is possible to get all the emails and put its values in a Google Sheets?

enter image description here

Update:

function getMail(){
var myspreadsheet = SpreadsheetApp.openById('xxxxxxx');
var mysheet = myspreadsheet.getSheets()[0];

var start = 0;
var max = 500;
var count =0;
var row = mysheet.getLastRow()+1
while(count < 7) 
{

var threads = GmailApp.getInboxThreads(start , max);
var messages = GmailApp.getMessagesForThreads(threads); 
var froms = [];
messages.get
  for(var i = 0; i < threads.length; i++)
{
   froms.push([messages[i][0].getId(),messages[i][0].getSubject()]);
}

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

start =  start + 501;
count++;
}
}

Greetings,

Upvotes: 3

Views: 4613

Answers (2)

Jonathan Arias
Jonathan Arias

Reputation: 111

My answer may be a bit late, however someone like me looking for the same topic may be useful... Considering that the getTreads() method, today only allows a maximum size of 500 "Treads", my solution was the following...

const mySheet = SpreadsheetApp.openById("here-your-id-sheet");
var label = GmailApp.getUserLabelByName(<mySheet>);
var Emails = label.getThreads();

while ( Emails.length > 0 ){
       Emails.forEach(cadena =>{
             const counterEmailsTread = cadena.getMessageCount();
             for (i=0; i < counter; i++){
                   const Email = cadena.getMessages()[i];
                   const entryDate = correo.getDate();
                   const emailBody = correo.getPlainBody();
                   const sender= correo.getFrom();
                     ......................................
             }
        mySheet.appendRow([entryDate, sender, emailBody])
        cadena.moveToArchive()
       }
      Emails = label.getThreads();
}

This algorithm retrieved all my emails, in my case it was 975 and it never stopped me until it was finished.

Every time my algorithm gets the information I need and makes it safe, I move that email to the archive.

After it finishes looping through the first 500 emails, I update the total number of emails again to continue or exit the while(){} loop

Upvotes: 1

Andres
Andres

Reputation: 671

Here is a solution that should resolve your use case.

Keep in mind, there are still limitations one cannot exceed, you can read more about it here. For example, getInboxThreads() will fail when the size of all threads is too large for the system to handle. This is most likely what happened in your solution.

Instead, try to use getInboxThreads(start, max) this will retrieves a range of Inbox threads irrespective of labels. So, the code should now look something like this assuming you have 500+ emails. It is not elegant but it works.

function getMail(){
 var myspreadsheet = SpreadsheetApp.openById('xxxxxxxxxxxxxxxxxxxxxx');
 var mysheet = myspreadsheet.getSheets()[0];

 var start = 0;
 var max = 99;
 var count =0;
 var row = mysheet.getLastRow()+1
 while(count < 7) 
 {

   var threads = GmailApp.getInboxThreads(start , max);
   var messages = GmailApp.getMessagesForThreads(threads); 
   var froms = [];
    messages.get
      for(var i = 0; i < threads.length; i++)
    {
       froms.push([messages[i][0].getId(),messages[i][0].getSubject()]);
    }

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

    start =  start + 100;
    count++;
    }
}

Reminder: Script runtime cannot exceed more than six minutes, if so the script will throw an exception.

Hope this helps and Good luck!

Upvotes: 2

Related Questions