This Google Script goes through and finds the emails in my Gmail which are in a label called "LabelToGet" and sends me a link to the spreadsheet created with these emails:

function createAndSendDocument() {
  // Create a new document with the title 'Email Contacts'
  var doc = SpreadsheetApp.create('My Contacts');

  var sh = doc.insertSheet('Emails', 0);

   // get all messages
   var eMails = GmailApp.getMessagesForThreads(
     .reduce(function(a, b) {return a.concat(b);})
     .map(function(eMails) {
        return eMails.getFrom() 

   // sort and filter for unique entries  
   var aEmails = eMails.sort().filter(function(el,j,a){if(j==a.indexOf(el))return 1;return 0});  

   // create 2D-array
   var aUnique = new Array();  
   for(var k in aEmails) {

   // add data to sheet
   sh.getRange(1, 1, aUnique.length, 1).setValues(aUnique);

  // Get the URL of the document
  var url = doc.getUrl();

  // Get the email address of the active user - that's you
  var emailAddress = Session.getActiveUser().getEmail();

  // Send yourself an email with a link to the document
                     'Heres the link to it',
                     'click here: ' + url);

It works well, but I have thousands of emails in this label, and the script only seems to be running through the most recent 500.

Does anybody know what might be causing this not to look through ALL emails in that label or what I can do to make that happen?


Gmail.search would only return the first few hundred emails and if you would like to retrieve the later message, you may have to specify the offset parameter.

I previously wrote a Google Script for extracting email addresses from a Gmail folder and it does work with the previous messages as well.

  • I found your script and this is what I am now using! What a great script! Thank you! It does seem to hang every now and again, getting stuck on one email and rewriting that same email to the spreadsheet repeatedly. I think it possibly has to do with reaching a Google Script daily limit on writing or pulling data. Thanks so much for putting this script out there! I modified it a little to also pull the Subject line and the Date of each email into the spreadsheet. =) – ObjectiveFlash Jun 24 '13 at 18:22
  • I added in another Utilities.sleep(500) before it pulls the next email and the script has now been running for 2 hours (and still going!) and has fetched email-address/subject/date for 1280 emails and still going! Accepting your answer now. – ObjectiveFlash Jun 25 '13 at 3:37
  • Still... sometimes it works for 700 - 3,000 emails at a time and then start pulling the same email over and over and over again. And sometimes it just pulls the same email over and over again from the very start and that's the only email I get. =/ – ObjectiveFlash Jun 26 '13 at 3:12
  • You can leave the script running in the background. It is trigger based and it will only work on messages that have not been processed. – Amit Agarwal Jun 26 '13 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.