Giuseppe Angelici
Giuseppe Angelici

Reputation: 11

gmail app script cleaninbox modding

Sorry, i'm not a programmer,

I'm using this gmail app script (CLEANINBOX) and it works great in roder to keep my inbox clean. As I need to use INBOX app (mobile) and GMAIL app (desktop) at the same time I need to implement this script in order to avoid that PINNED messages in INBOX get archieved.

I inserted a condition in the IF sequence and it does not work After struggling a bit I realized (correct me if I'm wrong) that the following code is not working becouse !thread.getLabels()=='PINNED') IS NOT BOOLEAN

Can anybody point me to the correct code?

function cleanInbox() {
    var threads = GmailApp.getInboxThreads();
    for (var i = 0; i < threads.length; i++) {
        var thread=threads[i];
        if (!thread.hasStarredMessages() && !thread.isUnread() && !thread.getLabels()=='PINNED') {
            GmailApp.moveThreadToArchive(threads[i]);
        }
    }
}

Upvotes: 0

Views: 144

Answers (2)

Giuseppe Angelici
Giuseppe Angelici

Reputation: 11

Ok it was much easyer then expected...

I just needed to narrow down the number of threads to work with, and i did it just excluding those with "pinned" label

var threads = GmailApp.search('in:inbox -label:pinned')

solved

thanks for input

Upvotes: 1

AndreyF
AndreyF

Reputation: 1838

The statement !thread.getLabels()=='PINNED' is boolean (as the operand == outputs true or false). I think your problem is that thread.getLabels() does not return a string and therefore it will never be equal to 'PINNED'

EDIT:

The getLabels() method return value is GmailLabel[]. You can iterate over the labels and check your condition by adding this code:

for (var i = 0; i < threads.length; i++) {
var thread=threads[i];
if (!thread.hasStarredMessages() && !thread.isUnread()) {
    var labels = thread.getLabels();
    var isPinned = false;        
    for (var j = 0; i < labels.length; i++) {
       if (labels[j].getName() == "PINNED"){
           isPinned = true;
           break;
       }      
    if (!isPinned){
        GmailApp.moveThreadToArchive(threads[i]);
    }
}

Upvotes: 0

Related Questions