Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

563
Visualizações
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.

Any help is greatly appreciated!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

This was really a tough job before ES6. But now, with a combination of filter() and indexOf() you can easily pull it off.

For Example:

var List = ['One','Two','Two','One','One','Two','One','Three']
const uniqueList = (value,index,self) =>
{return self.indexOf(value) ===index;}
var finalUniqueList =List.filter(unique); 
about 4 years ago · Juan Pablo Isaza Relatório

0

Try spreading the unique values into an array:

function getTimeFrames() {

  const sheet = SpreadsheetApp.getActive()
  const cell = sheet.getRange(`C1`).getValue()
  
  const values = sheet.getSheetByName(cell)
                      .getRange(`M2:M`)
                      .getValues()
                      .filter(String)
                      .flat()

  const unique = [...new Set(values)]

  Logger.log(unique)

}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda