1
votes

Hope you guys help me below question :

  1. I have below 2 rows

enter image description here

2. I want to use google app script to pull out the unique values like : HTS, WDS, HH 3. So I write below codes :

  • DATA =SHEET.GETRANGE("J2:O2").GETVALUES();
  • UNIQUELIST=[];
  • DATA.FOREACH(FUNCTION(X){ IF(UNIQUELIST.indexOf(X[0]) === -1 && X[0]!="" ){ UNIQUELIST.push(X[0]);}})
  1. THE RESULT IS H,W. NOT AS WHAT LOOKING FOR
1

1 Answers

1
votes

Explanation:

You can use Set and Spread syntax (...) to get the unique values of an array:

const uniqueArray = [...new Set(data)];
  • flat is used to convert the data into 1D array.

Solution:

Adjust 'Sheet1' to the name of your sheet:

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sheet = ss.getSheetByName('Sheet1'); // put the name of the sheet
  const data = sheet.getRange("J2:O2").getValues().flat();
  const uniqueArray = [...new Set(data)].filter(v=>v!='');
  console.log(uniqueArray)
}

enter image description here