0

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]);
        }
    }
}
Rubén
  • 34,714
  • 9
  • 70
  • 166

2 Answers2

1

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

0

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]);
    }
}
AndreyF
  • 1,798
  • 1
  • 14
  • 25
  • Beeing "pinned" the correct name of the label inbox assign to threads, any idea on what could be the correct statement? – Giuseppe Angelici Jun 14 '17 at 17:23
  • You can read the documentation and see an array of GmailLabels is returned. https://developers.google.com/apps-script/reference/gmail/gmail-thread – AndreyF Jun 15 '17 at 06:19
  • too bad it's not working. I think i'll try annother direction. maybe starring every pinned thread is simpler to code – Giuseppe Angelici Jun 16 '17 at 05:17