Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat: add sort facade API (#4312)

YuHong38be2cf6a3e5
diff --git a/packages/sheets-sort/src/facade/f-range.ts b/packages/sheets-sort/src/facade/f-range.ts
new file mode 100644
index 0000000000..c144748bb3
--- /dev/null
+++ b/packages/sheets-sort/src/facade/f-range.ts
@@ -0,0 +1,61 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IOrderRule } from '@univerjs/sheets-sort';
+import { SortRangeCommand, SortType } from '@univerjs/sheets-sort';
+import { FRange } from '@univerjs/sheets/facade';
+
+export type SortColumnSpec = { column: number; ascending: boolean } | number;
+
+export interface IFRangeSort {
+ /**
+ * Sort the range by the specified column(s) with specified order.
+ *
+ * @async
+ *
+ * @return The range itself to chain other operations.
+ */
+ sort(column: SortColumnSpec | SortColumnSpec[]): Promise<FRange>;
+}
+
+export class FRangeSort extends FRange implements IFRangeSort {
+ override async sort(column: SortColumnSpec | SortColumnSpec[]): Promise<FRange> {
+ const columns = Array.isArray(column) ? column : [column];
+ const orderRules: IOrderRule[] = columns.map((c) => {
+ if (typeof c === 'number') {
+ return { colIndex: c, type: SortType.ASC };
+ }
+ return {
+ colIndex: c.column,
+ type: c.ascending ? SortType.ASC : SortType.DESC,
+ };
+ });
+ await this._commandService.executeCommand(SortRangeCommand.id, {
+ orderRules,
+ range: this._range,
+ hasTitle: false,
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ });
+ return this;
+ }
+}
+
+FRange.extend(FRangeSort);
+declare module '@univerjs/sheets/facade' {
+ // eslint-disable-next-line ts/naming-convention
+ interface FRange extends IFRangeSort {}
+}
diff --git a/packages/sheets-sort/src/facade/f-worksheet.ts b/packages/sheets-sort/src/facade/f-worksheet.ts
new file mode 100644
index 0000000000..746fb52d8e
--- /dev/null
+++ b/packages/sheets-sort/src/facade/f-worksheet.ts
@@ -0,0 +1,65 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IOrderRule } from '@univerjs/sheets-sort';
+import { type IRange, RANGE_TYPE } from '@univerjs/core';
+
+import { SortRangeCommand, SortType } from '@univerjs/sheets-sort';
+import { FWorksheet } from '@univerjs/sheets/facade';
+
+export interface IFWorksheetSort {
+ /**
+ * Sort the worksheet by the specified column.
+ *
+ * @async
+ *
+ * @param colIndex The column index to sort by.
+ * @param asc Whether to sort in ascending order. Default is `true`.
+ * @returns The worksheet itself to chain calls.
+ */
+ sort(colIndex: number, asc?: boolean): Promise<FWorksheet>;
+}
+
+export class FWorksheetSort extends FWorksheet implements IFWorksheetSort {
+ override async sort(colIndex: number, asc = true): Promise<FWorksheet> {
+ const orderRules: IOrderRule[] = [{
+ colIndex,
+ type: asc ? SortType.ASC : SortType.DESC,
+ }];
+
+ const range: IRange = {
+ startRow: 0,
+ startColumn: 0,
+ endRow: this._worksheet.getRowCount() - 1,
+ endColumn: this._worksheet.getColumnCount() - 1,
+ rangeType: RANGE_TYPE.ALL,
+ };
+ await this._commandService.executeCommand(SortRangeCommand.id, {
+ orderRules,
+ range,
+ hasTitle: false,
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ });
+ return this;
+ }
+}
+
+FWorksheet.extend(FWorksheetSort);
+declare module '@univerjs/sheets/facade' {
+ // eslint-disable-next-line ts/naming-convention
+ interface FWorksheet extends IFWorksheetSort {}
+}
diff --git a/packages/sheets-sort/src/facade/index.ts b/packages/sheets-sort/src/facade/index.ts
new file mode 100644
index 0000000000..30ebc808e8
--- /dev/null
+++ b/packages/sheets-sort/src/facade/index.ts
@@ -0,0 +1,21 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import './f-range';
+import './f-worksheet';
+
+export type * from './f-range';
+export type * from './f-worksheet';