Search code examples
gmailgoogle-apps-scriptjavascript

Check if a message has a label using Google Script


At work we have a google script code that is run every 4 hours and check everyone's @folders for unread mail. If unread mail is found it is moved to INBOX and tagged with @@UNREAD.

What i need is a way for the script to check if it already has the @@UNREAD tag and in that case not move to inbox.

This is the code

function process_unread() {

    //Define user label
    var label = GmailApp.getUserLabelByName("@Josh");

    //Define unread label
    var unreadlabel = GmailApp.getUserLabelByName("@@UNREAD");

    if (label) {
        var threads = label.getThreads();

        for (var i = 0; i < threads.length; i++) {
            var thread = threads[i];
            if (thread.isUnread()) {
                //Remove label
                thread.addLabel(unreadlabel);
                thread.moveToInbox();
            }
        }

    }
}

How can i only move emails if they do not have the @@UNREAD label?


Solution

  • Here is my attempt:

    function process_unread() {
    
        //Define user label
        var label = GmailApp.getUserLabelByName("@Josh");
    
        //Define unread label
        var unreadlabel = GmailApp.getUserLabelByName("@@UNREAD");
    
        if (label) {
            var threads = label.getThreads();
    
            for (var i = 0; i < threads.length; i++) {
                var thread = threads[i];
                
                var labels = thread.getLabels();
                var doesThisThreadHaveTheLabel = false;
                
                  for (var i = 0; i < labels.length; i++) {
                    var thisParticularLabel = labels[i].getName();
                    Logger.log(labels[i].getName());
                  
                    if (thisParticularLabel === "@@UNREAD") {
                      var doesThisThreadHaveTheLabel = true;
                    };
                }
     
                if (thread.isUnread() && doesThisThreadHaveTheLabel === false) {
                    //Remove label
                    thread.addLabel(unreadlabel);
                    thread.moveToInbox();
                }
            }
    
        }
    }
    

    Before you move the thread to the inbox, you want to make sure it does NOT have the label. So add another condition to the If check.

    if (thread.isUnread() && doesThisThreadHaveTheLabel === false) {
    

    I created a variable: doesThisThreadHaveTheLabel that will either have a true or false value. It's default gets set to false before every for loop.

    var doesThisThreadHaveTheLabel = false;
                
        for (var i = 0; i < labels.length; i++) {
    

    You can debug the code to check it:

    Debug Window

    In the above picture, you see an icon of a bug in the menu. Before you click that, first click the drop down menu just to the right of the bug, and choose the name of the function to run. Also, add a breakpoint to the code. In that picture you'll see a red dot in the line numbers in the code editor. That's where the code will stop.

    I added the label @josh to one of the emails in my account, so the variable label has an object in it. But, I don't have any emails with a label @@UNREAD , so you'll notice that in the list of variables, the variable unreadlabel has a value of null.

    In that picture, the code is suspended on the line, 269. I can step in to the next line of code by clicking the step in icon. Hover over the icon to get a context help to pop up.

    Debug The Code

    I stepped line by line further, and retrieved the label that was put into the variable "ThisParticularLabel". You can see in the window, that it has a value of @Josh.

    I stepped through that code, and it ended after the main for loop had run once. I also ran that code by itself without debugging it, and it ran in:

    Execution succeeded [0.246 seconds total runtime]

    You need to debug your code, and see what it is doing on every line, and know what every single variable has for a value, and how the conditional statements are working.