gmail - Check if a message has a label using Google Script - Stack Overflow

At work we have a google script code that is run every 4 hours and check everyone's @folders for u

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?

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?

Share Improve this question asked Sep 11, 2014 at 13:45 Josh FradleyJosh Fradley 5621 gold badge10 silver badges26 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 3

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:

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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745549156a4632483.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信