Raggruppa i campi pivot nella tabella pivot
Possibili Scenari di Utilizzo
Microsoft Excel ti permette di raggruppare campi pivot della tabella pivot. Quando ci sono molti dati correlati a un campo pivot, è spesso utile raggrupparli in sezioni. Aspose.Cells for Node.js via C++ supporta anche questa funzione usando il metodo PivotTable.groupBy().
Come raggruppare i campi pivot nella tabella pivot
Il codice di esempio seguente carica il file Excel di esempio e esegue il raggruppamento sul primo campo pivot utilizzando il metodo PivotTable.groupBy(). Aggiorna quindi e calcola i dati della tabella pivot e salva il foglio di calcolo come file Excel di output. Lo screenshot mostra l’effetto del codice di esempio sul file Excel di esempio. Come si può vedere dallo screenshot, il primo campo pivot è ora raggruppato per mesi e trimestri.
Codice di Esempio
const AsposeCells = require("aspose.cells.node"); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Load sample workbook | |
var wb = new AsposeCells.Workbook("sampleGroupPivotFieldsInPivotTable.xlsx"); | |
//Access the second worksheet | |
var ws = wb.getWorksheets().get(1); | |
//Access the pivot table | |
var pt = ws.getPivotTables().get(0); | |
//Specify the start and end date time | |
var dtStart = new Date(2008, 1, 1); | |
var dtEnd = new Date(2008, 9, 5); | |
//Specify the group type list, we want to group by months and quarters | |
var groupTypeList = [AsposeCells.PivotGroupByType.Months, AsposeCells.PivotGroupByType.Quarters]; | |
//Apply the grouping on first pivot field | |
var field = pt.getRowFields().get(0); | |
field.groupBy(dtStart, dtEnd, groupTypeList, 1, true); | |
//Refresh and calculate pivot table | |
pt.setRefreshDataFlag(true); | |
pt.refreshData(); | |
pt.calculateData(); | |
pt.setRefreshDataFlag(false); | |
//Save the output Excel file | |
wb.save("outputGroupPivotFieldsInPivotTable.xlsx"); |