Advertisements
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
>Solution :
Try
return columnData.filter((el, i) => i === columnData.indexOf(el) && el !== '').sort()