Sorting
Preset Info
@univerjs/preset-sheets-sort
Server Required
No
Sorting functionality allows users to sort data in spreadsheets for better organization and analysis. It supports various sorting methods, including ascending, descending, and custom sorting, helping users quickly find the data they need.
Preset Mode
Installation
npm install @univerjs/preset-sheets-sort
Usage
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'
import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'
import { UniverSheetsSortPreset } from '@univerjs/preset-sheets-sort'
import UniverPresetSheetsSortEnUS from '@univerjs/preset-sheets-sort/locales/en-US'
import { createUniver, LocaleType, merge } from '@univerjs/presets'
import '@univerjs/preset-sheets-core/lib/index.css'
import '@univerjs/preset-sheets-sort/lib/index.css'
const { univerAPI } = createUniver({
locale: LocaleType.En_US,
locales: {
[LocaleType.En_US]: merge(
{},
UniverPresetSheetsCoreEnUS,
UniverPresetSheetsSortEnUS,
),
},
presets: [
UniverSheetsCorePreset(),
UniverSheetsSortPreset(),
],
})
Plugin Mode
Installation
npm install @univerjs/sheets-sort @univerjs/sheets-sort-ui
Usage
import { LocaleType, merge, Univer } from '@univerjs/core'
import { UniverSheetsSortPlugin } from '@univerjs/sheets-sort'
import { UniverSheetsSortUIPlugin } from '@univerjs/sheets-sort-ui'
import SheetsSortUIEnUS from '@univerjs/sheets-sort-ui/locale/en-US'
import '@univerjs/sheets-sort-ui/lib/index.css'
import '@univerjs/sheets-sort/facade'
const univer = new Univer({
locale: LocaleType.En_US,
locales: {
[LocaleType.En_US]: merge(
{},
SheetsSortUIEnUS,
),
},
})
univer.registerPlugin(UniverSheetsSortPlugin)
univer.registerPlugin(UniverSheetsSortUIPlugin)
Facade API
Complete Facade API type definitions can be found in the FacadeAPI.
Worksheet Sorting
FWorksheet.sort(colIndex, asc)
method can sort the worksheet by the specified column.
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()
// Sorts the worksheet in ascending order based on the first column
fWorksheet.sort(0)
// Sorts the worksheet in descending order based on the first column
fWorksheet.sort(0, false)
Range Sorting
FRange.sort(column)
method can sort the cells in a specified range by the given column and order.
const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()
const fRange = fWorksheet.getRange('D1:G10')
// Sorts the range in ascending order based on the first column
fRange.sort(0)
// Sorts the range in descending order based on the first column
fRange.sort({ column: 0, ascending: false })
// Sorts the range in descending order based on the first column and ascending order based on the second column
fRange.sort([{ column: 0, ascending: false }, 1])
Event Listeners
Complete event type definitions can be found in the Events.
SheetRangeSorted
event is triggered after the cell range sorting is completed.
const disposable = univerAPI.addEvent(univerAPI.Event.SheetRangeSorted, (params) => {
const { workbook, worksheet, range, sortColumn } = params
})
// Remove event listener using `disposable.dispose()`
SheetBeforeRangeSort
event is triggered before the cell range sorting.
const disposable = univerAPI.addEvent(univerAPI.Event.SheetBeforeRangeSort, (params) => {
const { workbook, worksheet, range, sortColumn } = params
// Cancel the sorting operation
params.cancel = true
})
// Remove event listener using `disposable.dispose()`
How is this guide?