在透视表中对透视字段进行分组
Contents
[
Hide
]
可能的使用场景
微软Excel允许对数据透视表的字段进行分组。当有大量与某字段相关的数据时,将它们分成不同的段通常非常有用。Aspose.Cells for Node.js via C++同样支持此功能,使用 PivotTable.groupBy() 方法。
在数据透视表中对字段进行分组
以下示例代码加载示例Excel文件,并使用PivotTable.groupBy()方法对第一个透视字段执行分组。然后刷新和计算透视表的数据,并将工作簿保存为输出Excel文件。屏幕截图显示了示例代码对示例Excel文件的效果。如屏幕截图所示,第一个透视字段现在按月份和季度分组。
示例代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |