0

I am using a script I found online to extract emails by label in my inbox (gmail). There are about 1300 emails labeled "COMPLETED." It works as intended but it always stops after it reaches 500 rows, or it has extracted 500 labeled emails from my gmail. I am using the business version of gmail so I don't think there should be a size limit but possibly there is and i haven't found any details.

I have posted the code below.

​​​function onOpen() {
  var ui = SpreadsheetApp.getUi();
  var menu = ui.createMenu('Emails')
  var item = menu.addItem('Extract Emails','myFunction')
  var item = menu.addItem('Delete Emails','clearRange')

      .addToUi();
}


function myFunction() {
  // Use sheet
  var ss = SpreadsheetApp.getActiveSheet();
  // Gmail query
  var query = "label:COMPLETED";
  // Search in Gmail, bind to array
  var threads = GmailApp.search(query);
  // Loop through query results
  for (var i = 0; i < threads.length; i++)
  {
    // Get messages in thread, add to array
    var messages = threads[i].getMessages();

    // Used to find max index in array
    var max = messages[0];
    var maxIndex = 0;

    // Loop through array to find maxIndexD = most recent mail
    for (var j = 0; j < messages.length; j++) {
      if (messages[j] > max) {
        maxIndex = j;
        max = messages[j];
      }
    } 
    // Find data
    var mId = messages[maxIndex].getId() // ID used to create mail link
    var from = messages[maxIndex].getFrom();
    var cc = messages[maxIndex].getCc();
    var time = messages[maxIndex].getDate()
    var sub = messages[maxIndex].getSubject();

    // Write data to sheet
    ss.appendRow([from, cc, time, sub, 'https://mail.google.com/mail/u/0/#inbox/'+mId])
  }
}​

Any help would be greatly appreciated. Thank you. Here is a copy of my sheet and how its set up. https://docs.google.com/spreadsheets/d/1GWgAAU---DMOaIRiOeTktbQTOgIvvK-IdG6szpPkHpA/edit?usp=sharing

  • How about replacing `var threads = GmailApp.search(query);` with `var threads=GmailApp.getInboxThreads()` – Cooper Nov 20 '19 at 17:28
  • Just tried your suggestion. But now it only fetches emails from my inbox. Not the Specified folder in my Gmail. – Steven Lipton Nov 20 '19 at 17:48
  • Oh so you did want the label. By the way, there are not folders in your inbox they're labels. And there may be 1300 emails in 500 or so threads. – Cooper Nov 20 '19 at 17:52
  • Apologies, and ill edit my post above to reflect that. Yes I would like to fetch all emails that are labeled "COMPLETED" from my gmail. i have over 1300 individual emails labeled completed. And i only need to retrieve the subject and date of the first message in the email thread – Steven Lipton Nov 20 '19 at 17:55
  • Someone commented earlier about appending new labels but it looks like their comment was deleted? – Steven Lipton Nov 20 '19 at 19:27
  • @StevenLipton I answered the question but then I deleted it because I wasn't sure exactly what you were trying to accomplish. All you want to do is retrieve the first message on an email thread? – CodeCamper Nov 20 '19 at 19:28
  • There is a comment in the developers guide that suggests batching them in groups, or the query can fail. https://developers.google.com/apps-script/reference/gmail/gmail-app.html#search(String) – Scot May Nov 20 '19 at 19:30
  • @CodeCamper Yes. So all I Need is the email address, The subject line, and the Date of the first message. So if its multiple messages in one thread I need the timestamp of the first one. Essentially the date and time of when the first message was received for that thread – Steven Lipton Nov 20 '19 at 19:39
  • @ScotMay I notice the documentation mentions a 'paged' call, but nowhere on the page is there a link to what that is. – CodeCamper Nov 20 '19 at 20:19
  • @StevenLipton so you want to know who sent and received that email, the subject, and time of each thread that is labeled completed? – CodeCamper Nov 20 '19 at 20:19
  • @CodeCamper found this code on another post and made adjustments to call the first message's date/time. And changed the label name to "completed". I'm not sure about anything just to be honest here. I included a copy of my sheet of how i would like the data to appear. Having the CC email is not very important but is nice. – Steven Lipton Nov 20 '19 at 20:41
  • @StevenLipton duplicate question https://stackoverflow.com/questions/55722600/how-to-read-all-mails-from-inbox-in-google-script – CodeCamper Nov 20 '19 at 20:49
  • 1
    Does this answer your question? [How to read all mails from inbox in google script?](https://stackoverflow.com/questions/55722600/how-to-read-all-mails-from-inbox-in-google-script) – CodeCamper Nov 20 '19 at 20:50
  • @CodeCamper I will try this when i get back from lunch. TY – Steven Lipton Nov 20 '19 at 20:54

0 Answers0