データソート

Microsoft Excel でのデータのソート

Microsoft Excel でデータをソートするには:

  1. ソートメニューからデータを選択します。ソートダイアログが表示されます。
  2. ソートオプションを選択します。

一般的に、ソートはリスト上で実行されます。リストは、データが列に表示される連続したグループと定義されます。

Aspose.Cells でのデータのソート

Aspose.Cells for Node.js via C++は、昇順または降順に並べ替えるためのDataSorterクラスを提供します。このクラスには、Key1…Key3やOrder1…Order3などの重要なメンバーがあります。これらは並べ替えのキーを定義し、並べ替え順序を指定するために使用されます。

データソートを実装する前に、キーを定義してソート順を設定する必要があります。このクラスは、ワークシート内のセルデータに基づいてデータのソートを実行するために使用される DataSorter.sort メソッドを提供しています。

DataSorter.sort メソッドは、以下のパラメータを受け入れます:

  • Cells、基になるワークシートのセル。
  • CellArea、セル範囲。データソーティングを適用する前にセル領域を定義します。

この例では、Microsoft Excelで作成した「Book1.xls」という名前のテンプレートファイルを使用します。以下のコードを実行した後、データが適切にソートされます。

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
const AsposeCells = require("aspose.cells.node");
// Instantiate a new Workbook object.
// Load a template file.
var workbook = new AsposeCells.Workbook("book1.xls");
// Get the workbook datasorter object.
var sorter = workbook.getDataSorter();
// Set the first order for datasorter object.
sorter.setOrder1(AsposeCells.SortOrder.Descending);
// Define the first key.
sorter.setKey1(0);
// Set the second order for datasorter object.
sorter.setOrder2(AsposeCells.SortOrder.Ascending);
// Define the second key.
sorter.setKey2(1);
// Create a cells area (range).
var ca = new AsposeCells.CellArea();
// Specify the start row index.
ca.startRow = 0;
// Specify the start column index.
ca.startColumn = 0;
// Specify the last row index.
ca.endRow = 13;
// Specify the last column index.
ca.endColumn = 1;
// Sort data in the specified data range (A1:B14)
sorter.sort(workbook.getWorksheets().get(0).getCells(), ca);
// Save the excel file.
workbook.save("output.out.xls");

背景色でデータをソートする

Excelは背景色に基づいてデータを並べ替える機能を提供しています。同じ機能がAspose.Cells for Node.js via C++のDataSorterを使用して提供されており、SortOnTypeのセルカラーをDataSorter.addKey内で使用して、背景色に基づいてデータを並べ替えることができます。指定された色を含むすべてのセルは、SortOrder設定と他のセルの順序に従って上または下に配置され、その他のセルの順序は変更されません。

これがこの機能のテストにダウンロードできるサンプルファイルです。

sampleBackGroundFile.xlsx

outputsampleBackGroundFile.xlsx

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
const AsposeCells = require("aspose.cells.node");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Create a workbook object and load template file
var workbook = new AsposeCells.Workbook("CellsNet46500.xlsx");
// Instantiate data sorter object
var sorter = workbook.getDataSorter();
// Add key for second column for red color
sorter.addKey(1, AsposeCells.SortOnType.CellColor, AsposeCells.SortOrder.Descending, AsposeCells.Color.Red);
// Sort the data based on the key
sorter.sort(workbook.getWorksheets().get(0).getCells(), AsposeCells.CellArea.createCellArea("A2", "C6"));
// Save the output file
workbook.save("outputSortData_CustomSortList.xlsx");

高度なトピック