Especificación de la posición absoluta del elemento pivote
Contents
[
Hide
]
A veces, el usuario necesita especificar la posición absoluta de los elementos pivote, Aspose.Cells API ha expuesto algunas propiedades nuevas y un método para lograr este requisito del usuario.
- AdicionalPivotItem.setPosition() propiedad que se puede usar para especificar el índice de posición en todos los PivotItems independientemente del nodo principal. AdicionalPivotItem.setPositionInSameParentNode() propiedad que se puede usar para especificar el índice de posición en PivotItems en el mismo nodo principal.
- Adicional[PivotItem.move(int count, boolean isSameParent)](https://reference.aspose.com/cells/java/com.aspose.cells/pivotitem#move(int,%20boolean)para mover el elemento hacia arriba o hacia abajo en función del valor de conteo, donde el conteo es el número de posiciones para mover el PivotItem hacia arriba o hacia abajo. Si el valor de conteo es menor que cero, el elemento se moverá hacia arriba, mientras que si el valor de conteo es mayor que cero, PivotItem se moverá hacia abajo, el parámetro de tipo booleano isSameParent especifica si la operación de movimiento debe realizarse en el mismo nodo principal o no.
- Obsoleto elPivotItem.move (recuento int) método, por lo tanto, se sugiere utilizar el método recién agregadoPivotItem.move(int count, boolean isSameParent) en cambio.
Tenga en cuenta que es necesario llamar alPivotTable.refreshData yPivotTable.calculateData métodos antes de usarPivotItem.setPosition(), PivotItem.setPositionInSameParentNode() propiedades yPivotItem.move(int count, boolean isSameParent) método.
Código de muestra
El siguiente código de ejemplo crea una tabla dinámica y luego especifica las posiciones de los elementos dinámicos en el mismo nodo principal.
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"); |