Reputation: 25
I have come out with below function to filter the row according to the drop down list in cell G2. However, it also hide my header as well. Imgur
Is it possible to exempt the row with "all sites" in column D from the filter? I need it to be shown at all time no matter what i select from the drop down list. before filter :Imgur
my goal is to achieve: selection made in drop down list will show all sites and those row containing what was chosen. Example: Hub 01 was selected from drop down list. Location in column D that does not include hub 01 will be hidden. Only showing row for all sites and contain Hub 01. Imgur
function hideRow () {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('Risk register'); // the sheet name
var cell2 = sheet.getRange('G2').getValue();
var criteria = SpreadsheetApp.newFilterCriteria().whenTextContains(cell2);
sheet.getFilter().setColumnFilterCriteria(4, criteria);
}
Upvotes: 1
Views: 420
Reputation: 201388
I believe your goal as follows.
Risk register
.For this, how about this answer?
Please copy and paste the following script to the script editor and save it. And, please change the value of the dropdown list of the cell "G2" on the sheet of Risk register
. By this, the script is run.
function onEdit(e) {
const range = e.range;
const sheet = range.getSheet();
if (sheet.getSheetName() == "Risk register" && range.getA1Notation() == "G2") {
const value = e.value;
let criteria = SpreadsheetApp.newFilterCriteria();
criteria = value.toLowerCase() == "all site"
? criteria.whenTextEqualTo(value)
: criteria.whenFormulaSatisfied(`=REGEXMATCH(D4,"All site|${value}")`);
const sheetFilter = sheet.getFilter();
if (sheetFilter) sheetFilter.remove();
sheet.getRange("D3:D").createFilter().setColumnFilterCriteria(4, criteria.build());
}
}
onEdit()
by the script editor, an error occurs. Please be careful this.Upvotes: 1