Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
El método PivotTable.AddFieldToArea(PivotFieldType fieldType, U16String fieldName) mueve un campo base desde los datos de origen a una de las cuatro regiones de la tabla dinámica. El argumento fieldType acepta uno de los siguientes valores de PivotFieldType.
Row — campos colocados verticalmente a la izquierdaColumn — campos colocados horizontalmente en la parte superiorData — campos cuyos valores se agreganPage — campos usados como filtros del informe
El orden de anidamiento de los campos es importante. Agregar Category a la región de fila primero y luego Item produce una tabla dinámica cuya agrupación externa es Category y cuya agrupación interna es Item. Invertir el orden invierte la jerarquía.El método PivotField.SetSubtotals(PivotFieldSubtotalType subtotalType, bool shown) controla qué filas de subtotal aparecen para un campo dinámico. Cada llamada activa o desactiva de forma independiente un único tipo de subtotal. Pasar shown = true muestra el subtotal, mientras que shown = false lo oculta. Dado que cada llamada solo afecta a un tipo, llamar al método varias veces con diferentes valores de subtotalType construye un subconjunto personalizado de subtotales.
La enumeración PivotFieldSubtotalType define los tipos de subtotales disponibles.
Automatic — Aspose.Cells elige la selección predeterminada (normalmente Sum para campos numéricos)None — suprime todas las filas de subtotalSumCountAverageMaxMinProductStdDevStdDevpVarVarpSetSubtotals no tienen ningún efecto visible en ese caso. Por lo tanto, este artículo coloca dos campos de fila (Category externo, Item interno) en cada ejemplo para que el límite de subtotal entre cada grupo Category sea visible.
Cuando no llama en absoluto a SetSubtotals, Aspose.Cells aplica la selección Automatic a los campos numéricos. El siguiente ejemplo confirma explícitamente este comportamiento llamando a SetSubtotals(PivotFieldSubtotalType.Automatic, true) en el campo de fila externo Category.
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
Aspose::Cells::Startup();
Workbook workbook;
Worksheet worksheet = workbook.GetWorksheets().Get(0);
worksheet.SetName(u"Data");
worksheet.GetCells().Get(0, 0).PutValue(u"Category");
worksheet.GetCells().Get(0, 1).PutValue(u"Item");
worksheet.GetCells().Get(0, 2).PutValue(u"Year");
worksheet.GetCells().Get(0, 3).PutValue(u"Amount");
worksheet.GetCells().Get(1, 0).PutValue(u"Fruit");
worksheet.GetCells().Get(1, 1).PutValue(u"Apple");
worksheet.GetCells().Get(1, 2).PutValue(2020);
worksheet.GetCells().Get(1, 3).PutValue(100);
worksheet.GetCells().Get(2, 0).PutValue(u"Fruit");
worksheet.GetCells().Get(2, 1).PutValue(u"Apple");
worksheet.GetCells().Get(2, 2).PutValue(2021);
worksheet.GetCells().Get(2, 3).PutValue(150);
worksheet.GetCells().Get(3, 0).PutValue(u"Fruit");
worksheet.GetCells().Get(3, 1).PutValue(u"Banana");
worksheet.GetCells().Get(3, 2).PutValue(2020);
worksheet.GetCells().Get(3, 3).PutValue(80);
worksheet.GetCells().Get(4, 0).PutValue(u"Fruit");
worksheet.GetCells().Get(4, 1).PutValue(u"Banana");
worksheet.GetCells().Get(4, 2).PutValue(2021);
worksheet.GetCells().Get(4, 3).PutValue(90);
worksheet.GetCells().Get(5, 0).PutValue(u"Vegetable");
worksheet.GetCells().Get(5, 1).PutValue(u"Carrot");
worksheet.GetCells().Get(5, 2).PutValue(2020);
worksheet.GetCells().Get(5, 3).PutValue(50);
worksheet.GetCells().Get(6, 0).PutValue(u"Vegetable");
worksheet.GetCells().Get(6, 1).PutValue(u"Carrot");
worksheet.GetCells().Get(6, 2).PutValue(2021);
worksheet.GetCells().Get(6, 3).PutValue(60);
worksheet.GetCells().Get(7, 0).PutValue(u"Vegetable");
worksheet.GetCells().Get(7, 1).PutValue(u"Daikon");
worksheet.GetCells().Get(7, 2).PutValue(2020);
worksheet.GetCells().Get(7, 3).PutValue(40);
worksheet.GetCells().Get(8, 0).PutValue(u"Vegetable");
worksheet.GetCells().Get(8, 1).PutValue(u"Daikon");
worksheet.GetCells().Get(8, 2).PutValue(2021);
worksheet.GetCells().Get(8, 3).PutValue(45);
int pivotIndex = worksheet.GetPivotTables().Add(u"A1:D9", u"F3", u"PivotTable1");
PivotTable pivotTable = worksheet.GetPivotTables().Get(pivotIndex);
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Category");
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Item");
pivotTable.AddFieldToArea(PivotFieldType::Column, u"Year");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
PivotField categoryField = pivotTable.GetRowFields().Get(0);
categoryField.SetSubtotals(PivotFieldSubtotalType::Automatic, true);
pivotTable.CalculateData();
workbook.Save(u"output_automatic.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
Llamar a SetSubtotals(PivotFieldSubtotalType.None, true) elimina todas las filas de subtotal de la tabla dinámica, dejando solo las filas de campo y el total general al final. Esto es útil cuando desea los datos agrupados sin procesar sin ninguna fila de resumen.
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
Aspose::Cells::Startup();
Workbook wb;
Worksheet sheet = wb.GetWorksheets().Get(0);
sheet.SetName(u"Data");
U16String headers[] = { u"Category", u"Item", u"Year", u"Amount" };
for (int j = 0; j < 4; j++) {
sheet.GetCells().Get(0, j).PutValue(headers[j]);
}
U16String categories[] = { u"Fruit", u"Fruit", u"Fruit", u"Fruit",
u"Vegetable", u"Vegetable", u"Vegetable", u"Vegetable" };
U16String items[] = { u"Apple", u"Apple", u"Banana", u"Banana",
u"Carrot", u"Carrot", u"Daikon", u"Daikon" };
int years[] = { 2020, 2021, 2020, 2021, 2020, 2021, 2020, 2021 };
int amounts[] = { 100, 150, 80, 90, 50, 60, 40, 45 };
for (int i = 0; i < 8; i++) {
sheet.GetCells().Get(i + 1, 0).PutValue(categories[i]);
sheet.GetCells().Get(i + 1, 1).PutValue(items[i]);
sheet.GetCells().Get(i + 1, 2).PutValue(years[i]);
sheet.GetCells().Get(i + 1, 3).PutValue(amounts[i]);
}
int pivotIndex = sheet.GetPivotTables().Add(u"A1:D9", u"F3", u"PivotTable1");
PivotTable pivotTable = sheet.GetPivotTables().Get(pivotIndex);
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Category");
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Item");
pivotTable.AddFieldToArea(PivotFieldType::Column, u"Year");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
PivotField categoryField = pivotTable.GetRowFields().Get(0);
categoryField.SetSubtotals(PivotFieldSubtotalType::None, true);
pivotTable.CalculateData();
wb.Save(u"output_none.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
No está limitado a un único tipo de subtotal. Cada llamada a SetSubtotals opera de forma independiente sobre un tipo, por lo que llamar al método dos veces — una con Sum y otra con Average — produce un subconjunto personalizado de dos filas de subtotal para cada grupo Category.
#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");
worksheet.GetCells().Get(u"A1").PutValue(u"Category");
worksheet.GetCells().Get(u"B1").PutValue(u"Item");
worksheet.GetCells().Get(u"C1").PutValue(u"Year");
worksheet.GetCells().Get(u"D1").PutValue(u"Amount");
worksheet.GetCells().Get(1, 0).PutValue(u"Fruit");
worksheet.GetCells().Get(1, 1).PutValue(u"Apple");
worksheet.GetCells().Get(1, 2).PutValue(2020);
worksheet.GetCells().Get(1, 3).PutValue(100);
worksheet.GetCells().Get(2, 0).PutValue(u"Fruit");
worksheet.GetCells().Get(2, 1).PutValue(u"Apple");
worksheet.GetCells().Get(2, 2).PutValue(2021);
worksheet.GetCells().Get(2, 3).PutValue(150);
worksheet.GetCells().Get(3, 0).PutValue(u"Fruit");
worksheet.GetCells().Get(3, 1).PutValue(u"Banana");
worksheet.GetCells().Get(3, 2).PutValue(2020);
worksheet.GetCells().Get(3, 3).PutValue(80);
worksheet.GetCells().Get(4, 0).PutValue(u"Fruit");
worksheet.GetCells().Get(4, 1).PutValue(u"Banana");
worksheet.GetCells().Get(4, 2).PutValue(2021);
worksheet.GetCells().Get(4, 3).PutValue(90);
worksheet.GetCells().Get(5, 0).PutValue(u"Vegetable");
worksheet.GetCells().Get(5, 1).PutValue(u"Carrot");
worksheet.GetCells().Get(5, 2).PutValue(2020);
worksheet.GetCells().Get(5, 3).PutValue(50);
worksheet.GetCells().Get(6, 0).PutValue(u"Vegetable");
worksheet.GetCells().Get(6, 1).PutValue(u"Carrot");
worksheet.GetCells().Get(6, 2).PutValue(2021);
worksheet.GetCells().Get(6, 3).PutValue(60);
worksheet.GetCells().Get(7, 0).PutValue(u"Vegetable");
worksheet.GetCells().Get(7, 1).PutValue(u"Daikon");
worksheet.GetCells().Get(7, 2).PutValue(2020);
worksheet.GetCells().Get(7, 3).PutValue(40);
worksheet.GetCells().Get(8, 0).PutValue(u"Vegetable");
worksheet.GetCells().Get(8, 1).PutValue(u"Daikon");
worksheet.GetCells().Get(8, 2).PutValue(2021);
worksheet.GetCells().Get(8, 3).PutValue(45);
PivotTableCollection pivotTables = worksheet.GetPivotTables();
int pivotIndex = pivotTables.Add(u"A1:D9", u"F3", u"PivotTable1");
PivotTable pivotTable = pivotTables.Get(pivotIndex);
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Category");
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Item");
pivotTable.AddFieldToArea(PivotFieldType::Column, u"Year");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
PivotField categoryField = pivotTable.GetRowFields().Get(0);
categoryField.SetSubtotals(PivotFieldSubtotalType::Sum, true);
categoryField.SetSubtotals(PivotFieldSubtotalType::Average, true);
pivotTable.CalculateData();
workbook.Save(u"output_custom.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.