在包含自定义排序列表的列中排序数据
Contents
[
Hide
]
可能的使用场景
你可以使用自定义列表对列中的数据进行排序。这可以通过DataSorter.addKey方法实现。但此方法仅适用于自定义列表中的项不含逗号的情况。如果项中含有逗号,比如"USA,US"、“China,CN"等,你必须使用**DataSorter.addKey(number, SortOrder, string[])**)方法。此方法的最后一个参数不是字符串,而是字符串数组。
使用自定义排序列表对列中的数据进行排序
以下示例代码说明如何使用**DataSorter.addKey(number, SortOrder, string[])**)方法,通过自定义排序列表对数据进行排序。请查看此代码中使用的示例Excel文件以及由此生成的输出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
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
const AsposeCells = require("aspose.cells.node"); | |
//Load the source Excel file | |
var wb = new AsposeCells.Workbook("sampleSortData_CustomSortList.xlsx"); | |
//Access first worksheet | |
var ws = wb.getWorksheets().get(0); | |
//Specify cell area - sort from A1 to A40 | |
var ca = AsposeCells.CellArea.createCellArea("A1", "A40"); | |
//Create Custom Sort list | |
var customSortList = ["USA,US", "Brazil,BR", "China,CN", "Russia,RU", "Canada,CA" ]; | |
//Add Key for Column A, Sort it in Ascending Order with Custom Sort List | |
wb.getDataSorter().addKey(0, AsposeCells.SortOrder.Ascending, customSortList); | |
wb.getDataSorter().sort(ws.getCells(), ca); | |
//Save the output Excel file | |
wb.save("outputSortData_CustomSortList.xlsx"); |