ピボット アイテムの絶対位置の指定
Contents
[
Hide
]
場合によっては、ユーザーはピボット アイテムの絶対位置を指定する必要があります。
- 追加したPivotItem.Position親ノードに関係なく、すべての PivotItems の位置インデックスを指定するために使用できるプロパティ。追加したPivotItem.PositionInSameParentNode同じ親ノードの下の PivotItems で位置インデックスを指定するために使用できるプロパティ。
- 追加したPivotItem.Move(int count, bool isSameParent)count 値に基づいて項目を上下に移動するためのメソッド。 count は PivotItem を上下に移動する位置の数です。カウント値がゼロより小さい場合、アイテムは上に移動し、カウント値がゼロより大きい場合、PivotItem は下に移動します。ブール型の isSameParent パラメータは、移動操作を同じ親ノードで実行する必要があるかどうかを指定しますか否か。
- 廃止された*PivotItem.Move(整数カウント)*メソッドしたがって、新しく追加されたメソッドを使用することをお勧めしますPivotItem.Move(int count, bool isSameParent)代わりは。
次のサンプル コードは、ピボット テーブルを作成し、同じ親ノードでピボット アイテムの位置を指定します。ダウンロードできますソースエクセルとエクセル出力参照用のファイル。出力された Excel ファイルを開くと、ピボット アイテム「4H12」が親「K11」の 0 番目の位置にあり、「DIF400」が 3 番目の位置にあることがわかります。同様に、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 | |
// 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"); |
使用する前に PivotTable.RefreshData および PivotTable.CalculateData メソッドを呼び出す必要があることに注意してください。PivotItem.Position, PivotItem.PositionInSameParentNodeプロパティとPivotItem.Move(int count, bool isSameParent)方法。