0
votes

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)

enter image description here

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.

1
How is parseLabel() called. You haven't provided any examples. Also I think this Logger.log("label: " + GmailApp.createLabel(row)); should maybe be this Logger.log("label: " + GmailApp.createLabel(row)).getName(); - Cooper
Can you define the errors you're getting? - Rafa Guillermo
@Cooper parseLabel()? You mean parseEmail()? If that is so, I've edited the top half of the script to show the current script I did. I've also tried the method you mentioned, the same error occurs when I tried to gather the emails on google sheet (Exception: Invalid argument: name) - Anies
@RafaGuillermo the error I'm getting is "Exception: Invalid argument: name". It appears on the top of the page when I tried to gather the emails into the spreadsheet. Although, no error was detected when I run the script in the script editor.. - Anies
@Cooper Sorry, I don't quite understand your question? But if you need me to be more specific about the error.. I've added a picture of the error in my question - Anies

1 Answers

1
votes

Here's what I did after discovering the problem with the method.

Just to clarify, the labels created are only empty labels based on the cell value, so no threads or filter assigned. However, it did solve the error I'm getting.

function callLabelcolumn() 
{
  var ss = SpreadsheetApp.getActive();
  
  var sh = ss.getSheetByName('Supplier Labels');
  var rg=sh.getRange('A2:A');
  var vA=rg.getValues();

  
  vA.forEach(function(row) {
  if(row != "" && row != "#N/A"){
   
   Logger.log('Label: %s', row);
    var label = GmailApp.createLabel(row);
    
   Logger.log("label: " + label);
             
   }