ピボットアイテムの絶対位置を指定する
Contents
[
Hide
]
ユーザーがピボットアイテムの絶対位置を指定する必要がある場合、Aspose.Cells APIはこれを実現するためのいくつかの新しいプロパティとメソッドを公開しています。
- 親ノードに関係なくすべてのPivotItemsの位置インデックスを指定するために使用できるPivotItem.Positionプロパティが追加されました。- 同じ親ノード内のPivotItemsの位置インデックスを指定するために使用できるPivotItem.PositionInSameParentNodeプロパティが追加されました。
- PivotItemを移動するためのPivotItem.Move(int count, bool isSameParent)メソッドが追加されました。こちらでは、移動する位置数(count)に基づいてアイテムを上または下に移動します。count値がゼロ未満の場合、アイテムは上に移動し、count値がゼロより大きい場合は、PivotItemは下に移動します。isSameParentパラメータは、移動操作を同じ親ノード内で実行するかどうかを指定します。
- *PivotItem.Move(int count)*メソッドは廃止されたため、新しく追加されたPivotItem.Move(int count, bool isSameParent)メソッドを使用することが推奨されています。
次のサンプルコードは、ピボットテーブルを作成し、その後、同じ親ノード内でのPivot Itemsの位置を指定しています。参照のために、ソースExcelおよび出力Excelファイルをダウンロードできます。出力Excelファイルを開くと、「K11」の親での0番目の位置に「4H12」アイテムが、3番目の位置に「DIF400」が表示されます。同様に、CA32は1番目の位置にあり、AAA3は2番目の位置にあります。
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
Workbook wb = new Workbook(dataDir + "source.xlsx"); | |
Worksheet wsPivot = wb.Worksheets.Add("pvtNew Hardware"); | |
Worksheet wsData = wb.Worksheets["New Hardware - Yearly"]; | |
// Get the pivottables collection for the pivot sheet | |
PivotTableCollection pivotTables = wsPivot.PivotTables; | |
// Add PivotTable to the worksheet | |
int index = pivotTables.Add("='New Hardware - Yearly'!A1:D621", "A3", "HWCounts_PivotTable"); | |
// Get the PivotTable object | |
PivotTable pvtTable = pivotTables[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.RowFields["Vendor"]; | |
pivotField.SetSubtotals(PivotFieldSubtotalType.None, true); | |
// Turn off grand total | |
pvtTable.ColumnGrand = 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.RowFields["Item"].PivotItems["4H12"].PositionInSameParentNode = 0; | |
pvtTable.RowFields["Item"].PivotItems["DIF400"].PositionInSameParentNode = 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.RowFields["Item"].PivotItems["CA32"].PositionInSameParentNode = 1; | |
pvtTable.RowFields["Item"].PivotItems["AAA3"].PositionInSameParentNode = 2; | |
// Save file | |
wb.Save(dataDir + "output_out.xlsx"); |
注意: PivotItem.Position、PivotItem.PositionInSameParentNodeプロパティおよびPivotItem.Move(int count, bool isSameParent)メソッドを使用する前に、PivotTable.RefreshDataおよびPivotTable.CalculateDataメソッドを呼び出す必要があります。