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

可能な使用シナリオ

ワークシートで自動フィルターを適用すると、一部の行が自動的に非表示になります。しかし、エクセルのエンドユーザーによって手動で既に非表示にされている行もあり、それらは自動フィルターによるものではありません。そのため、自動フィルターにより非表示となった行と手動で非表示にされた行を区別することは難しいです。Aspose.Cells for Node.js via C++はこの問題をAutoFilter.refresh(hideRows)配列を用いて解決します。この方法は、自動フィルターによって非表示になったが手動ではない行の行インデックスを返します。

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

Excelのエンドユーザーによって手動で非表示になった行を含むサンプルExcelファイルをロードし、自動フィルターを適用・更新し、非表示の行のインデックスを返すAutoFilter.refresh(hideRows)メソッドを使用した例。これらのインデックスとセルの名前・値もコンソールに出力されます。

サンプルコード

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Load the sample Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sampleGetAllHiddenRowsIndicesAfterRefreshingAutoFilter.xlsx"));
// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);
// Apply autofilter
worksheet.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.
const rowIndices = worksheet.getAutoFilter().refresh(true);
console.log("Printing Rows Indices, Cell Names and Values Hidden By AutoFilter.");
console.log("--------------------------");
rowIndices.forEach(r => {
const cell = worksheet.getCells().get(r, 0);
console.log(`${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