0
votes

I'm close to getting this to what I want to do, but having a couple issues.

  1. My addLabel and removeLabel command is impacting all my labels, not just the ones I'm trying to target by identifying a specific subject line. How can I target this change to be only the subject lines I want?
  2. Somehow my Process1 function is running more times than the number of emails it should be identifying (ie if there are two "Flagged" emails, it will produce three entries in my spreadsheet)

Any help or clean up of this would be greatly appreciated!

Thanks,

function getemails() {

var label = GmailApp.getUserLabelByName("Flagged");
var Nlabel = GmailApp.getUserLabelByName("Logged");
 var threads = label.getThreads();
     for (var i = 0; i < threads.length; i++) { 

 var messages=threads[i].getMessages();  

     for (var j = 0; j < messages.length; j++) {

   var message=messages[j];
   var subject = message.getSubject();
       if(subject=="test4")
         
   threads[i].removeLabel(label);
   threads[i].addLabel(Nlabel);
   
   process1(message);
 }
 }

   function process1(message) {
     
 var body =message.getPlainBody();
 var label = GmailApp.getUserLabelByName("Flagged");
 var id = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
 var ss = SpreadsheetApp.openById([id]);
 var sheet = ss.getActiveSheet();
 sheet.appendRow([body]);

}  
    
  }
1
threads[i].addLabel(Nlabel); is not within the if scope - Cooper
Thank you, and you were spot on. Here is what I did to solve: threads[t].removeLabel(label); threads[t].addLabel(Nlabel); - Jack Edwards

1 Answers

0
votes

As sugested by @Cooper. I don't believe your conditional is enclosing the functions you need it to. Check your line 14:

if (subject == "test4")

You should use curly brackets "{" & "}" after the if conditional, as stated in the documentation I link below to enclose any instructions that you want the program to execute only if the condition is met.


Reference: