Search code examples
google-apps-scriptgmailgmail-imap

in Gmail how to show the search result of (is:unread label:customer1) in specific Lable


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));
}


Solution

  • 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 or customer2 are retrieved. If you want customer1 AND customer2, please modify to is: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.

    1. You wanted to change the label of the unread emails of customer1 to customer1/Unread.
    2. You wanted to change the label of the read emails of customer1/Unread to customer1.

    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 to customer1/unread. And, the read emails of customer1/unread is moved to customer1.
    • 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));
    }