英文:
in Gmail how to show the search result of (is:unread label:customer1) in specific Lable
问题
我已尝试使用筛选器来探索所有可用选项,但没有产生期望的结果。能否使用Google Apps Script完成这个任务?我的尝试使用筛选器是徒劳的,因为无法同时选择多个标签或类别。
更新:
我需要结果消息可见于一个名为Unread的标签中,该标签是Customer1的子标签。
这是@Tanaike的最终结果,非常感谢他。
function customer1_unread_label() {
const customer1_label1 = "customer1"; // This label name is from your reply.
const unreadCustomer1_label2 = "customer1/Unread customer1"; // This label name is from your reply.
const label2 = GmailApp.getUserLabelByName(unreadCustomer1_label2);
const threads1 = GmailApp.search(`is:unread label:${customer1_label1}`);
threads1.forEach(thread => { if (!thread.getLabels().find(l => l.getName() == unreadCustomer1_label2)) { thread.addLabel(label2); } });
const threads2 = GmailApp.search(`is:read label:${unreadCustomer1_label2}`);
threads2.forEach(thread => thread.removeLabel(label2));
}
英文:
I have explored every available option using filters, but none yielded the desired outcome. Is it possible to accomplish this task using Google Apps Script? My attempts with filters were fruitless due to the limitation of not being able to select more than one label or category simultaneously.
Updates:
I need the result messages to be visible in a label called Unread, which is a sub-label of Customer1
This is the final from @Tanaike, thanks a lot for him
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function customer1_unread_label() {
const customer1_label1 = "customer1"; // This label name is from your reply.
const unreadCustomer1_label2 = "customer1/Unread customer1"; // This label name is from your reply.
const label2 = GmailApp.getUserLabelByName(unreadCustomer1_label2);
const threads1 = GmailApp.search(`is:unread label:${customer1_label1}`);
threads1.forEach(thread => { if (!thread.getLabels().find(l => l.getName() == unreadCustomer1_label2)) { thread.addLabel(label2); } });
const threads2 = GmailApp.search(`is:read label:${unreadCustomer1_label2}`);
threads2.forEach(thread => thread.removeLabel(label2));
}
<!-- end snippet -->
答案1
得分: 0
我相信您的目标如下。
- 您想通过搜索
is:unread label:customer1
来检索消息。 - 您想使用 Google Apps Script 来实现这一目标。
在这种情况下,以下示例脚本如何?
示例脚本:
function myFunction() {
const threads = GmailApp.search("is:unread label:customer1");
const res = GmailApp.getMessagesForThreads(threads);
res.forEach(thread => {
thread.forEach(message => {
const title = message.getSubject();
console.log(title);
});
});
}
-
在此示例脚本中,检索到的消息的标题将显示在日志中。
-
当您想要搜索多个标签时,以下修改如何?
-
从
const threads = GmailApp.search("is:unread label:customer1");
-
到
const threads = GmailApp.search("is:unread {label:customer1 label:customer2}");
-
在这种情况下,将检索具有标签
customer1
或customer2
的未读邮件。如果您想要customer1
和customer2
,请修改为is:unread label:customer1 label:customer2
。
-
参考:
添加 1:
根据您的以下回复,
> 我需要完整的代码来从“customer1”标签检索未读消息并在“customer1/未读”标签中显示它们。此外,一旦我阅读了任何消息,它应该自动从“customer1/未读”标签中删除。我认为每分钟触发一次代码将是最合适的方法。
我理解您实际期望的请求如下。
- 您希望将
customer1
的未读邮件的标签更改为customer1/未读
。 - 您希望将
customer1/未读
的已读邮件的标签更改为customer1
。
示例脚本:
function sample2() {
const customer1_label1 = "customer1"; // This label name is from your reply.
const unreadCustomer1_label2 = "customer1/未读"; // This label name is from your reply.
const [label1, label2] = [customer1_label1, unreadCustomer1_label2].map(e => GmailApp.getUserLabelByName(e));
const threads1 = GmailApp.search(`is:unread label:${customer1_label1}`);
threads1.forEach(thread => thread.removeLabel(label1).addLabel(label2));
const threads2 = GmailApp.search(`is:read label:${unreadCustomer1_label2}`);
threads2.forEach(thread => thread.removeLabel(label2).addLabel(label1));
}
- 运行此脚本时,
customer1
的未读邮件将移动到customer1/未读
。而customer1/未读
的已读邮件将移动到customer1
。 - 从
我认为每分钟触发一次代码将是最合适的方法。
,如果您想要使用定时触发器运行此脚本,请将其安装到函数中。
添加 2:
根据您的以下回复,
> 但我需要澄清一点,我不需要从Customer1中删除未读消息,保持它在此标签中显示不变。所以,如果有未读消息,它们应该在Customer1/未读中可见,一旦它变为已读,它们将不再在Customer1/未读中可见,代码不应更改Customer1标签中的任何内容。
在这种情况下,以下示例脚本如何?
示例脚本:
function sample2() {
const customer1_label1 = "customer1"; // This label name is from your reply.
const unreadCustomer1_label2 = "customer1/未读"; // This label name is from your reply.
const label2 = GmailApp.getUserLabelByName(unreadCustomer1_label2);
const threads1 = GmailApp.search(`is:unread label:${customer1_label1}`);
threads1.forEach(thread => thread.addLabel(label2));
const threads2 = GmailApp.search(`is:read label:${unreadCustomer1_label2}`);
threads2.forEach(thread => thread.removeLabel(label2));
}
英文:
I believe your goal is as follows.
- You want to retrieve the messages by searching with
is:unread label:customer1
. - You want to achieve this using Google Apps Script.
In this case, how about the following sample script?
Sample script:
function myFunction() {
const threads = GmailApp.search("is:unread label:customer1");
const res = GmailApp.getMessagesForThreads(threads);
res.forEach(thread => {
thread.forEach(message => {
const title = message.getSubject();
console.log(title);
});
});
}
-
In this sample script, the message titles of the retrieved messages are shown in the log.
-
When you want to search the multiple labels, how about the following modification?
-
From
const threads = GmailApp.search("is:unread label:customer1");
-
To
const threads = GmailApp.search("is:unread {label:customer1 label:customer2}");
-
In this case, the unread emails with the label
customer1
orcustomer2
are retrieved. If you wantcustomer1
ANDcustomer2
, please modify tois:unread label:customer1 label:customer2
.
-
References:
Added 1:
From your following reply,
> I need the entire code to retrieve unread messages from the "customer1" label and display them in the "customer1/Unread" label. Additionally, once I read any message, it should be automatically removed from the "customer1/Unread" label. I believe triggering the code every minute would be the most suitable approach.
I understood that your actual expected request was as follows.
- You wanted to change the label of the unread emails of
customer1
tocustomer1/Unread
. - You wanted to change the label of the read emails of
customer1/Unread
tocustomer1
.
Sample script:
function sample2() {
const customer1_label1 = "customer1"; // This label name is from your reply.
const unreadCustomer1_label2 = "customer1/unread"; // This label name is from your reply.
const [label1, label2] = [customer1_label1, unreadCustomer1_label2].map(e => GmailApp.getUserLabelByName(e));
const threads1 = GmailApp.search(`is:unread label:${customer1_label1}`);
threads1.forEach(thread => thread.removeLabel(label1).addLabel(label2));
const threads2 = GmailApp.search(`is:read label:${unreadCustomer1_label2}`);
threads2.forEach(thread => thread.removeLabel(label2).addLabel(label1));
}
- When this script is run, the unread emails of
customer1
are moved tocustomer1/unread
. And, the read emails ofcustomer1/unread
is moved tocustomer1
. - From
I believe triggering the code every minute would be the most suitable approach.
, if you want to run this script with the time-driven trigger, please install it to the function.
Added 2:
From your following reply,
> but I need to clarify one point, I don't need to remove the unread messages from Customer1, keep it visible as it is in this label. so, if there are unread messages, they should be visible in the Customer1/Unread and once it is became read they will not be visible in Customer1/Unread, the code should not change anything in the customer1 label
In this case, how about the following sample script?
Sample script:
function sample2() {
const customer1_label1 = "customer1"; // This label name is from your reply.
const unreadCustomer1_label2 = "customer1/unread"; // This label name is from your reply.
const label2 = GmailApp.getUserLabelByName(unreadCustomer1_label2);
const threads1 = GmailApp.search(`is:unread label:${customer1_label1}`);
threads1.forEach(thread => thread.addLabel(label2));
const threads2 = GmailApp.search(`is:read label:${unreadCustomer1_label2}`);
threads2.forEach(thread => thread.removeLabel(label2));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论