Inconsistencies between app scripts GmailApp.search and search in gmail interface

I am trying to create a google script application to import mail received from an online form into a spreadsheet.

I am using two tags: one "to_process" is added by the gmail filter and the other "processed" is added by this script after the email has been added to the sheet.
I am looking for all emails that have "to_process" but are not "processed" using the search label: label: to_process! Label: processed in: all '

I got it partially (see the core script below)
I run the script using the script editor's run function.

The problem is that using the same request in the gmail frontend I get over 100 emails, but in the script log I get 6 and they are all processed. Did I miss something?

function extractInfo() {
  var step = 30;
  var max = 500;
  var currentStep = 0;
  while(max--) {
    var threads = GmailApp.search('label:to_process !label:processed in:all', currentStep++ * step, step);
    if(threads.length == 0) break;
    Logger.log("-------- found threads: " + threads.length);
    var threadId = threads.length;

    while(threadId--) {
      var thread = threads[threadId];
      thread.refresh();
      if(hasLabel(thread, "processed")) {
        Logger.log("was processed: " + thread.getPermalink())
        continue;
      }
      if(!hasLabel(thread, "to_process")) {
        Logger.log("isn't mark to process: " + thread.getPermalink())
        continue;
      }
      var messages = thread.getMessages(); 
      var messageId = messages.length;
      while(messageId--) {
        var message = messages[messageId];
        var row = extractMessageData(message);
        sheet.appendRow(row);
        GmailApp.markMessageRead(message);
      }
      threads[threadId].addLabel(processedLabel);
    }
  }
}

function hasLabel(thread, name) {
  var labels = thread.getLabels();
  var l = labels.length;
  while(l--) {
    if(labels[l].getName() == name) {
      return true;
    }
  }
  return false;
}

      

+3


source to share


1 answer


After many mistakes and mistakes, I partially figured it out.

What I see in the gmail ui is actually a message, not streams. But manipulating labels is one thing. I just pulled all messages of all threads from a given label and then processed the theses.



If a stream is labeled "processed" it does not mean that all messages have been processed, which is a problem.

There are still inconsistencies regarding the number of messages I see in the UI and what I use the API.

0


source







All Articles