我有一个成功的谷歌脚本,它允许我从谷歌表单文档中获取内容,并将其显示在下拉菜单中。这很好用,但我不喜欢它的显示顺序,我需要按字母顺序列出菜单,但由于某种原因,我无法计算出来。
这是脚本:
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;
}
有人能指引我吗
尝试
return columnData.filter((el, i) => i === columnData.indexOf(el) && el !== '').sort()