Especificar la posición absoluta del ítem de tabla dinámica
A veces, el usuario necesita especificar la posición absoluta de los ítems de tabla dinámica, la API de Aspose.Cells ha expuesto algunas nuevas propiedades y un método para cumplir con este requisito del usuario.
- Se agregó la propiedad PivotItem.setPosition() que se puede usar para especificar el índice de posición en todos los PivotItems independientemente del nodo padre. Se agregó la propiedad PivotItem.setPositionInSameParentNode() que se puede utilizar para especificar el índice de posición en los PivotItems bajo el mismo nodo padre.
- Se agregó el método PivotItem.move(int count, boolean isSameParent) para mover el ítem hacia arriba o hacia abajo en función del valor del contador, donde el contador es el número de posiciones para mover el PivotItem hacia arriba o hacia abajo. Si el valor del contador es menor que cero, el ítem se moverá hacia arriba, mientras que si el valor del contador es mayor que cero, el PivotItem se moverá hacia abajo, el parámetro de tipo Boolean isSameParent especifica si la operación de movimiento debe realizarse en el mismo nodo padre o no.
- Se ha vuelto obsoleto el método PivotItem.move(int count), por lo tanto, se sugiere usar el nuevo método PivotItem.move(int count, boolean isSameParent) en su lugar.
Tenga en cuenta que es necesario llamar a los métodos PivotTable.refreshData y PivotTable.calculateData antes de usar las propiedades PivotItem.setPosition(), PivotItem.setPositionInSameParentNode() y PivotItem.move(int count, boolean isSameParent).
Código de Muestra
El siguiente código de muestra crea una tabla dinámica y luego especifica las posiciones de los ítems de tabla dinámica en el mismo nodo padre.
// 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"); |