オートフィルタを更新した後のすべての非表示行のインデックスを取得する

可能な使用シナリオ

ワークシートのセルにオートフィルターを適用すると、一部の行が自動的に非表示になります。ただし、エンドユーザーが手動で非表示にした場合もあり、それらがオートフィルターによって非表示にされたかどうかはわかりにくいことがあります。Aspose.Cellsは、この問題に対処するためにint[] AutoFilter.refresh(bool hideRows)メソッドを使用します。このメソッドは、オートフィルターによって非表示になった行のインデックスをすべて返します。

オートフィルタの更新後の非表示行インデックスの取得

以下のサンプルコードは、sample Excel fileをロードし、Excelエンドユーザーによって手動で非表示にされた行が含まれています。コードはオートフィルタを適用し、int[] AutoFilter.refresh(bool hideRows)メソッドを使用してオートフィルタを更新し、オートフィルターによって非表示にされたすべての行のインデックスをコンソールに出力し、セルの名前と値とともに非表示にされた行のインデックスを印刷します。

サンプルコード

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Load the sample Excel file
Workbook wb = new Workbook(srcDir + "sampleGetAllHiddenRowsIndicesAfterRefreshingAutoFilter.xlsx");
//Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
//Apply autofilter
ws.getAutoFilter().addFilter(0, "Orange");
//True means, it will refresh autofilter and return hidden rows.
//False means, it will not refresh autofilter but return same hidden rows.
int[] rowIndices = ws.getAutoFilter().refresh(true);
System.out.println("Printing Rows Indices, Cell Names and Values Hidden By AutoFilter.");
System.out.println("--------------------------");
for(int i=0; i<rowIndices.length; i++)
{
int r = rowIndices[i];
Cell cell = ws.getCells().get(r, 0);
System.out.println(r + "\t" + cell.getName() + "\t" + cell.getStringValue());
}

コンソール出力

Printing Rows Indices, Cell Names and Values Hidden By AutoFilter.

\--------------------------

1       A2      Apple

2       A3      Apple

3       A4      Apple

6       A7      Apple

7       A8      Apple

11      A12     Pear

12      A13     Pear