Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
Use a Google Script to display a dropdown menu in a HTML form in alphabetical order from a Google Sheet Doc

I have a successful Google script that allows me to grab the contents from a Google Sheets Doc and display it in a dropdown menu. This works perfectly but I don't like the order it's displayed in, I need to list the menu alphabetically but for some reason I can't work it out.

Here's the script:

function getColors() { 
  const sheet = SpreadsheetApp.openById("1234567").getSheetByName("tab");

  const colName = 'Media Type';
  const colNum = getColNumByName(colName);
  if (colNum === null) {
    Logger.log('Column ' + colName + ' was not found!');
    return [];
  }

  const firstRow = 2;
  const lastRow = sheet.getLastRow();

  // get all values from column
  const columnData = sheet.getRange(firstRow, colNum, lastRow).getValues().flat();

  // filter values on duplicates
  return columnData.filter((el, i) => i === columnData.indexOf(el) && el !== '');
}

function getColNumByName(colName, row = 1) {
  const sheet = SpreadsheetApp.openById("1234567").getSheetByName("tab");
  
  const [data] = sheet.getRange(row, 1, row, sheet.getLastColumn()).getValues();
  const col = data.indexOf(colName);

  // adding 1 because column nums starting from 1
  return col === -1 ? null : col + 1;
}

Can someone guide me please

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try

return columnData.filter((el, i) => i === columnData.indexOf(el) && el !== '').sort()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!