Kevin Ngo
Kevin Ngo

Reputation: 51

Use Google App Script to find unique cell value in a row (including duplicated cell values)

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 :

  1. THE RESULT IS H,W. NOT AS WHAT LOOKING FOR

Upvotes: 1

Views: 141

Answers (1)

Marios
Marios

Reputation: 27350

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

Upvotes: 1

Related Questions