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
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 believe your goal is as follows.
is:unread label:customer1.In this case, how about the following sample script?
Sample script:
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
To
In this case, the unread emails with the label
customer1orcustomer2are retrieved. If you wantcustomer1ANDcustomer2, please modify tois:unread label:customer1 label:customer2.References:
Added 1:
From your following reply,
I understood that your actual expected request was as follows.
customer1tocustomer1/Unread.customer1/Unreadtocustomer1.Sample script:
customer1are moved tocustomer1/unread. And, the read emails ofcustomer1/unreadis moved tocustomer1.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,
In this case, how about the following sample script?
Sample script: