I'm trying to assign auto-created label to Gmail threads. The script is working, but the problem is every labels were assigned to every email threads instead of just one respective thread.
For example, if there were 7 emails retrieved and 7 labels were created, for every of those emails there will be 7 labels assigned. However, the labels should only be assigned to their respective threads.
Here's the script:
//call cell value by column (Sheet: "SupplierNumber")
//label created based on the "Label" column in Sheet: "SupplierNumber"
//Label should be applied to related threads
function callLabelcolumn()
{
var ss = SpreadsheetApp.getActive();
//get sheet: VendorNumber from cell A2 to the rest of column A
var sh = ss.getSheetByName('SupplierNumber');
var rg=sh.getRange('A2:A');
var vA=rg.getValues();
//get sheet3: SearchParameter (cell A2 only)
var sh3 = ss.getSheetByName("SearchParameter")
var rg3=sh3.getRange('A2');
var searchPara =rg3.getValues();
var threads = GmailApp.search(searchPara); //Get gmail threads - within search parameter only
vA.forEach(function(row) {
if(row != "" && row != "#N/A"){
Logger.log('Label: %s', row); //Log script to debug - check get values
var label = GmailApp.createLabel(row);
Logger.log("label: " + label); //Log script to debug - check GmailLabel
}
for (var i = 0; i < threads.length; i++){
var msg = threads[i].getMessages();
for (var j = 0; j < msg.length; j++){
var msgid = msg[j].getId();
var thread = GmailApp.getMessageById(msgid).getThread();
var name = GmailApp.getUserLabelByName(label.getName());
console.log("So far so good. Let's add label");
thread.addLabel(name);
}
}
});
}
This is part of Log during the execution
Labelling Process flow:
Script refers to SearchParameter Sheet to retrieve specific emails
Emails are retrieved into RetrieveEmail Sheet while at the same time SupplierNumber Sheet is also automatically updated based on the list in RetrieveEmail.
[Note: Column A (Label) has the =ArrayFormula() within the google sheet (as seen in the SupplierNumber image) to extract Supplier number from email subject in the RetrieveEmail sheet list - column Label is the referral for .createLabel(row)]
For example: Email subject is "Supplier A (Supplier No.: 12345)", hence output for Label is 12345.
Script will refer to Label column in SupplierNumber sheet to create gmail labels. While at the same time,
var threadswill retrieve emails based on the same search parameter in sheet SearchParameter, which is used to getmsgand thenmsgid. Then each label created should be assigned to each email retrieved. (This is assuming number of emails will equal to number of labels)The desired result should be, each label assigned to its designated email as in this example.
I've tried different loops and changing positions of the loops, but I would get either TypeError or similar things happened where every emails got assigned to every labels created.
So is there a way to have the newly created Gmail labels be assigned to their respective thread accordingly?
var sh3 = ss.getSheetByName("SearchParameter"). But if you could also point out how to check the title/subject when assigning the label, it would be very helpful. Also, for your reference, I have added the process flow with the desired result in the question and some image example. Sorry I couldn't share the full project as it contains some sensitive data. Hope this helps! - Anies