ピボットテーブルを挿入する
Contents
[
Hide
]
ピボットテーブルの作成
Aspose.Cells for Node.js via C++を使って、ピボットテーブルをプログラム的にスプレッドシートに追加することが可能です。
ピボットテーブルオブジェクトモデル
Aspose.Cells for Node.js via C++は、ピボットテーブルを作成・制御するための特別なクラスセットを提供します。これらのクラスは、PivotTableオブジェクトを作成・設定するために使われ、これがピボットテーブルの基本構造となります。次のオブジェクトがあります:
- PivotFieldは、PivotTableのフィールドを表します。
- PivotFieldCollectionは、PivotTableのすべてのPivotFieldオブジェクトのコレクションを表します。
- PivotTableは、ワークシート上のPivotTableを表します。
- PivotTableCollectionは、ワークシート上のすべてのPivotTableオブジェクトのコレクションを表します。
Aspose.Cellsを使用して簡単なピボットテーブルを作成する
- CellオブジェクトのputValueメソッドを使用してワークシートにデータを追加します。 このデータは、ピボットテーブルのデータソースとして使用されます。
- ワークシートにピボットテーブルを追加するために、PivotTablesコレクションのaddメソッドを呼び出します。このメソッドはWorksheetオブジェクトでカプセル化されています。
- 新しいPivotTableオブジェクトをPivotTablesコレクションから取得します。このコレクションはPivotTableのインデックスを渡すことでアクセスできます。
- ピボットテーブルを管理するために、上記で説明したPivotTableのいずれかを使用します。
例のコードを実行すると、ワークシートにピボットテーブルが追加されます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AsposeCells = require("aspose.cells.node"); | |
//Instantiating a Workbook object | |
var workbook = new AsposeCells.Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
var sheetIndex = workbook.getWorksheets().add(); | |
var sheet = workbook.getWorksheets().get(sheetIndex); | |
var cells = sheet.getCells(); | |
//Setting the value to the cells | |
var cell = cells.get("A1"); | |
cell.setValue("Sport"); | |
cell = cells.get("B1"); | |
cell.setValue("Quarter"); | |
cell = cells.get("C1"); | |
cell.setValue("Sales"); | |
cell = cells.get("A2"); | |
cell.setValue("Golf"); | |
cell = cells.get("A3"); | |
cell.setValue("Golf"); | |
cell = cells.get("A4"); | |
cell.setValue("Tennis"); | |
cell = cells.get("A5"); | |
cell.setValue("Tennis"); | |
cell = cells.get("A6"); | |
cell.setValue("Tennis"); | |
cell = cells.get("A7"); | |
cell.setValue("Tennis"); | |
cell = cells.get("A8"); | |
cell.setValue("Golf"); | |
cell = cells.get("B2"); | |
cell.setValue("Qtr3"); | |
cell = cells.get("B3"); | |
cell.setValue("Qtr4"); | |
cell = cells.get("B4"); | |
cell.setValue("Qtr3"); | |
cell = cells.get("B5"); | |
cell.setValue("Qtr4"); | |
cell = cells.get("B6"); | |
cell.setValue("Qtr3"); | |
cell = cells.get("B7"); | |
cell.setValue("Qtr4"); | |
cell = cells.get("B8"); | |
cell.setValue("Qtr3"); | |
cell = cells.get("C2"); | |
cell.setValue(1500); | |
cell = cells.get("C3"); | |
cell.setValue(2000); | |
cell = cells.get("C4"); | |
cell.setValue(600); | |
cell = cells.get("C5"); | |
cell.setValue(1500); | |
cell = cells.get("C6"); | |
cell.setValue(4070); | |
cell = cells.get("C7"); | |
cell.setValue(5000); | |
cell = cells.get("C8"); | |
cell.setValue(6430); | |
var pivotTables = sheet.getPivotTables(); | |
//Adding a PivotTable to the worksheet | |
var index = pivotTables.add("=A1:C8", "E3", "PivotTable2"); | |
//Accessing the instance of the newly added PivotTable | |
var pivotTable = pivotTables.get(index); | |
//Unshowing grand totals for rows. | |
pivotTable.setRowGrand(false); | |
//Dragging the first field to the row area. | |
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Row, 0); | |
//Dragging the second field to the column area. | |
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Column, 1); | |
//Dragging the third field to the data area. | |
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, 2); | |
//Saving the Excel file | |
workbook.save("CreatePivotTable_out.xlsx"); |
データソースとしてセル範囲を割り当てるときには、範囲は左上から右下に向かっている必要があります。例えば、「A1:C3」は有効ですが、「C3:A1」は無効です。