ピボットアイテムの絶対位置を指定する
Contents
[
Hide
]
ユーザーがピボットアイテムの絶対位置を指定する必要がある場合、Aspose.Cells API はこのユーザー要件を満たすためにいくつかの新しいプロパティとメソッドを公開しています。
- 親ノードに関係なくすべてのPivotItemsの位置インデックスを指定するために使用できるPivotItem.setPosition()プロパティが追加されました。- 同じ親ノード内のPivotItemsの位置インデックスを指定するために使用できるPivotItem.setPositionInSameParentNode()プロパティが追加されました。
- 移動する位置のカウント値に基づいてアイテムを上下に移動するための PivotItem.move(int count, boolean isSameParent) メソッドが追加されました。カウント値がゼロより小さい場合、アイテムが上に移動し、カウント値がゼロより大きい場合、PivotItem が下に移動します。同じ親ノード内で移動操作を行うかどうかを指定するブール型の isSameParent パラメータが含まれています。
- PivotItem.move(int count) メソッドが廃止されたため、代わりに新たに追加されたメソッド PivotItem.move(int count, boolean isSameParent) を使用することが推奨されます。
なお、これらのプロパティとメソッドを使用する前に、必ず PivotTable.refreshData および PivotTable.calculateData メソッドを呼び出す必要があります。
サンプルコード
次のサンプルコードでは、ピボットテーブルを作成し、同じ親ノード内での Pivot Items の位置を指定しています。
This file contains 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SpecifyAbsolutePositionOfPivotItem.class); | |
Workbook wb = new Workbook(dataDir + "source.xlsx"); | |
Worksheet wsPivot = wb.getWorksheets().add("pvtNew Hardware"); | |
Worksheet wsData = wb.getWorksheets().get("New Hardware - Yearly"); | |
// Get the pivottables collection for the pivot sheet | |
PivotTableCollection pivotTables = wsPivot.getPivotTables(); | |
// Add PivotTable to the worksheet | |
int index = pivotTables.add("='New Hardware - Yearly'!A1:D621", "A3", "HWCounts_PivotTable"); | |
// Get the PivotTable object | |
PivotTable pvtTable = pivotTables.get(index); | |
// Add vendor row field | |
pvtTable.addFieldToArea(PivotFieldType.ROW, "Vendor"); | |
// Add item row field | |
pvtTable.addFieldToArea(PivotFieldType.ROW, "Item"); | |
// Add data field | |
pvtTable.addFieldToArea(PivotFieldType.DATA, "2014"); | |
// Turn off the subtotals for the vendor row field | |
PivotField pivotField = pvtTable.getRowFields().get("Vendor"); | |
pivotField.setSubtotals(PivotFieldSubtotalType.NONE, true); | |
// Turn off grand total | |
pvtTable.setColumnGrand(false); | |
// Please call the PivotTable.RefreshData() and PivotTable.CalculateData(). Before using PivotItem.Position, | |
// PivotItem.PositionInSameParentNode and PivotItem.Move(int count, bool isSameParent). | |
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.PositionInSameParentNode,it will change the original sort sequence, // so when you use | |
* PivotItem.PositionInSameParentNode 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(dataDir + "output.xlsx"); |