数据透视表隐藏和排序数据
Contents
[
Hide
]
数据透视表隐藏和排序数据
Aspose.Cells for Node.js via C++支持在数据透视表中隐藏和排序数据。以下代码示例展示了如何按降序排序Score列,并隐藏分数低于60的行。
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"); | |
//directories | |
var sourceDir = RunExamples.Get_SourceDirectory(); | |
var outputDir = RunExamples.Get_OutputDirectory(); | |
var workbook = new AsposeCells.Workbook(sourceDir + "PivotTableHideAndSortSample.xlsx"); | |
var worksheet = workbook.getWorksheets().get(0); | |
var pivotTable = worksheet.getPivotTables().get(0); | |
var dataBodyRange = pivotTable.getDataBodyRange(); | |
var currentRow = 3; | |
var rowsUsed = dataBodyRange.endRow; | |
//Sorting score in descending | |
var field = pivotTable.getRowFields().get(0); | |
field.setIsAutoSort(true); | |
field.setIsAscendSort(false); | |
field.setAutoSortField(0); | |
pivotTable.refreshData(); | |
pivotTable.calculateData(); | |
//Hiding rows with score less than 60 | |
while (currentRow < rowsUsed) | |
{ | |
var cell = worksheet.getCells().get(currentRow, 1); | |
var score = cell.getFloatValue(); | |
if (score < 60) | |
{ | |
worksheet.getCells().hideRow(currentRow); | |
} | |
currentRow = currentRow + 1; | |
} | |
pivotTable.refreshData(); | |
pivotTable.calculateData(); | |
//Saving the Excel file | |
workbook.save(outputDir + "PivotTableHideAndSort_out.xlsx"); |
示例代码中使用的源和输出Excel文件已附上供参考。