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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(facade): add facade for freeze and getCellData (#3561)
zhangw983ad9f48c2e
diff --git a/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts b/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.tsindex ca0c5296bc..c5c9af0fda 100644--- a/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts+++ b/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts@@ -14,13 +14,14 @@* limitations under the License.*/-import type { ICellData, Injector, IStyleData, Nullable, Workbook } from '@univerjs/core';import { ICommandService, IUniverInstanceService, RANGE_TYPE, UniverInstanceType } from '@univerjs/core';-import { AddWorksheetMergeCommand, AddWorksheetMergeMutation, InsertColCommand, InsertColMutation, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, RemoveWorksheetMergeCommand, RemoveWorksheetMergeMutation, SetColHiddenCommand, SetColHiddenMutation, SetColVisibleMutation, SetColWidthCommand, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetRowHeightCommand, SetRowHiddenCommand, SetRowHiddenMutation, SetRowVisibleMutation, SetSelectionsOperation, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetColWidthMutation, SetWorksheetRowHeightMutation, SetWorksheetRowIsAutoHeightCommand, SetWorksheetRowIsAutoHeightMutation, SheetsSelectionsService } from '@univerjs/sheets';+import { AddWorksheetMergeCommand, AddWorksheetMergeMutation, InsertColCommand, InsertColMutation, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, RemoveWorksheetMergeCommand, RemoveWorksheetMergeMutation, SetColHiddenCommand, SetColHiddenMutation, SetColVisibleMutation, SetColWidthCommand, SetFrozenCommand, SetFrozenMutation, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetRowHeightCommand, SetRowHiddenCommand, SetRowHiddenMutation, SetRowVisibleMutation, SetSelectionsOperation, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetColWidthMutation, SetWorksheetRowHeightMutation, SetWorksheetRowIsAutoHeightCommand, SetWorksheetRowIsAutoHeightMutation, SheetsSelectionsService } from '@univerjs/sheets';+import { CancelFrozenCommand } from '@univerjs/sheets-ui';import { beforeEach, describe, expect, it } from 'vitest';-import type { FUniver } from '../../facade';+import type { ICellData, Injector, IStyleData, Nullable, Workbook } from '@univerjs/core';import { createWorksheetTestBed } from './create-worksheet-test-bed';+import type { FUniver } from '../../facade';describe('Test FWorksheet', () => {let get: Injector['get'];@@ -90,6 +91,11 @@ describe('Test FWorksheet', () => {commandService.registerCommand(RemoveWorksheetMergeCommand);commandService.registerCommand(RemoveWorksheetMergeMutation);+ // freeze+ commandService.registerCommand(SetFrozenCommand);+ commandService.registerCommand(SetFrozenMutation);+ commandService.registerCommand(CancelFrozenCommand);+getValueByPosition = (startRow: number,startColumn: number,@@ -487,5 +493,57 @@ describe('Test FWorksheet', () => {expect(currentColWidth2).toBe(100);});+ it('Worksheet freeze', async () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();++ const freeze = activeSheet?.getFreeze();+ expect(freeze).toEqual({ startRow: -1, startColumn: -1, xSplit: 0, ySplit: 0 });++ activeSheet?.setFreeze({ startRow: 1, startColumn: 1, xSplit: 1, ySplit: 1 });+ const freeze2 = activeSheet?.getFreeze();+ expect(freeze2).toEqual({ startRow: 1, startColumn: 1, xSplit: 1, ySplit: 1 });++ activeSheet?.cancelFreeze();+ const freeze3 = activeSheet?.getFreeze();+ expect(freeze3).toEqual({ startRow: -1, startColumn: -1, xSplit: 0, ySplit: 0 });+ });++ it('Worksheet setFrozenColumns and getFrozenColumns', () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();++ activeSheet?.setFrozenColumns(2);+ expect(activeSheet?.getFrozenColumns()).toBe(2);++ const freeze = activeSheet?.getFreeze();+ expect(freeze).toEqual({ startRow: -1, startColumn: 2, xSplit: 2, ySplit: 0 });+ });++ it('Worksheet setFrozenRows and getFrozenRows', () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();++ activeSheet?.setFrozenRows(3);+ expect(activeSheet?.getFrozenRows()).toBe(3);++ const freeze = activeSheet?.getFreeze();+ expect(freeze).toEqual({ startRow: 3, startColumn: -1, xSplit: 0, ySplit: 3 });+ });++ it('Worksheet combined frozen rows and columns', () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();++ activeSheet?.setFrozenColumns(2);+ activeSheet?.setFrozenRows(3);++ expect(activeSheet?.getFrozenColumns()).toBe(2);+ expect(activeSheet?.getFrozenRows()).toBe(3);++ const freeze = activeSheet?.getFreeze();+ expect(freeze).toEqual({ startRow: 3, startColumn: 2, xSplit: 2, ySplit: 3 });++ activeSheet?.setFreeze({ startRow: 21, startColumn: 9, ySplit: 16, xSplit: 4 });+ expect(activeSheet?.getFrozenRows()).toBe(21);+ expect(activeSheet?.getFrozenColumns()).toBe(9);+ });+// #endregion});diff --git a/packages/facade/src/apis/sheets/f-range.ts b/packages/facade/src/apis/sheets/f-range.tsindex c3c732a605..0223292d48 100644--- a/packages/facade/src/apis/sheets/f-range.ts+++ b/packages/facade/src/apis/sheets/f-range.ts@@ -255,6 +255,24 @@ export class FRange {return range;}+ /**+ * Returns the cell data for the cells in the range.+ * @returns A two-dimensional array of cell data.+ */+ getCellDataGrid(): Nullable<ICellData>[][] {+ const { startRow, endRow, startColumn, endColumn } = this._range;+ const range: Nullable<ICellData>[][] = [];++ for (let r = startRow; r <= endRow; r++) {+ const row: Nullable<ICellData>[] = [];+ for (let c = startColumn; c <= endColumn; c++) {+ row.push(this._worksheet.getCellRaw(r, c));+ }+ range.push(row);+ }+ return range;+ }+/*** Returns the formulas (A1 notation) for the cells in the range. Entries in the 2D array are empty strings for cells with no formula.* @returns A two-dimensional array of formulas in string format.diff --git a/packages/facade/src/apis/sheets/f-workbook.ts b/packages/facade/src/apis/sheets/f-workbook.tsindex d03b5f393e..1a15e07539 100644--- a/packages/facade/src/apis/sheets/f-workbook.ts+++ b/packages/facade/src/apis/sheets/f-workbook.ts@@ -263,7 +263,13 @@ export class FWorkbook {*/openDialog(dialog: IDialogPartMethodOptions): IDisposable {const dialogService = this._injector.get(IDialogService);- return dialogService.open(dialog);+ const disposable = dialogService.open({+ ...dialog,+ onClose: () => {+ disposable.dispose();+ },+ });+ return disposable;}// #region callbacksdiff --git a/packages/facade/src/apis/sheets/f-worksheet.ts b/packages/facade/src/apis/sheets/f-worksheet.tsindex 2759b60b44..f3cd571847 100644--- a/packages/facade/src/apis/sheets/f-worksheet.ts+++ b/packages/facade/src/apis/sheets/f-worksheet.ts@@ -16,14 +16,15 @@import { Direction, ICommandService, Inject, Injector, RANGE_TYPE } from '@univerjs/core';import { deserializeRangeWithSheet } from '@univerjs/engine-formula';-import { copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColHiddenCommand, SetColWidthCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService } from '@univerjs/sheets';+import { copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService } from '@univerjs/sheets';import { DataValidationModel, SheetsDataValidationValidatorService } from '@univerjs/sheets-data-validation';import { SheetCanvasFloatDomManagerService } from '@univerjs/sheets-drawing-ui';import { SheetsFilterService } from '@univerjs/sheets-filter';import { SheetsThreadCommentModel } from '@univerjs/sheets-thread-comment';+import { CancelFrozenCommand } from '@univerjs/sheets-ui';import { ComponentManager } from '@univerjs/ui';-import type { IRange, Nullable, ObjectMatrix, Workbook, Worksheet } from '@univerjs/core';+import type { IFreeze, IRange, Nullable, ObjectMatrix, Workbook, Worksheet } from '@univerjs/core';import type { IDataValidationResCache } from '@univerjs/sheets-data-validation';import type { FilterModel } from '@univerjs/sheets-filter';import { FDataValidation } from './f-data-validation';@@ -947,4 +948,90 @@ export class FWorksheet {* @param range The range to set as the active selection.*/setActiveSelection = this.setActiveRange;++ /**+ * Sets the frozen state of the current sheet.+ * @param freeze - The freeze object containing the parameters for freezing the sheet.+ * @returns True if the command was successful, false otherwise.+ */+ setFreeze(freeze: IFreeze): boolean {+ return this._commandService.syncExecuteCommand(SetFrozenCommand.id, {+ ...freeze,+ unitId: this._workbook.getUnitId(),+ subUnitId: this.getSheetId(),+ });+ }++ /**+ * Cancels the frozen state of the current sheet.+ * @returns True if the command was successful, false otherwise.+ */+ cancelFreeze(): boolean {+ return this._commandService.syncExecuteCommand(CancelFrozenCommand.id, {+ unitId: this._workbook.getUnitId(),+ subUnitId: this.getSheetId(),+ });+ }++ /**+ * Get the freeze state of the current sheet.+ * @returns The freeze state of the current sheet.+ */+ getFreeze(): IFreeze {+ return this._worksheet.getFreeze();+ }++ /**+ * Set the number of frozen columns.+ * @param columns The number of columns to freeze.+ * To unfreeze all columns, set this value to 0.+ */+ setFrozenColumns(columns: number): void {+ const currentFreeze = this.getFreeze();+ this.setFreeze({+ ...currentFreeze,+ startColumn: columns > 0 ? columns : -1,+ xSplit: columns,+ });+ }++ /**+ * Set the number of frozen rows.+ * @param rows The number of rows to freeze.+ * To unfreeze all rows, set this value to 0.+ */+ setFrozenRows(rows: number): void {+ const currentFreeze = this.getFreeze();+ this.setFreeze({+ ...currentFreeze,+ startRow: rows > 0 ? rows : -1,+ ySplit: rows,+ });+ }++ /**+ * Get the number of frozen columns.+ * @returns The number of frozen columns.+ * Returns 0 if no columns are frozen.+ */+ getFrozenColumns(): number {+ const freeze = this.getFreeze();+ if (freeze.startColumn === -1) {+ return 0;+ }+ return freeze.startColumn;+ }++ /**+ * Get the number of frozen rows.+ * @returns The number of frozen rows.+ * Returns 0 if no rows are frozen.+ */+ getFrozenRows(): number {+ const freeze = this.getFreeze();+ if (freeze.startRow === -1) {+ return 0;+ }+ return freeze.startRow;+ }}diff --git a/packages/sheets-ui/src/commands/commands/set-frozen.command.ts b/packages/sheets-ui/src/commands/commands/set-frozen.command.tsindex 8f802aa18c..9dfb4a2c87 100644--- a/packages/sheets-ui/src/commands/commands/set-frozen.command.ts+++ b/packages/sheets-ui/src/commands/commands/set-frozen.command.ts@@ -14,12 +14,12 @@* limitations under the License.*/-import type { ICommand } from '@univerjs/core';import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, RANGE_TYPE } from '@univerjs/core';-import type { ISetFrozenMutationParams } from '@univerjs/sheets';+import { IRenderManagerService } from '@univerjs/engine-render';import { getSheetCommandTarget, SetFrozenMutation, SetFrozenMutationFactory, SheetsSelectionsService } from '@univerjs/sheets';+import type { ICommand } from '@univerjs/core';-import { IRenderManagerService } from '@univerjs/engine-render';+import type { ISetFrozenMutationParams } from '@univerjs/sheets';import { SheetScrollManagerService } from '../../services/scroll-manager.service';export enum SetSelectionFrozenType {@@ -132,14 +132,19 @@ export const SetColumnFrozenCommand: ICommand = {},};-export const CancelFrozenCommand: ICommand = {+export interface ICancelFrozenCommandParams {+ unitId?: string;+ subUnitId?: string;+}++export const CancelFrozenCommand: ICommand<ICancelFrozenCommandParams> = {type: CommandType.COMMAND,id: 'sheet.command.cancel-frozen',- handler: async (accessor) => {+ handler: (accessor, params) => {const commandService = accessor.get(ICommandService);const univerInstanceService = accessor.get(IUniverInstanceService);const undoRedoService = accessor.get(IUndoRedoService);- const target = getSheetCommandTarget(univerInstanceService);+ const target = getSheetCommandTarget(univerInstanceService, { unitId: params?.unitId, subUnitId: params?.subUnitId });if (!target) return false;const { unitId, subUnitId } = target;diff --git a/packages/sheets/src/commands/commands/set-frozen-cancel.command.ts b/packages/sheets/src/commands/commands/set-frozen-cancel.command.tsindex 9c22408453..0f82ec608b 100644--- a/packages/sheets/src/commands/commands/set-frozen-cancel.command.ts+++ b/packages/sheets/src/commands/commands/set-frozen-cancel.command.ts@@ -14,17 +14,17 @@* limitations under the License.*/-import type { IAccessor, ICommand } from '@univerjs/core';import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';+import type { IAccessor, ICommand } from '@univerjs/core';-import type { ISetFrozenMutationParams } from '../mutations/set-frozen.mutation';import { SetFrozenMutation, SetFrozenMutationFactory } from '../mutations/set-frozen.mutation';import { getSheetCommandTarget } from './utils/target-util';+import type { ISetFrozenMutationParams } from '../mutations/set-frozen.mutation';export const SetFrozenCancelCommand: ICommand = {type: CommandType.COMMAND,id: 'sheet.command.set-frozen-cancel',- handler: async (accessor: IAccessor) => {+ handler: (accessor: IAccessor) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);diff --git a/packages/sheets/src/commands/commands/set-frozen.command.ts b/packages/sheets/src/commands/commands/set-frozen.command.tsindex 00e2a90b3a..3aad298bf5 100644--- a/packages/sheets/src/commands/commands/set-frozen.command.ts+++ b/packages/sheets/src/commands/commands/set-frozen.command.ts@@ -14,29 +14,31 @@* limitations under the License.*/-import type { IAccessor, ICommand } from '@univerjs/core';import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';+import type { IAccessor, ICommand } from '@univerjs/core';-import type { ISetFrozenMutationParams } from '../mutations/set-frozen.mutation';import { SetFrozenMutation, SetFrozenMutationFactory } from '../mutations/set-frozen.mutation';import { getSheetCommandTarget } from './utils/target-util';+import type { ISetFrozenMutationParams } from '../mutations/set-frozen.mutation';interface ISetFrozenCommandParams {startRow: number;startColumn: number;ySplit: number;xSplit: number;+ unitId?: string;+ subUnitId?: string;}export const SetFrozenCommand: ICommand = {type: CommandType.COMMAND,id: 'sheet.command.set-frozen',- handler: async (accessor: IAccessor, params: ISetFrozenCommandParams) => {+ handler: (accessor: IAccessor, params: ISetFrozenCommandParams) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);- const target = getSheetCommandTarget(univerInstanceService);+ const target = getSheetCommandTarget(univerInstanceService, { unitId: params.unitId, subUnitId: params.subUnitId });if (!target) return false;const { unitId, subUnitId, worksheet } = target;