Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
L’ajout d’un champ de base à la zone de données (valeur) constitue la première étape pour façonner la manière dont un tableau croisé dynamique agrège vos données sources. Aspose.Cells expose PivotTable.AddFieldToArea(PivotFieldType, string), une surcharge qui accepte la constante PivotFieldType.Data et le nom de la colonne source. Une fois qu’un champ est ajouté à la zone de données, l’API l’expose via la collection PivotTable.DataFields, dans l’ordre dans lequel les champs ont été ajoutés. Par défaut, une colonne source numérique est résumée avec ConsolidationFunction.Sum, tandis qu’une colonne non numérique utilise Count par défaut.
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
Aspose::Cells::Startup();
Workbook workbook;
Worksheet worksheet = workbook.GetWorksheets().Get(0);
worksheet.SetName(u"Data");
Cells cells = worksheet.GetCells();
// Headers in A1:D1
cells.Get(0, 0).PutValue(U16String("Category"));
cells.Get(0, 1).PutValue(U16String("Item"));
cells.Get(0, 2).PutValue(U16String("Year"));
cells.Get(0, 3).PutValue(U16String("Amount"));
// Data rows A2:D9 using nested loops branching on j
for (int i = 1; i <= 8; i++)
{
for (int j = 0; j < 4; j++)
{
switch (j)
{
case 0:
cells.Get(i, j).PutValue(U16String(i <= 4 ? "Fruit" : "Vegetable"));
break;
case 1:
if (i == 1 || i == 2) cells.Get(i, j).PutValue(U16String("Apple"));
else if (i == 3 || i == 4) cells.Get(i, j).PutValue(U16String("Banana"));
else if (i == 5 || i == 6) cells.Get(i, j).PutValue(U16String("Carrot"));
else cells.Get(i, j).PutValue(U16String("Daikon"));
break;
case 2:
cells.Get(i, j).PutValue(2020 + ((i - 1) % 2));
break;
case 3:
if (i == 1) cells.Get(i, j).PutValue(100);
else if (i == 2) cells.Get(i, j).PutValue(150);
else if (i == 3) cells.Get(i, j).PutValue(80);
else if (i == 4) cells.Get(i, j).PutValue(90);
else if (i == 5) cells.Get(i, j).PutValue(50);
else if (i == 6) cells.Get(i, j).PutValue(60);
else if (i == 7) cells.Get(i, j).PutValue(40);
else cells.Get(i, j).PutValue(45);
break;
}
}
}
// Add pivot table at F3 with name PivotTable1
int pivotIndex = worksheet.GetPivotTables().Add(u"A1:D9", u"F3", u"PivotTable1", true, false);
PivotTable pivotTable = worksheet.GetPivotTables().Get(pivotIndex);
// Pivot layout: Category and Item on Row, Year on Column, Amount as data field
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Category");
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Item");
pivotTable.AddFieldToArea(PivotFieldType::Column, u"Year");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
pivotTable.CalculateData();
workbook.Save(u"output_drag.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
Chaque champ placé dans la zone de données est encapsulé en interne sous forme d’instance PivotField, et sa propriété Function renvoie une valeur de l’énumération ConsolidationFunction. Le même setter Function vous permet de basculer entre les agrégats disponibles, notamment Sum, Count, Average, Max, Min, Product, StdDev, StdDevp, Var, et Varp.
Function n’affecte que l’agrégat, la colonne source ne change pas.
Vous pouvez donc conserver un champ de données en tant que Sum tout en ajoutant un second champ de données qui cible la même colonne source mais utilise Count ou Average, le tout dans un seul tableau croisé dynamique.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
Workbook workbook;
Worksheet ws = workbook.GetWorksheets().Get(0);
ws->SetName("Data");
Vector<String> headers{ "Category", "Item", "Year", "Amount" };
for (int j = 0; j < 4; j++) ws->GetCells()->Get(0, j)->PutValue(headers[j]);
Vector<Vector<Object*>> data;
// Fill data ...
int pivotIndex = ws->GetPivotTables()->Add("A1:D9", "F3", "PivotTable1", true, false);
PivotTable pivotTable = ws.GetPivotTables().Get(pivotIndex);
pivotTable->AddFieldToArea(PivotFieldType::Row, "Category");
pivotTable->AddFieldToArea(PivotFieldType::Row, "Item");
pivotTable->AddFieldToArea(PivotFieldType::Column, "Year");
pivotTable->AddFieldToArea(PivotFieldType::Data, "Amount");
pivotTable->AddFieldToArea(PivotFieldType::Data, "Amount");
PivotField countField = pivotTable.GetDataFields().Get(1);
countField->SetFunction(ConsolidationFunction_Count);
pivotTable->CalculateData();
workbook->Save("output_function.xlsx");
}
Lorsqu’un tableau croisé dynamique contient deux champs de données ou plus, Aspose.Cells expose un champ virtuel supplémentaire appelé PivotTable.ValuesField. Ce champ virtuel représente l’agrégat de chaque champ de données résidant dans la zone de données. Vous pouvez le faire glisser dans la zone Ligne ou Colonne en tant que champ croisé dynamique de base, ce qui est utile pour disposer plusieurs mesures côte à côte.
PivotTable.ValuesField ne fonctionne pas s’il n’y a aucun ou un seul champ de valeur.
Les scénarios ci-dessous présentent trois exemples de bout en bout qui illustrent chaque capacité décrite ci-dessus sur la même structure de tableau croisé dynamique.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
Workbook workbook;
Worksheet ws = workbook.GetWorksheets().Get(0);
ws->SetName("Data");
// ... build data ...
int pivotIndex = ws->GetPivotTables()->Add("A1:D9", "F3", "PivotTable1", true, false);
PivotTable pivotTable = ws.GetPivotTables().Get(pivotIndex);
pivotTable->AddFieldToArea(PivotFieldType::Row, "Category");
pivotTable->AddFieldToArea(PivotFieldType::Row, "Item");
pivotTable->AddFieldToArea(PivotFieldType::Column, "Year");
pivotTable->AddFieldToArea(PivotFieldType::Data, "Amount");
pivotTable->AddFieldToArea(PivotFieldType::Data, "Amount");
pivotTable->GetDataFields()->Get(1)->SetFunction(ConsolidationFunction_Count);
pivotTable->AddFieldToArea(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.