ピボットアイテムの絶対位置を指定する
Contents
[
Hide
]
時には、ユーザーがピボットアイテムの絶対位置を指定する必要があり、Aspose.Cells for Node.js via C++ APIはこれを実現するための新しいプロパティとメソッドをいくつか公開しています。
- 親ノードに関係なくすべてのPivotItemsの位置インデックスを指定するために使用できるPivotItem.setPositionプロパティが追加されました。- 同じ親ノード内のPivotItemsの位置インデックスを指定するために使用できるPivotItem.setPositionInSameParentNodeプロパティが追加されました。
- PivotItemを移動するためのPivotItem.moveメソッドが追加されました。こちらでは、移動する位置数(count)に基づいてアイテムを上または下に移動します。count値がゼロ未満の場合、アイテムは上に移動し、count値がゼロより大きい場合は、PivotItemは下に移動します。isSameParentパラメータは、移動操作を同じ親ノード内で実行するかどうかを指定します。
- *PivotItem.move(int count)*メソッドは廃止されたため、新たに追加されたPivotItem.move(number, boolean)メソッドを代わりに使用することを推奨します。
次のサンプルコードは、ピボットテーブルを作成し、その後、同じ親ノード内でのPivot Itemsの位置を指定しています。参照のために、ソースExcelおよび出力Excelファイルをダウンロードできます。出力Excelファイルを開くと、「K11」の親での0番目の位置に「4H12」アイテムが、3番目の位置に「DIF400」が表示されます。同様に、CA32は1番目の位置にあり、AAA3は2番目の位置にあります。
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
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
const AsposeCells = require("aspose.cells.node"); | |
var wb = new AsposeCells.Workbook("source.xlsx"); | |
var wsPivot = wb.getWorksheets().add("pvtNew Hardware"); | |
var wsData = wb.getWorksheets().get("New Hardware - Yearly"); | |
// Get the pivottables collection for the pivot sheet | |
var pivotTables = wsPivot.getPivotTables(); | |
// Add PivotTable to the worksheet | |
var index = pivotTables.add("='New Hardware - Yearly'!A1:D621", "A3", "HWCounts_PivotTable"); | |
// Get the PivotTable object | |
var pvtTable = pivotTables.get(index); | |
// Add vendor row field | |
pvtTable.addFieldToArea(AsposeCells.PivotFieldType.Row, "Vendor"); | |
// Add item row field | |
pvtTable.addFieldToArea(AsposeCells.PivotFieldType.Row, "Item"); | |
// Add data field | |
pvtTable.addFieldToArea(AsposeCells.PivotFieldType.Data, "2014"); | |
// Turn off the subtotals for the vendor row field | |
var pivotField = pvtTable.getRowFields().get("Vendor"); | |
pivotField.setSubtotals(AsposeCells.PivotFieldSubtotalType.None, true); | |
// Turn off grand total | |
pvtTable.setColumnGrand(false); | |
/* | |
* Please call the PivotTable.refreshData() and PivotTable.calculateData() | |
* before using PivotItem.setPosition, | |
* PivotItem.setPositionInSameParentNode and PivotItem.move methods. | |
*/ | |
pvtTable.refreshData(); | |
pvtTable.calculateData(); | |
pvtTable.getRowFields().get("Item").getPivotItems().get("4H12").setPositionInSameParentNode(0); | |
pvtTable.getRowFields().get("Item").getPivotItems().get("DIF400").setPositionInSameParentNode(3); | |
/* | |
* As a result of using PivotItem.setPositionInSameParentNode, | |
* it will change the original sort sequence. | |
* So when you use PivotItem.setPositionInSameParentNode in another parent node. | |
* You need call the method named "calculateData" again. | |
*/ | |
pvtTable.calculateData(); | |
pvtTable.getRowFields().get("Item").getPivotItems().get("CA32").setPositionInSameParentNode(1); | |
pvtTable.getRowFields().get("Item").getPivotItems().get("AAA3").setPositionInSameParentNode(2); | |
// Save file | |
wb.save("output_out.xlsx"); |
注意: PivotItem.setPosition、PivotItem.setPositionInSameParentNodeプロパティおよびPivotItem.moveメソッドを使用する前に、PivotTable.RefreshDataおよびPivotTable.CalculateDataメソッドを呼び出す必要があります。