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

可能な使用シナリオ

ワークシートのセルにオートフィルタを適用すると、いくつかの行が自動的に非表示になります。 ただし、自動フィルタによって非表示にされている行と、Excel エンドユーザーによって手動で非表示にされている行の区別が困難な場合があります。 そのような場合、Aspose.Cells for Python via .NET は AutoFilter.refresh(hide_rows) メソッドを使用してこの問題に対処します。 このメソッドは、自動フィルタによって非表示にされており、Excel エンドユーザーによって手動で非表示にされていない行の行インデックスを返します。

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

以下のサンプルコードでは、Excel エンドユーザーによって手動で非表示にされた行がいくつか含まれる サンプル Excel ファイル を読み込みます。 その後、AutoFilter.refresh(hide_rows) メソッドを使用してオートフィルタを適用し、更新します。 このメソッドは、オートフィルタによって非表示にされたすべての行の行インデックスを取得し、非表示の行のインデックスをセル名と値とともにコンソールに出力します。

サンプルコード

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Load the sample Excel file
wb = Workbook(sourceDir + "sampleGetAllHiddenRowsIndicesAfterRefreshingAutoFilter.xlsx")
# Access first worksheet
ws = wb.worksheets[0]
# Apply autofilter
ws.auto_filter.add_filter(0, "Orange")
# True means, it will refresh autofilter and return hidden rows.
# False means, it will not refresh autofilter but return same hidden rows.
rowIndices = ws.auto_filter.refresh(True)
print("Printing Rows Indices, Cell Names and Values Hidden By AutoFilter.")
print("--------------------------")
for i in range(len(rowIndices)):
r = rowIndices[i]
cell = ws.cells.get(r, 0)
print(str(str(str(str(r) + "\t" ) + cell.name) + "\t" ) + cell.string_value)

コンソール出力

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