Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aggiungere un campo di base all’area dati (valore) è il primo passo per definire come una tabella pivot aggrega i dati di origine. Aspose.Cells espone PivotTable.addFieldToArea(PivotFieldType, string), un overload che accetta la costante PivotFieldType.Data e il nome della colonna di origine. Una volta che un campo viene aggiunto all’area dati, l’API lo espone tramite la raccolta PivotTable.getDataFields(), nell’ordine in cui i campi sono stati aggiunti. Per impostazione predefinita, una colonna di origine numerica viene sintetizzata con ConsolidationFunction.Sum, mentre una colonna non numerica utilizza Count come valore predefinito.
const AsposeCells = require("aspose.cells");
const workbook = new AsposeCells.Workbook();
const worksheet = workbook.getWorksheets().get(0);
worksheet.setName("Data");
// Intestazioni in A1:D1
worksheet.getCells().get(0, 0).putValue("Category");
worksheet.getCells().get(0, 1).putValue("Item");
worksheet.getCells().get(0, 2).putValue("Year");
worksheet.getCells().get(0, 3).putValue("Amount");
// Righe di dati A2:D9 utilizzando cicli annidati con branching su j
for (let i = 1; i <= 8; i++) {
for (let j = 0; j < 4; j++) {
switch (j) {
case 0:
worksheet.getCells().get(i, j).putValue(i <= 4 ? "Fruit" : "Vegetable");
break;
case 1:
if (i === 1 || i === 2) worksheet.getCells().get(i, j).putValue("Apple");
else if (i === 3 || i === 4) worksheet.getCells().get(i, j).putValue("Banana");
else if (i === 5 || i === 6) worksheet.getCells().get(i, j).putValue("Carrot");
else worksheet.getCells().get(i, j).putValue("Daikon");
break;
case 2:
worksheet.getCells().get(i, j).putValue(2020 + ((i - 1) % 2));
break;
case 3:
if (i === 1) worksheet.getCells().get(i, j).putValue(100);
else if (i === 2) worksheet.getCells().get(i, j).putValue(150);
else if (i === 3) worksheet.getCells().get(i, j).putValue(80);
else if (i === 4) worksheet.getCells().get(i, j).putValue(90);
else if (i === 5) worksheet.getCells().get(i, j).putValue(50);
else if (i === 6) worksheet.getCells().get(i, j).putValue(60);
else if (i === 7) worksheet.getCells().get(i, j).putValue(40);
else worksheet.getCells().get(i, j).putValue(45);
break;
}
}
}
// Aggiungi tabella pivot in F3 con nome PivotTable1
const pivotIndex = worksheet.getPivotTables().add("A1:D9", "F3", "PivotTable1", true, false);
const pivotTable = worksheet.getPivotTables().get(pivotIndex);
// Layout pivot: Category e Item su Riga, Year su Colonna, Amount come campo dati
pivotTable.addFieldToArea(AsposeCells.Pivot.PivotFieldType.Row, "Category");
pivotTable.addFieldToArea(AsposeCells.Pivot.PivotFieldType.Row, "Item");
pivotTable.addFieldToArea(AsposeCells.Pivot.PivotFieldType.Column, "Year");
pivotTable.addFieldToArea(AsposeCells.Pivot.PivotFieldType.Data, "Amount");
pivotTable.calculateData();
workbook.save("output_drag.xlsx");
Ogni campo inserito nell’area dati viene incapsulato internamente come un’istanza di PivotField, e la sua proprietà getFunction() restituisce un valore dall’enum ConsolidationFunction. Lo stesso setter setFunction() ti consente di passare tra le aggregazioni disponibili, tra cui Sum, Count, Average, Max, Min, Product, StdDev, StdDevp, Var e Varp.
Puoi quindi mantenere un campo dati come Sum mentre aggiungi un secondo campo dati che fa riferimento alla stessa colonna di origine ma utilizza Count o Average, tutto in un’unica pivot.
let worksheet = workbook.getWorksheets().get(0);
worksheet.setName("Data");
worksheet.getCells().get(0, 0).putValue("Category");
worksheet.getCells().get(0, 1).putValue("Item");
worksheet.getCells().get(0, 2).putValue("Year");
worksheet.getCells().get(0, 3).putValue("Amount");
for (let i = 1; i <= 8; i++)
{
for (let j = 0; j <= 3; j++)
{
if (j == 0)
{
worksheet.getCells().get(i, j).putValue(i <= 5 ? "Fruit" : "Vegetable");
}
else if (j == 1)
{
let items = ["Apple", "Apple", "Banana", "Banana", "Carrot", "Carrot", "Daikon", "Daikon"];
worksheet.getCells().get(i, j).putValue(items[i - 1]);
}
else if (j == 2)
{
let years = [2020, 2021, 2020, 2021, 2020, 2021, 2020, 2021];
worksheet.getCells().get(i, j).putValue(years[i - 1]);
}
else
{
let amounts = [100, 150, 80, 90, 50, 60, 40, 45];
worksheet.getCells().get(i, j).putValue(amounts[i - 1]);
}
}
}
let pivotIndex = worksheet.getPivotTables().add("A1:D9", "F3", "PivotTable1", true, false);
let 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");
let countField = pivotTable.getDataFields().get(1);
countField.setFunction(AsposeCells.ConsolidationFunction.Count);
pivotTable.calculateData();
workbook.save("output_function.xlsx");
Quando una tabella pivot contiene due o più campi dati, Aspose.Cells espone un ulteriore campo virtuale chiamato PivotTable.getValuesField. Questo campo virtuale rappresenta l’aggregazione di ogni campo dati che risiede nell’area dati. Puoi trascinarlo nell’area Riga o Colonna come campo pivot di base, utile per affiancare più misure.
PivotTable.getValuesField() non funziona se non c’è alcun campo valore o se ne esiste solo uno.
Gli scenari seguenti illustrano tre esempi end-to-end che dimostrano ciascuna funzionalità descritta sopra applicata alla stessa struttura pivot.
let workbook = new AsposeCells.Workbook();
let worksheet = workbook.getWorksheets().get(0);
worksheet.setName("Data");
worksheet.getCells().get(0, 0).putValue("Category");
worksheet.getCells().get(0, 1).putValue("Item");
worksheet.getCells().get(0, 2).putValue("Year");
worksheet.getCells().get(0, 3).putValue("Amount");
let categories = ["Fruit", "Fruit", "Fruit", "Fruit", "Vegetable", "Vegetable", "Vegetable", "Vegetable"];
let items = ["Apple", "Apple", "Banana", "Banana", "Carrot", "Carrot", "Daikon", "Daikon"];
let years = [2020, 2021, 2020, 2021, 2020, 2021, 2020, 2021];
let amounts = [100, 150, 80, 90, 50, 60, 40, 45];
for (let i = 1; i <= 8; i++)
{
for (let j = 0; j <= 3; j++)
{
if (j == 0) worksheet.getCells().get(i, j).putValue(categories[i - 1]);
else if (j == 1) worksheet.getCells().get(i, j).putValue(items[i - 1]);
else if (j == 2) worksheet.getCells().get(i, j).putValue(years[i - 1]);
else worksheet.getCells().get(i, j).putValue(amounts[i - 1]);
}
}
let pivotIndex = worksheet.getPivotTables().add("A1:D9", "F3", "PivotTable1", true, false);
let 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.