Google Apps 脚本 - 删除标签 IFF Star 已从线程中删除

问题描述 投票:0回答:1

我一直在使用 Google Apps 脚本根据我应用于邮件的星星颜色来应用 Gmail 标签(例如,红星 = 跟进等)

我使用这篇文章来帮助构建脚本,该脚本已成功运行,如下所示。

function myFunction() {
//Gets the label names.
   var rLabel = GmailApp.getUserLabelByName('Follow Up');

//Applies the label 'Follow Up' whenever messages are found with redStar
   var redStar = GmailApp.search('l:^ss_sr');
   redStar.forEach(x => x.addLabel(rLabel).markRead());
}

下一步,我想在操作电子邮件后删除标签,这是通过在函数中添加搜索查询和

forEach
循环来实现的。

function myFunction() {
//Gets the label names.
   var rLabel = GmailApp.getUserLabelByName('Follow Up');

//Applies the label 'Follow Up' whenever messages are found with redStar
   var redStar = GmailApp.search('l:^ss_sr');
   redStar.forEach(x => x.addLabel(rLabel).markRead());
   
//Removes Follow Up Label when redStar is removed
   var labelFollow = GmailApp.search('label:follow-up -l:^ss_sr');
   labelFollow.forEach(x => x.removeLabel(rLabel));
}

据我了解,如果

线程中的任何消息
没有指定的星号,则
-
查询中的“GmailApp.search()”运算符匹配,而不是如果线程中的所有消息没有指定的星号指定的星星。因此,虽然这对于单消息线程来说完美无缺,但一旦对话中存在第二条消息(例如回复),它就会注册其中一条消息没有
red-star
并从该消息中删除标签。整个线程。

我无法执行毯子

is:starred
操作员,因为我可能会切换星星类型(例如,从
Red-Star
Blue-Info
,在这种情况下,我想删除“跟进”标签并应用我的“重要”改为“信息”标签。

是否有一种方法可以仅在线程中的任何消息上没有与指定星号匹配的消息时删除标签? (即,如果所有消息都没有

Red-star
,则删除“跟进”标签)

google-apps-script gmail
1个回答
0
投票

这不是很有效(如果有很多带有该标签的线程,可能会超时),但是您应该能够通过获取带有以下内容的所有线程的列表来减去地生成“无红星”线程的列表后续标签,然后获取该标签中红星线程的搜索结果,并将它们从第一个列表中删除。例如像这样的:

var rLabel = GmailApp.getUserLabelByName('Follow Up');
let followUpThreads = rLabel.getThreads()
// Make a map of {id1:true, id2:true, ...} 
// for all thread id's that are red-starred
let redStarThreadIdsMap = Object.fromEntries(
  GmailApp.search('label:follow-up l:^ss_sr')
    .map(t => [t.getId(), true])
)
followUpThreads.forEach(thread =>
{
  if (!redStarThreadIdsMap[thread.getId()])
  {
    thread.removeLabel(rLabel)
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.