How to skip hidden rows while iterating through Google Spreadsheet w/ Google Apps Script

There's no direct way of doing this in Apps Script, but there is a feature request open to provide a way get the show/hide status of a row, if you want to star it.


A workaround using SUBTOTAL. Create 2 columns A and B. A must always have a value and B has a set of formulas. These 2 columns look like this:

A |           B
---------------------------
1 | =NOT(SUBTOTAL(103, A1))
1 | =NOT(SUBTOTAL(103, A2))
1 | =NOT(SUBTOTAL(103, A3))

SUBTOTAL returns a subtotal using a specified aggregation function. The first argument 103 defines the type of function used for aggregation. The second argument is the range to apply the function to.

  • 3 means COUNTA and counts the number of values in the range
  • +100 means ignore hidden cells in the range.

The result of SUBTOTAL with a range of 1 cell will be 0 when the cell is hidden and 1 when the cell is shown. NOT inverts it.

Now you can read the column B with your script to know if a row is hidden.

Here's the transposed question and answer: https://stackoverflow.com/a/27846180/1385429


New API as of 2018 that's useful for this problem: isRowHiddenByUser. See also isRowFilteredByUser.