Agregar filtro en tabla dinámica
Escenarios de uso posibles
Cuando creas una tabla dinámica con datos conocidos y deseas filtrar la tabla dinámica, debes aprender y usar el filtrado. Puede ayudarte a filtrar los datos que deseas de manera efectiva. Usando la API Aspose.Cells for Node.js via C++, puedes agregar un filtro en los valores de campo en las tablas dinámicas.
Agregar filtro en tabla dinámica usando Aspose.Cells for Node.js via C++
Por favor, consulta el siguiente código de ejemplo. Establece los datos y crea una Tabla Dinámica basada en ellos. Luego añade un filtro en el campo de fila de la tabla dinámica. Finalmente, guarda el libro de trabajo en formato XLSX de salida. Después de ejecutar el código de ejemplo, se añade una tabla dinámica con filtro top10 a la hoja de cálculo.
Código de muestra
const AsposeCells = require("aspose.cells.node"); | |
//Instantiating an Workbook object | |
var workbook = new AsposeCells.Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
var ws = workbook.getWorksheets().get(0); | |
var cells = ws.getCells(); | |
//Setting the value to the cells | |
var cell = cells.get("A1"); | |
cell.putValue("Fruit"); | |
cell = cells.get("B1"); | |
cell.putValue("Count"); | |
cell = cells.get("A2"); | |
cell.putValue("Apple"); | |
cell = cells.get("A3"); | |
cell.putValue("Mango"); | |
cell = cells.get("A4"); | |
cell.putValue("Blackberry"); | |
cell = cells.get("A5"); | |
cell.putValue("Cherry"); | |
cell = cells.get("A6"); | |
cell.putValue("Guava"); | |
cell = cells.get("A7"); | |
cell.putValue("Carambola"); | |
cell = cells.get("A8"); | |
cell.putValue("Banana"); | |
cell = cells.get("B2"); | |
cell.putValue(5); | |
cell = cells.get("B3"); | |
cell.putValue(3); | |
cell = cells.get("B4"); | |
cell.putValue(6); | |
cell = cells.get("B5"); | |
cell.putValue(4); | |
cell = cells.get("B6"); | |
cell.putValue(5); | |
cell = cells.get("B7"); | |
cell.putValue(2); | |
cell = cells.get("B8"); | |
cell.putValue(20); | |
//Adding a PivotTable to the worksheet | |
var i = ws.getPivotTables().add("=A1:B8", "D10", "PivotTable1"); | |
//Accessing the instance of the newly added PivotTable | |
var pivotTable = ws.getPivotTables().get(i); | |
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Row, 0); | |
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, "Count"); | |
pivotTable.getDataFields().get(0).setFunction(AsposeCells.ConsolidationFunction.Sum); | |
var field = pivotTable.getRowFields().get(0); | |
field.setIsAutoSort(true); | |
field.setIsAscendSort(false); | |
field.setAutoSortField(0); | |
//Add top10 filter | |
var index = pivotTable.getPivotFilters().add(0, AsposeCells.PivotFilterType.Count); | |
var filter = pivotTable.getPivotFilters().get(index); | |
filter.getAutoFilter().filterTop10(0, True, False, 5); | |
pivotTable.refreshData(); | |
pivotTable.calculateData(); | |
workbook.save("out.xlsx"); |