3

I'm having an issue with one of my scripts created from a macro. I want to update a filter onEdit but only when a certain sheet ("Invoice") is open. Here's what I have now that works across all sheets. I've tried a few different things in this forum but I haven't gotten anything to work.

function onEdit(e) {
  var spreadsheet = SpreadsheetApp.getActiveSheet();
  spreadsheet.getRange('D16').activate();
  var criteria = SpreadsheetApp.newFilterCriteria()
  .setHiddenValues([''])
  .build();
  spreadsheet.getActiveSheet().getFilter().setColumnFilterCriteria(4, criteria);
};
1

1 Answer 1

3

Add a sheet name test like this:

function onEdit(e) {
  const sheet = e.range.getSheet();
  if (sheet.getName() !== 'Invoice') {
    return;
  }
  const criteria = SpreadsheetApp.newFilterCriteria()
    .setHiddenValues([''])
    .build();
  sheet.getFilter().setColumnFilterCriteria(4, criteria);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.