Warm tip: This article is reproduced from serverfault.com, please click

Script to search for multiple unread labels in Gmail threads at once

发布于 2020-11-27 11:40:43

I have the following structure of labels

+------------+------------+---------------+
|   label    | sub-label  | sub-sub-label |
+------------+------------+---------------+
| 01-fruit   |            |               |
|            | 01-apples  |               |
|            |            | green         |
|            |            | red           |
|            | 02-oranges |               |
|            |            | red           |
|            |            | orange        |
| 02-veggies |            |               |
|            | 01-peppers |               |
|            |            | green         |
|            |            | red           |
+------------+------------+---------------+

The script in use is:

function mail2Sheets() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('newRec'); //get the sheet
  var freshLabel = GmailApp.getUserLabelByName("00-fresh"); // in the end, add this label
  const query = "label:unread" + " label:01-fruit"; 
  var foundThreads = GmailApp.search(query);
  var newReceipts = [];
  for (var i = 0; i < foundThreads.length; i++) {

  +++++++ SOME CODE HERE +++++++

    }
  }
  if(!foundThreads.length) return; //  if there are no unread ones, do nothing.
  sheet.getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,2,newReceipts.length,newReceipts[0].length).setValues(newReceipts); //write to sheet
  GmailApp.markThreadsRead(foundThreads); // mark "foundThreads" as read
  freshLabel.addToThreads(foundThreads); // add label "00-fresh" to "foundThreads"
  GmailApp.refreshThreads(foundThreads); // refresh "foundThreads" for changes to show
}

I can successfully search for a single label like: const query = "label:unread" + " label:01-fruit";

Also.
Although I have GmailApp.refreshThreads(foundThreads); the Execution never completes.
Instead it shows Status Running

enter image description here

TO RECAP

How can I make the query search at the same time for multiple labels like
"label:unread" + " label:00-fruit/01-apples/red"
AND
"label:unread" + " label:02-veggies/01-peppers/red"

Also. How can the Status Running issue be fixed?

Questioner
marikamitsos
Viewed
55
Tanaike 2020-11-28 07:36:24
  • " " which is a space is used as AND operator.
  • OR and {} can be used as OR operator.

Using above operators, your goal can be achieved.

  • When you want to search the mails with label:unread and label:00-fruit/01-apples/red, please use the search query as follows.

    label:unread label:00-fruit/01-apples/red

  • When you want to search the mails with label:unread and label:00-fruit/01-apples/red or label:02-veggies/01-peppers/red, please use the search query as follows.

      label:unread (label:00-fruit/01-apples/red OR label:02-veggies/01-peppers/red)
    
  • or

      label:unread {label:00-fruit/01-apples/red label:02-veggies/01-peppers/red}
    

Reference: