I have a function that one of the users suggested to get the criterion of the built-in google sheets filter. I dont know if I'm doing it right but when I paste the getVisibleValues()
function to the cell I get an error:
TypeError: Cannot read property 'getColumnFilterCriteria' of undefined (row18)
function test_getVisibleValues() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test1");
const columnRange = sheet.getRange("C2:C");
const filter = columnRange.getFilter();
const visibleValues = getVisibleValues(filter, columnRange);
console.log(visibleValues);
}
/**
* Returns the values to show.
* Simulates https://developers.google.com/apps-script/reference/spreadsheet/filter-criteria#getvisiblevalues
* @param {Filter} filter
* @param {Range} range
* @returns {string[]}
*/
function getVisibleValues(filter, range) {
const filterCriteria = filter.getColumnFilterCriteria(range.getColumn());
const hiddenValues = filterCriteria.getHiddenValues();
const allNonBlankValues = range.getValues().filter(String);
const uniqueValues = Array.from(new Set(allNonBlankValues));
// filter unique values that aren't hidden
return uniqueValues.flat().filter(value => !hiddenValues.includes(value));
}
Test1
the issue might be that your selected range ( column C) does not have a filter and as a resultgetFilter()
returns null. Can you confirm that ? Also try C1:C instead to get the full column. – soMariotest_getVisibleValues
orgetVisibleValues
. If you execute the latter, then the arguments are not defined and hence the error. I tested your code by executingtest_getVisibleValues
and it didn't generate any error. I think it is straightforward to understand. The functiongetVisibleValues
requires two arguments:filter
andrange
. If you execute first thegetVisibleValues
then the arguments are not defined anywhere. But if you executetest_getVisibleValues
first, then you pass these arguments togetVisibleValues
and then it works. – soMario