Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the unique items out of a list in Google Apps Script?

I have this code snippet:

function getTimeFrames(){
  var ss = SpreadsheetApp.getActive();
  var cell = ss.getRange("C1").getValue();
  Logger.log(cell);
  
  var s = ss.getSheetByName(cell).getRange("M2:M");
  var unique = new Set(s.getValues());
  
  unique.forEach(x => Logger.log(x))
}

This code would be called from a Google Sheets spreadsheet which serves as an UI, where the C1 cell would be a name of a sheet used for storing data. In that sheet I have a column (M) that I need to get the unique values out of in order to store in a drop-down in the UI sheet.

The problem I have is that I can’t get the unique values part working at all. Throughout my experimentation, I would either get a list of all the values of column M or no Logging output at all.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help is greatly appreciated!

>Solution :

Try

function getTimeFrames(){
  var ss = SpreadsheetApp.getActive();
  var cell = ss.getRange("C1").getValue();
  Logger.log(cell);
  
  var unique = ss.getSheetByName(cell).getRange("M2:M").getValues().flat().filter(onlyUnique)

  console.log(unique)
}
function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading