一次在 Gmail 线程中搜索多个未读标签的脚本

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

我有以下标签结构

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

正在使用的脚本是:

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
}

我可以成功搜索单个标签,例如:const query = "label:unread" + " label:01-fruit";

还有。
尽管我有 GmailApp.refreshThreads(foundThreads);,但 Execution 从未完成。
相反,它显示 Status Running

回顾

如何让查询同时搜索多个标签,例如
"label:unread" + " label:00-fruit/01-apples/red"

"label:unread" + " label:02-veggies/01-peppers/red"

还有。 Status Running 问题如何解决?

  • " " 是 space 用作 AND 运算符。
  • OR{} 可以用作 OR 运算符。

使用上面的运算符,你的目标就可以实现了。

  • 当您想搜索label:unreadlabel:00-fruit/01-apples/red的邮件时,请使用如下搜索查询。

    标签:未读label:00-fruit/01-apples/red

  • 当您要搜索label:unreadlabel:00-fruit/01-apples/redlabel:02-veggies/01-peppers/red的邮件时,请使用如下搜索查询。

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

参考: