Doesn't sheet.isRowHiddenByFilter(n) work with slicers?

耗尽温柔 提交于 2021-01-29 04:12:33

问题


I am trying to use a script to do something to only the rows that are not filtered when using slicers in Google Sheets. I can't get it to work.

I have tried this:

Logger.log(SpreadsheetApp.getActiveSheet().isRowHiddenByFilter(1));

I get "false" even if the row is hidden by using a slicer (new feature in Google Sheets).

Anyone got it to work?

Edit: It works with regular filters, grammar, edited row number in function parameter. Replaced the phrase filter control (direct translation from Norwegian) to slicers, which is the correct english name for the feature.


回答1:


  • You want to use isRowHiddenByFilter() for Slicer.

If my understanding is correct, how about this answer? Please think of this as just one of several answers.

By the update of Google side at November 6, 2019, Class Slicer was added. By this, Slicer of Spreadsheet got to be able to be managed with Google Apps Script.

In this answer, I would like to propose the method for using isRowHiddenByFilter() using Class Slicer.

Flow:

Unfortunately, even when Class Slicer was added, isRowHiddenByFilter() cannot be directly used for the Slicer. So as a workaround, the following flow is used.

  1. Retrieve the current Slicer.
  2. Retrieve the range, column position and the filter criteria from the Slicer.
  3. Create new basic filter using the retrieved range, column position and the filter criteria.
    • Here, the filter of Slicer is copied as the basic filter.
  4. Here, isRowHiddenByFilter() can be used for the created basic filter.
  5. Delete the basic filter.

Usage

1. Create Slicer on Spreadsheet

Here, as a test case, please manually create new Slicer on a sheet in the Spreadsheet. This sample script supposes that only one Slicer is put in the active sheet.

2. IMPORTANT: Save Setting of Slicer

When Set current filters as default is selected (clicked), the setting of Slicer is saved. Please save the setting of the Slicer by selecting Set current filters as default. You can see Set current filters as default at the following figure.

If Set current filters as default is not selected, the setting of the current Slicer is not saved. Please be careful this. The official document can be seen at here.

3. Run sample script

When the function of myFunction() is run, the filtered values of Slicer on the active sheet can be retrieved using isRowHiddenByFilter().

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var slicer = sheet.getSlicers()[0];
  var range = slicer.getRange();
  var filter = range
    .createFilter()
    .setColumnFilterCriteria(
      slicer.getColumnPosition(),
      slicer.getFilterCriteria()
    );
  var result = range.getValues().filter(function(_, i) {
    return !sheet.isRowHiddenByFilter(i + 1);
  });
  filter.remove();
  Logger.log(result);
}
  • In this case, when return !sheet.isRowHiddenByFilter(i + 1); is modified to return sheet.isRowHiddenByFilter(i + 1);, the hidden rows can be retrieved.

References

  • isRowHiddenByFilter(rowPosition)
  • Class Slicer
  • Save your slicer filtering selections


来源:https://stackoverflow.com/questions/57776892/doesnt-sheet-isrowhiddenbyfiltern-work-with-slicers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!