I'm close to getting this to what I want to do, but having a couple issues.
- 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?
- 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]);
}
}
threads[i].addLabel(Nlabel);is not within the if scope - Cooper