Check if post has a shortcut using Google Script

At work we have a google script code that runs every 4 hours and checks all @folders for unread mail. If unread mail is found, it is moved to INBOX and tagged @@ UNREAD.

What I need is a way for the script to check if it has an already existing @@ UNREAD tag, in which case it doesn't go to the 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 don't have the @@ UNREAD tag?

+3


source to share


1 answer


Here's 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 a stream to your Inbox, you want to make sure it doesn't have a label. So add another condition to the If check .

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

      

I created a variable: doesThisThreadHaveTheLabel

which will either be true or false. By default, each loop in the loop is set to false.

var doesThisThreadHaveTheLabel = false;

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

      

You can debug the code to test it:

Debug Window



In the above picture, you will see an error icon in the menu. Before clicking on this, first click the dropdown menu to the right of the error and select the name of the function to run. Also, add a breakpoint to your code. In this photo, you will see a red dot in the line numbers in the code editor. That's where the code will stop.

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

.

In this image, the code is paused at line 269. I can step on the next line of code by clicking the icon on . Hover over the icon for contextual help.

Debug The Code

I took a step further down the line and retrieved the label, which was placed in the ThisParticularLabel variable. You can see in the window that it is @Josh .

I went through this code and it ended after the main for loop ran once. I also ran this code myself without debugging it and it worked in:

Execution complete [0.246 seconds total execution time]

You need to debug your code and see what it does on each line and find out what each variable has to mean and how conditional statements work.

+1


source







All Articles