Search code examples
google-apps-scriptarraylistgmaillabel

Creating Nested Labels in Gmail using Apps Script


I'm trying to create a set of nested labels from a column of values in a spreadsheet. I've gotten my array situated, and I can create a single Label/sub-label with the following syntax: GmailApp.createLabel("Label/sub-label").

Looking at the last line of code, how can I rewrite this to produce a sub-label for each value in my array under the Parent Label "Clients"? The error I receive currently is: The parameters (String,number[]) don't match the method signature for GmailApp.createLabel.

function clientLabels(){
  /** List of Clients */
    const ss = SpreadsheetApp.openById("1BWutWGYPW9RjewRNdgLdHCn-Rytbyi63xbPF2Hd3tPg")
    const sh = ss.getSheetByName("Company")
    const range = sh.getRange(2,2,sh.getLastRow()-1)
    const values = range.getValues()

  /** Creating Labels */
    GmailApp.createLabel("Clients")
    GmailApp.createLabel("Clients/%s", values))
}

Thank you for your help!


Solution

  • I see that you are trying to enter a number array as a string. In my solution you will break the array in individual values and will execute the function for every individual value.

    /** List of Clients */
      const ss = SpreadsheetApp.openById("1BWutWGYPW9RjewRNdgLdHCn-Rytbyi63xbPF2Hd3tPg")
      const sh = ss.getSheetByName("Company")
      const range = sh.getRange(2,2,sh.getLastRow()-1)
      const values = range.getValues()
    
    /** Creating Labels */
      GmailApp.createLabel("Clients")
    
      values.forEach(function(value) {
        GmailApp.createLabel("Clients/" + value[0].toString());
      });
    

    EDIT: Thanks for the dummy information, it looks like the array that .getValues() returned was a 2D array with one dimension being 1 in length. The code needed little change and now it should not. I changed the way the data is accessed from the array and removed the '%s' you had initially in favor of just concatenating the value.