I am new to google script.
I have been trying to auto sublabel the emails I received from suppliers based on certain ID or numbers extracted within the email subjects.
For example, email subject is "Supplier A (Supplier ID: 12349)". So under the parent label "Supplier", I am trying to get 12349 as the sublabel for the thread or messages referring to that specific ID. It should be automated where the sublabel is created as soon as the Gmail received a new ID within the subject and will automatically label the new incoming emails with already created sublabel IDs.
I have tried many things by referring to other forum threads but none of them is working. The closest I think I could get was by having the script to refer to a certain column in sheets that I use to extract the ID numbers and create labels based on that particular row. However, I kept getting errors because GmailApp.createLabel("") requires name/string in the ("").
Below is the current script so far:
//retrieve gmail messages
function GatherEmails() {
let messages = getGmail();
let curSheet = SpreadsheetApp.getActive().getSheetByName('SupplierEmails');
messages.forEach(message => {curSheet.appendRow(parseEmail(message))});
}
function getGmail() {
const query = "NOT label:Bounced/Out of Office";
let threads = GmailApp.search(query);
let label = GmailApp.getUserLabelByName("Supplier");
let messages = [];
threads.forEach(thread => {
messages.push(thread.getMessages()[0].getPlainBody());
});
return messages;
}
//call cell value by column (Sheet: "Supplier Labels")
function callLabelcolum()
{
var ss1=SpreadsheetApp.getActive();
var sh=ss1.getSheetByName("Supplier Labels");
var rg=sh.getDataRange();
var vA=rg.getValues();
var hA=vA[0];
for(var i=1;i<vA.length;i++)
{
var row="";
for(var j=0;j<vA[0].length;j++)
{
row[hA[j]]=vA[i][j];
}
Logger.log('Label: %s',row.Label);
}
}
//parse & label the email massage
function parseEmail(messages, row) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("SupplierEmails");
var threads = GmailApp.search('');
for (var i = 0; i < threads.length; i++){
var msg = threads[i].getMessages();
for (var j = 0; j < msg.length; j++){
// Get same thread by its ID.
var threadID = GmailApp.getThreadById(msg[j].getId());
// Logs the permalink for the first thread in the inbox
var thrd = GmailApp.getInboxThreads(0,1)[0];
var link = thrd.getPermalink();
var date = msg[j].getDate();
var from = msg[j].getFrom();
var subj = msg[j].getSubject();
var pmsg = msg[j].getPlainBody();
var msgid = msg[j].getId();
var msglink = "https://mail.google.com/mail/u/0/#inbox/"+msg[j].getId();
// Creates the label and logs label: callLabel()
Logger.log("label: " + GmailApp.createLabel(row));
sheet.appendRow([from, msgid, subj, msglink]);
}
}
}
This is the method I used to create the labels but has error:
Logger.log("label: " + GmailApp.createLabel(row));
Error: Exception: Invalid argument: name + no label created in gmail after running
Exception: Invalid argument: name (Message Details)
So is there anyway I could get this right? Or are there any other methods I may have missed?
Would truly appreciate if someone could help me on this.

Logger.log("label: " + GmailApp.createLabel(row));should maybe be thisLogger.log("label: " + GmailApp.createLabel(row)).getName();- Cooper