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

可能な使用シナリオ

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

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

サンプルExcelファイルを読み込み、Excelエンドユーザーによって手動で非表示にされた行の一部を示しています。コードはオートフィルタを適用し、int[] AutoFilter.Refresh(bool hideRows)メソッドを使用して自動フィルタによって非表示にされたすべての行の行インデックスを取得し、非表示にされた行のインデックスとセル名、値をコンソールに出力します。

サンプルコード

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Load the sample Excel file
Workbook wb = new Workbook(sourceDir + "sampleGetAllHiddenRowsIndicesAfterRefreshingAutoFilter.xlsx");
//Access first worksheet
Worksheet ws = wb.Worksheets[0];
//Apply autofilter
ws.AutoFilter.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.AutoFilter.Refresh(true);
Console.WriteLine("Printing Rows Indices, Cell Names and Values Hidden By AutoFilter.");
Console.WriteLine("--------------------------");
for (int i = 0; i < rowIndices.Length; i++)
{
int r = rowIndices[i];
Cell cell = ws.Cells[r, 0];
Console.WriteLine(r + "\t" + cell.Name + "\t" + cell.StringValue);
}

コンソール出力

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