Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Adding a base field to the data (value) region is the first step in shaping how a pivot table aggregates the source data. Aspose.Cells exposes PivotTable.AddFieldToArea(PivotFieldType, string), an overload that accepts the constant PivotFieldType.Data and the source-column name. Once a field is added to the data region, the API exposes it through the PivotTable.DataFields collection, in the order in which the fields were added. By default, a numeric source column is summarised with Sum, while a non-numeric column defaults to Count.
const AsposeCells = require("aspose.cells");
const workbook = new AsposeCells.Workbook();
const worksheet = workbook.getWorksheets().get(0);
worksheet.setName("Data");
const headers = ["Category", "Item", "Year", "Amount"];
for (let j = 0; j < headers.length; j++) {
worksheet.getCells().get(0, j).putValue(headers[j]);
}
const data = [
["Fruit", "Apple", 2020, 100],
["Fruit", "Apple", 2021, 150],
["Fruit", "Banana", 2020, 80],
["Fruit", "Banana", 2021, 90],
["Vegetable", "Carrot", 2020, 50],
["Vegetable", "Carrot", 2021, 60],
["Vegetable", "Daikon", 2020, 40],
["Vegetable", "Daikon", 2021, 45]
];
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].length; j++) {
worksheet.getCells().get(i + 1, j).putValue(data[i][j]);
}
}
const pivotIndex = worksheet.getPivotTables().add("A1:D9", "F3", "PivotTable1", true, false);
const pivotTable = worksheet.getPivotTables().get(pivotIndex);
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Row, "Category");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Row, "Item");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Column, "Year");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, "Amount");
pivotTable.calculateData();
workbook.save("output_drag.xlsx");
Once a field lives in the data region, Aspose.Cells exposes it through the PivotField object on PivotTable.DataFields. Each PivotField has a writable Function property of type ConsolidationFunction, which controls the aggregate applied to that field’s underlying values. ConsolidationFunction is an enum with members Sum, Count, Average, Max, Min, Product, StdDev, StdDevp, Var, and Varp — the first six cover the vast majority of real-world use cases, while the last four are statistical aggregates useful for variance analysis.
Function only affects the aggregate; the source column and the pivot’s row/column structure are not modified. To switch the aggregate for an existing data field, set pivotTable.DataFields[i].Function = ConsolidationFunction.<X>; and then call pivotTable.CalculateData() to re-render the pivot.
const AsposeCells = require("aspose.cells");
const workbook = new AsposeCells.Workbook();
const worksheet = workbook.getWorksheets().get(0);
worksheet.setName("Data");
const headers = ["Category", "Item", "Year", "Amount"];
for (let j = 0; j < headers.length; j++) {
worksheet.getCells().get(0, j).putValue(headers[j]);
}
const data = [
["Fruit", "Apple", 2020, 100],
["Fruit", "Apple", 2021, 150],
["Fruit", "Banana", 2020, 80],
["Fruit", "Banana", 2021, 90],
["Vegetable", "Carrot", 2020, 50],
["Vegetable", "Carrot", 2021, 60],
["Vegetable", "Daikon", 2020, 40],
["Vegetable", "Daikon", 2021, 45]
];
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].length; j++) {
worksheet.getCells().get(i + 1, j).putValue(data[i][j]);
}
}
const pivotIndex = worksheet.getPivotTables().add("A1:D9", "F3", "PivotTable1", true, false);
const pivotTable = worksheet.getPivotTables().get(pivotIndex);
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Row, "Category");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Row, "Item");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Column, "Year");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, "Amount");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, "Amount");
pivotTable.getDataFields().get(1).setFunction(AsposeCells.ConsolidationFunction.Count);
pivotTable.calculateData();
workbook.save("output_function.xlsx");
When a pivot table contains two or more data fields, Aspose.Cells exposes an additional virtual field called PivotTable.ValuesField. This virtual field represents the aggregate of every data field that lives in the data region. You can drag it into the Row or Column region as a base pivot field, which is useful for laying out multiple measures side by side.
PivotTable.ValuesField does not work if there is no or only one value field.
const AsposeCells = require("aspose.cells");
const workbook = new AsposeCells.Workbook();
const worksheet = workbook.getWorksheets().get(0);
worksheet.setName("Data");
const headers = ["Category", "Item", "Year", "Amount"];
for (let j = 0; j < headers.length; j++) {
worksheet.getCells().get(0, j).putValue(headers[j]);
}
const data = [
["Fruit", "Apple", 2020, 100],
["Fruit", "Apple", 2021, 150],
["Fruit", "Banana", 2020, 80],
["Fruit", "Banana", 2021, 90],
["Vegetable", "Carrot", 2020, 50],
["Vegetable", "Carrot", 2021, 60],
["Vegetable", "Daikon", 2020, 40],
["Vegetable", "Daikon", 2021, 45]
];
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].length; j++) {
worksheet.getCells().get(i + 1, j).putValue(data[i][j]);
}
}
const pivotIndex = worksheet.getPivotTables().add("A1:D9", "F3", "PivotTable1", true, false);
const pivotTable = worksheet.getPivotTables().get(pivotIndex);
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Row, "Category");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Row, "Item");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Column, "Year");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, "Amount");
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, "Amount");
pivotTable.getDataFields().get(1).setFunction(AsposeCells.ConsolidationFunction.Count);
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Column, pivotTable.getValuesField());
pivotTable.calculateData();
workbook.save("output_plot.xlsx");
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.