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(sheet): add custom for row and column (#3693)
Dushusir4e31b24e6977
diff --git a/packages/core/src/sheets/column-manager.ts b/packages/core/src/sheets/column-manager.tsindex 731949a0da..1bc85fb67b 100644--- a/packages/core/src/sheets/column-manager.ts+++ b/packages/core/src/sheets/column-manager.ts@@ -203,6 +203,14 @@ export class ColumnManager {}}+ /**+ * Remove column data of given column+ * @param columnPos+ */+ removeColumn(columnPos: number) {+ delete this._columnData[columnPos];+ }+/*** get given column data or create a column data when it's null* @param columnPos column index@@ -210,15 +218,11 @@ export class ColumnManager {*/getColumnOrCreate(columnPos: number): Partial<IColumnData> {const { _columnData } = this;- const config = this._config;const column = _columnData[columnPos];if (column) {return column;}- const create = {- w: config.defaultColumnWidth,- hd: BooleanNumber.FALSE,- };+ const create = {};this._columnData[columnPos] = create;return create;}diff --git a/packages/core/src/sheets/row-manager.ts b/packages/core/src/sheets/row-manager.tsindex 4bcc5e115c..ad93b33db4 100644--- a/packages/core/src/sheets/row-manager.ts+++ b/packages/core/src/sheets/row-manager.ts@@ -102,6 +102,14 @@ export class RowManager {return this._rowData[rowPos];}+ /**+ * Remove row data of given row+ * @param rowPos+ */+ removeRow(rowPos: number) {+ delete this._rowData[rowPos];+ }+/*** Get given row data or create a row data when it's null* @param rowPos row index@@ -113,8 +121,8 @@ export class RowManager {if (row) {return row;}- const config = this._config;- const create = { hd: BooleanNumber.FALSE, h: config.defaultRowHeight };++ const create = {};_rowData[rowPos] = create;return create;diff --git a/packages/core/src/sheets/typedef.ts b/packages/core/src/sheets/typedef.tsindex cf07f503bc..acf9972636 100644--- a/packages/core/src/sheets/typedef.ts+++ b/packages/core/src/sheets/typedef.ts@@ -141,6 +141,8 @@ export interface IWorksheetData {rightToLeft: BooleanNumber;}+export type CustomData = Nullable<Record<string, any>>;+/*** Properties of row data*/@@ -169,6 +171,11 @@ export interface IRowData {* style id*/s?: Nullable<IStyleData | string>;++ /**+ * User stored custom fields+ */+ custom?: CustomData;}export interface IRowAutoHeightInfo {@@ -194,6 +201,11 @@ export interface IColumnData {* style id*/s?: Nullable<IStyleData | string>;++ /**+ * User stored custom fields+ */+ custom?: CustomData;}export interface IColAutoWidthInfo {@@ -240,7 +252,7 @@ export interface ICellData {/*** User stored custom fields*/- custom?: Nullable<Record<string, any>>;+ custom?: CustomData;}export interface ICellMarksStyle {diff --git a/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts b/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.tsindex c5c9af0fda..2be1983f2c 100644--- a/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts+++ b/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts@@ -14,14 +14,14 @@* limitations under the License.*/+import type { ICellData, Injector, IStyleData, Nullable, Workbook } from '@univerjs/core';+import type { FUniver } from '../../facade';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, SetFrozenCommand, SetFrozenMutation, 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, SetColDataCommand, SetColDataMutation, SetColHiddenCommand, SetColHiddenMutation, SetColVisibleMutation, SetColWidthCommand, SetFrozenCommand, SetFrozenMutation, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowDataMutation, 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 { 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'];@@ -70,6 +70,10 @@ describe('Test FWorksheet', () => {commandService.registerCommand(SetWorksheetRowHeightMutation);commandService.registerCommand(SetWorksheetRowIsAutoHeightCommand);commandService.registerCommand(SetWorksheetRowIsAutoHeightMutation);+ commandService.registerCommand(SetRowDataCommand);+ commandService.registerCommand(SetRowDataMutation);+ commandService.registerCommand(SetColDataCommand);+ commandService.registerCommand(SetColDataMutation);// columncommandService.registerCommand(InsertColCommand);@@ -349,6 +353,21 @@ describe('Test FWorksheet', () => {expect(currentRowHeight).toBe(200);});+ it('Worksheet setRowCustom', async () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getSheetByName('sheet1');++ const sheet = await activeSheet?.setRowCustom({+ 0: {+ color: 'red',+ },+ });+ expect(sheet).toBeDefined();++ const currentWorksheet = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)?.getActiveSheet();+ const currentRowCustom = currentWorksheet?.getRowManager().getRow(0)?.custom;+ expect(currentRowCustom).toEqual({ color: 'red' });+ });+// #endregion// #region Column@@ -493,6 +512,23 @@ describe('Test FWorksheet', () => {expect(currentColWidth2).toBe(100);});+ it('Worksheet setColumnCustom', async () => {+ const activeSheet = univerAPI.getActiveWorkbook()?.getSheetByName('sheet1');++ const sheet = await activeSheet?.setColumnCustom({+ 0: {+ color: 'red',+ },+ });+ expect(sheet).toBeDefined();++ const currentWorksheet = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)?.getActiveSheet();+ const currentColCustom = currentWorksheet?.getColumnManager().getColumn(0)?.custom;+ expect(currentColCustom).toEqual({ color: 'red' });+ });++ // #endregion+it('Worksheet freeze', async () => {const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();@@ -544,6 +580,4 @@ describe('Test FWorksheet', () => {expect(activeSheet?.getFrozenRows()).toBe(21);expect(activeSheet?.getFrozenColumns()).toBe(9);});-- // #endregion});diff --git a/packages/facade/src/apis/sheets/f-worksheet.ts b/packages/facade/src/apis/sheets/f-worksheet.tsindex da161bb4ef..ac5c8ac1e6 100644--- a/packages/facade/src/apis/sheets/f-worksheet.ts+++ b/packages/facade/src/apis/sheets/f-worksheet.ts@@ -14,16 +14,16 @@* limitations under the License.*/-import type { ICellData, IDisposable, IFreeze, IRange, IStyleData, Nullable, Workbook, Worksheet } from '@univerjs/core';-import type { ISetRangeValuesMutationParams, IToggleGridlinesCommandParams } from '@univerjs/sheets';-import type { IDataValidationResCache } from '@univerjs/sheets-data-validation';+import type { CustomData, ICellData, IColumnData, IDisposable, IFreeze, IObjectArrayPrimitiveType, IRange, IRowData, IStyleData, Nullable, Workbook, Worksheet } from '@univerjs/core';++import type { ISetColDataCommandParams, ISetRangeValuesMutationParams, ISetRowDataCommandParams, ISetWorksheetDefaultStyleMutationParams, IToggleGridlinesCommandParams } from '@univerjs/sheets';import type { FilterModel } from '@univerjs/sheets-filter';import type { FWorkbook, IFICanvasFloatDom } from './f-workbook';import { BooleanNumber, Direction, ICommandService, Inject, Injector, ObjectMatrix, RANGE_TYPE } from '@univerjs/core';import { DataValidationModel } from '@univerjs/data-validation';import { deserializeRangeWithSheet } from '@univerjs/engine-formula';-import { copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetRangeValuesMutation, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetDefaultStyleMutation, SetWorksheetRowColumnStyleMutation, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets';-import { SheetsDataValidationValidatorService } from '@univerjs/sheets-data-validation';+import { copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColDataCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetDefaultStyleCommand, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets';+import { type IDataValidationResCache, 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';@@ -124,15 +124,17 @@ export class FWorksheet {* @param style default style* @returns this worksheet*/- async setDefaultStyle(style: string): Promise<FWorksheet> {+ async setDefaultStyle(style: Nullable<IStyleData>): Promise<FWorksheet> {const unitId = this._workbook.getUnitId();const subUnitId = this._worksheet.getSheetId();- await this._commandService.executeCommand(SetWorksheetDefaultStyleMutation.id, {++ const params: ISetWorksheetDefaultStyleMutationParams = {unitId,subUnitId,defaultStyle: style,- });- this._worksheet.setDefaultCellStyle(style);+ };++ await this._commandService.executeCommand(SetWorksheetDefaultStyleCommand.id, params);return this;}@@ -144,13 +146,18 @@ export class FWorksheet {async setColumnDefaultStyle(index: number, style: string | Nullable<IStyleData>): Promise<FWorksheet> {const unitId = this._workbook.getUnitId();const subUnitId = this._worksheet.getSheetId();- const styles = [{ index, style }];- await this._commandService.executeCommand(SetWorksheetRowColumnStyleMutation.id, {++ const params: ISetColDataCommandParams = {unitId,subUnitId,- isRow: false,- styles,- });+ columnData: {+ [index]: {+ s: style,+ },+ },+ };++ await this._commandService.executeCommand(SetColDataCommand.id, params);return this;}@@ -162,13 +169,18 @@ export class FWorksheet {async setRowDefaultStyle(index: number, style: string | Nullable<IStyleData>): Promise<FWorksheet> {const unitId = this._workbook.getUnitId();const subUnitId = this._worksheet.getSheetId();- const styles = [{ index, style }];- await this._commandService.executeCommand(SetWorksheetRowColumnStyleMutation.id, {++ const params: ISetRowDataCommandParams = {unitId,subUnitId,- isRow: true,- styles,- });+ rowData: {+ [index]: {+ s: style,+ },+ },+ };++ await this._commandService.executeCommand(SetRowDataCommand.id, params);return this;}// #endregion@@ -687,6 +699,33 @@ export class FWorksheet {return this;}+ /**+ * Set custom properties for given rows.+ * @param custom The custom properties to set.+ * @returns This sheet, for chaining.+ */+ async setRowCustom(custom: IObjectArrayPrimitiveType<CustomData>): Promise<FWorksheet> {+ const unitId = this._workbook.getUnitId();+ const subUnitId = this._worksheet.getSheetId();++ const rowData: IObjectArrayPrimitiveType<Nullable<IRowData>> = {};+ for (const [rowIndex, customData] of Object.entries(custom)) {+ rowData[Number(rowIndex)] = {+ custom: customData,+ };+ }++ const params: ISetRowDataCommandParams = {+ unitId,+ subUnitId,+ rowData,+ };++ await this._commandService.executeCommand(SetRowDataCommand.id, params);++ return this;+ }+// #endregion// #region Column@@ -977,6 +1016,33 @@ export class FWorksheet {return this;}+ /**+ * Set custom properties for given columns.+ * @param custom The custom properties to set.+ * @returns This sheet, for chaining.+ */+ async setColumnCustom(custom: IObjectArrayPrimitiveType<CustomData>): Promise<FWorksheet> {+ const unitId = this._workbook.getUnitId();+ const subUnitId = this._worksheet.getSheetId();++ const columnData: IObjectArrayPrimitiveType<Nullable<IColumnData>> = {};+ for (const [columnIndex, customData] of Object.entries(custom)) {+ columnData[Number(columnIndex)] = {+ custom: customData,+ };+ }++ const params: ISetColDataCommandParams = {+ unitId,+ subUnitId,+ columnData,+ };++ await this._commandService.executeCommand(SetColDataCommand.id, params);++ return this;+ }+// #endregion// #region merge cellsdiff --git a/packages/sheets/src/basics/__tests__/cell-custom.spec.ts b/packages/sheets/src/basics/__tests__/cell-custom.spec.tsdeleted file mode 100644index ddcf09a4a2..0000000000--- a/packages/sheets/src/basics/__tests__/cell-custom.spec.ts+++ /dev/null@@ -1,50 +0,0 @@-/**- * 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 { describe, expect, it } from 'vitest';-import { handleCustom, transformCustom } from '../cell-custom';--describe('test utils cell custom', () => {- it('Function transformCustom', () => {- expect(transformCustom({}, null)).toStrictEqual({});- expect(transformCustom({}, undefined)).toStrictEqual({});- expect(transformCustom({}, {})).toStrictEqual({});- expect(transformCustom(undefined, { a: 1 })).toStrictEqual({ a: null });- expect(transformCustom({ a: 1 }, { b: 2 })).toStrictEqual({ a: 1, b: null });- expect(transformCustom({ a: 1 }, { b: 2, a: 3 })).toStrictEqual({ a: 1, b: null });- });-- it('Function handleCustom', () => {- const oldVal = {};- handleCustom(oldVal, { custom: undefined });- expect(oldVal).toStrictEqual({});-- handleCustom(oldVal, { custom: null });- expect(oldVal).toStrictEqual({});-- handleCustom(oldVal, { custom: {} });- expect(oldVal).toStrictEqual({});-- handleCustom(oldVal, { custom: { a: 1 } });- expect(oldVal).toStrictEqual({ custom: { a: 1 } });-- handleCustom(oldVal, { custom: { b: 1 } });- expect(oldVal).toStrictEqual({ custom: { a: 1, b: 1 } });-- handleCustom(oldVal, { custom: null });- expect(oldVal).toStrictEqual({});- });-});diff --git a/packages/sheets/src/basics/__tests__/row-column-value.spec.ts b/packages/sheets/src/basics/__tests__/row-column-value.spec.tsnew file mode 100644index 0000000000..980d410cea--- /dev/null+++ b/packages/sheets/src/basics/__tests__/row-column-value.spec.ts@@ -0,0 +1,103 @@+/**+ * 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 { BooleanNumber, type IColumnData, type IRowData } from '@univerjs/core';+import { describe, expect, it } from 'vitest';+import { getOldColumnData, getOldRowData } from '../row-column-value';++describe('test getOldRowData', () => {+ it('should return null if currentRow is null or undefined', () => {+ expect(getOldRowData(null, {})).toBeNull();+ expect(getOldRowData(undefined, {})).toBeUndefined();+ });++ it('should return a deep clone of currentRow if newRow is null or undefined', () => {+ const currentRow: Partial<IRowData> = { h: 20, ia: BooleanNumber.TRUE };+ const result = getOldRowData(currentRow, null);+ expect(result).toEqual(currentRow);+ expect(result).not.toBe(currentRow); // Ensure it's a deep clone++ const result2 = getOldRowData(currentRow, undefined);+ expect(result2).toEqual(currentRow);+ expect(result2).not.toBe(currentRow); // Ensure it's a deep clone+ });++ it('should return only the changed properties from currentRow', () => {+ const currentRow: Partial<IRowData> = { h: 20, ia: BooleanNumber.TRUE, ah: 30, hd: BooleanNumber.FALSE, s: 'style', custom: { key: 'customData' } };+ const newRow: Partial<IRowData> = { h: 25, ia: BooleanNumber.FALSE };++ const result = getOldRowData(currentRow, newRow);+ expect(result).toEqual({ h: 20, ia: BooleanNumber.TRUE });+ });++ it('should return an empty object if no properties have changed', () => {+ const currentRow: Partial<IRowData> = { h: 20, ia: BooleanNumber.TRUE };+ const newRow: Partial<IRowData> = {};++ const result = getOldRowData(currentRow, newRow);+ expect(result).toEqual({});+ });++ it('should handle mixed property changes correctly', () => {+ const currentRow: Partial<IRowData> = { h: 20, ia: BooleanNumber.TRUE, ah: 30, hd: BooleanNumber.FALSE, s: 'style', custom: { key: 'customData' } };+ const newRow: Partial<IRowData> = { h: 25, ah: 40, custom: { key: 'newCustomData' } };++ const result = getOldRowData(currentRow, newRow);+ expect(result).toEqual({ h: 20, ah: 30, custom: { key: 'customData' } });+ });+});++describe('test getOldColumnData', () => {+ it('should return null if currenColumn is null or undefined', () => {+ expect(getOldColumnData(null, {})).toBeNull();+ expect(getOldColumnData(undefined, {})).toBeUndefined();+ });++ it('should return a deep clone of currenColumn if newColumn is null or undefined', () => {+ const currenColumn: Partial<IColumnData> = { w: 20, hd: BooleanNumber.TRUE };+ const result = getOldColumnData(currenColumn, null);+ expect(result).toEqual(currenColumn);+ expect(result).not.toBe(currenColumn); // Ensure it's a deep clone++ const result2 = getOldColumnData(currenColumn, undefined);+ expect(result2).toEqual(currenColumn);+ expect(result2).not.toBe(currenColumn); // Ensure it's a deep clone+ });++ it('should return only the changed properties from currenColumn', () => {+ const currenColumn: Partial<IColumnData> = { w: 20, hd: BooleanNumber.TRUE, s: 'style', custom: { key: 'customData' } };+ const newColumn: Partial<IColumnData> = { w: 25, hd: BooleanNumber.FALSE };++ const result = getOldColumnData(currenColumn, newColumn);+ expect(result).toEqual({ w: 20, hd: BooleanNumber.TRUE });+ });++ it('should return an empty object if no properties have changed', () => {+ const currenColumn: Partial<IColumnData> = { w: 20, hd: BooleanNumber.TRUE };+ const newColumn: Partial<IColumnData> = {};++ const result = getOldColumnData(currenColumn, newColumn);+ expect(result).toEqual({});+ });++ it('should handle mixed property changes correctly', () => {+ const currenColumn: Partial<IColumnData> = { w: 20, hd: BooleanNumber.FALSE, s: 'style', custom: { key: 'customData' } };+ const newColumn: Partial<IColumnData> = { w: 25, custom: { key: 'newCustomData' } };++ const result = getOldColumnData(currenColumn, newColumn);+ expect(result).toEqual({ w: 20, custom: { key: 'customData' } });+ });+});diff --git a/packages/sheets/src/basics/cell-custom.ts b/packages/sheets/src/basics/cell-custom.tsdeleted file mode 100644index 969fddd4d5..0000000000--- a/packages/sheets/src/basics/cell-custom.ts+++ /dev/null@@ -1,78 +0,0 @@-/**- * 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 ICellData, type Nullable, Tools } from '@univerjs/core';--/**- * Update custom object by old and new value- * @param oldVal- * @param newVal- */-export function handleCustom(oldVal: ICellData, newVal: ICellData) {- // merge custom- const merge = mergeCustom(oldVal.custom, newVal.custom);-- // then remove null- if (merge) {- Tools.removeNull(merge);- }-- if (Tools.isEmptyObject(merge)) {- delete oldVal.custom;- } else {- oldVal.custom = merge;- }-}--/**- * Merge custom object by old and new value- * @param oldCustom- * @param newCustom- * @returns- */-function mergeCustom(oldCustom: Nullable<Record<string, any>>, newCustom: Nullable<Record<string, any>>) {- // clear custom- if (newCustom === null) return newCustom;- // don't operate- if (newCustom === undefined) return oldCustom;-- // Merge the first layer of attributes to prevent overlap. Do not process deeper layers, as too deep will cause performance issues.- return { ...oldCustom, ...newCustom };-}--/**- * Convert old custom normal key for storage- * @param style- */-export function transformCustom(- oldCustom: Nullable<Record<string, any>>,- newCustom: Nullable<Record<string, any>>-): Nullable<Record<string, any>> {- // If there is no newly set custom, directly store the historical custom- if (!newCustom || !Object.keys(newCustom).length) {- return oldCustom;- }- const backupCustom: Record<string, any> = oldCustom || {};-- for (const k in newCustom) {- // 1. To modify the existing custom,we need original setting to undo- // 2. Newly set the custom, we need null to undo- if (!(k in backupCustom)) {- backupCustom[k] = null;- }- }- return backupCustom;-}diff --git a/packages/sheets/src/basics/const/command-listener-const.ts b/packages/sheets/src/basics/const/command-listener-const.tsindex 8aee8ba307..88befb86af 100644--- a/packages/sheets/src/basics/const/command-listener-const.ts+++ b/packages/sheets/src/basics/const/command-listener-const.ts@@ -21,12 +21,13 @@ import { MoveColsMutation, MoveRowsMutation } from '../../commands/mutations/movimport { RemoveColMutation, RemoveRowMutation } from '../../commands/mutations/remove-row-col.mutation';import { RemoveWorksheetMergeMutation } from '../../commands/mutations/remove-worksheet-merge.mutation';import { ReorderRangeMutation } from '../../commands/mutations/reorder-range.mutation';+import { SetColDataMutation } from '../../commands/mutations/set-col-data.mutation';import { SetColHiddenMutation, SetColVisibleMutation } from '../../commands/mutations/set-col-visible.mutation';import { SetRangeValuesMutation } from '../../commands/mutations/set-range-values.mutation';+import { SetRowDataMutation } from '../../commands/mutations/set-row-data.mutation';import { SetRowHiddenMutation, SetRowVisibleMutation } from '../../commands/mutations/set-row-visible.mutation';import { SetWorksheetColWidthMutation } from '../../commands/mutations/set-worksheet-col-width.mutation';-import { SetWorksheetDefaultStyleMutation } from '../../commands/mutations/set-worksheet-default-style.mutations';-import { SetWorksheetRowColumnStyleMutation } from '../../commands/mutations/set-worksheet-row-column-style.mutation';+import { SetWorksheetDefaultStyleMutation } from '../../commands/mutations/set-worksheet-default-style.mutation';import {SetWorksheetRowAutoHeightMutation,SetWorksheetRowHeightMutation,@@ -63,6 +64,7 @@ export const COMMAND_LISTENER_VALUE_CHANGE = [RemoveWorksheetMergeMutation.id,AddWorksheetMergeMutation.id,ReorderRangeMutation.id,- SetWorksheetRowColumnStyleMutation.id,SetWorksheetDefaultStyleMutation.id,+ SetRowDataMutation.id,+ SetColDataMutation.id,];diff --git a/packages/sheets/src/basics/row-column-value.ts b/packages/sheets/src/basics/row-column-value.tsnew file mode 100644index 0000000000..64f99e4961--- /dev/null+++ b/packages/sheets/src/basics/row-column-value.ts@@ -0,0 +1,103 @@+/**+ * 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 { IColumnData, IRowData, Nullable } from '@univerjs/core';+import { Tools } from '@univerjs/core';++/**+ * Reset the row data to undefined when undoing the operation+ * @param currentRow+ * @returns+ */+export function getOldRowData(currentRow: Nullable<Partial<IRowData>>, newRow: Nullable<Partial<IRowData>>): Nullable<Partial<IRowData>> {+ if (currentRow === null || currentRow === undefined) {+ return currentRow;+ }++ const row = Tools.deepClone(currentRow);++ if (newRow === null || newRow === undefined) {+ return row;+ }++ const oldRow: Partial<IRowData> = {};++ // Restore only changed properties+ if ('h' in newRow) {+ oldRow.h = row.h;+ }++ if ('ia' in newRow) {+ oldRow.ia = row.ia;+ }++ if ('ah' in newRow) {+ oldRow.ah = row.ah;+ }++ if ('hd' in newRow) {+ oldRow.hd = row.hd;+ }++ if ('s' in newRow) {+ oldRow.s = row.s;+ }++ if ('custom' in newRow) {+ oldRow.custom = row.custom;+ }++ return oldRow;+}++/**+ * Reset the column data to undefined when undoing the operation+ * @param currenColumn+ * @param newColumn+ * @returns+ */+export function getOldColumnData(currenColumn: Nullable<Partial<IColumnData>>, newColumn: Nullable<Partial<IColumnData>>): Nullable<Partial<IColumnData>> {+ if (currenColumn === null || currenColumn === undefined) {+ return currenColumn;+ }++ const column = Tools.deepClone(currenColumn);++ if (newColumn === null || newColumn === undefined) {+ return column;+ }++ const oldColumn: Partial<IColumnData> = {};++ // Restore only changed properties+ if ('w' in newColumn) {+ oldColumn.w = column.w;+ }++ if ('hd' in newColumn) {+ oldColumn.hd = column.hd;+ }++ if ('s' in newColumn) {+ oldColumn.s = column.s;+ }++ if ('custom' in newColumn) {+ oldColumn.custom = column.custom;+ }++ return oldColumn;+}diff --git a/packages/sheets/src/commands/commands/__tests__/set-col-data.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-col-data.command.spec.tsnew file mode 100644index 0000000000..16aa840939--- /dev/null+++ b/packages/sheets/src/commands/commands/__tests__/set-col-data.command.spec.ts@@ -0,0 +1,159 @@+/**+ * 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 { Injector, IWorkbookData, Univer, Workbook } from '@univerjs/core';+import type { ISetColDataCommandParams } from '../set-col-data.command';+import { BooleanNumber, ICommandService, IUniverInstanceService, LocaleType, RedoCommand, UndoCommand, UniverInstanceType } from '@univerjs/core';++import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { SetColDataMutation } from '../../mutations/set-col-data.mutation';+import { SetColDataCommand } from '../set-col-data.command';+import { createCommandTestBed } from './create-command-test-bed';++const TEST_WORKBOOK_DATA_DEMO = (): IWorkbookData => {+ return {+ id: 'test',+ appVersion: '3.0.0-alpha',+ sheets: {+ sheet1: {+ id: 'sheet1',+ name: 'sheet1',+ cellData: {+ 0: {+ 0: {+ v: 'A1',+ },+ 1: {+ v: 'A2',+ },+ },+ },+ columnData: {+ 1: {+ hd: BooleanNumber.FALSE,+ },+ 5: {+ custom: {+ key: 'value',+ },+ },+ },+ rowData: {+ 1: {+ hd: BooleanNumber.FALSE,+ },++ },+ },+ },+ locale: LocaleType.ZH_CN,+ name: '',+ sheetOrder: [],+ styles: {},+ };+};++describe('test set column data commands', () => {+ let univer: Univer;+ let get: Injector['get'];+ let commandService: ICommandService;++ function getColumnData(column: number) {+ const worksheet = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet()!;+ const columnManager = worksheet.getColumnManager();+ return columnManager.getColumn(column);+ }++ beforeEach(() => {+ const testBed = createCommandTestBed(TEST_WORKBOOK_DATA_DEMO());+ univer = testBed.univer;+ get = testBed.get;++ commandService = get(ICommandService);+ commandService.registerCommand(SetColDataCommand);+ commandService.registerCommand(SetColDataMutation);+ });++ afterEach(() => univer.dispose());++ it('should set custom properties on columns', async () => {+ expect(getColumnData(1)).toEqual({ hd: BooleanNumber.FALSE });++ const params: ISetColDataCommandParams = {+ unitId: 'test',+ subUnitId: 'sheet1',+ columnData: {+ 1: {+ custom: {+ color: 'red',+ },+ },+ 2: {+ custom: {+ color: 'green',+ },+ },+ 5: {+ custom: undefined,+ },+ },++ };+ await commandService.executeCommand(SetColDataCommand.id, params);++ expect(getColumnData(1)).toEqual({+ hd: BooleanNumber.FALSE,+ custom: {+ color: 'red',+ },+ });+ expect(getColumnData(2)).toEqual({+ custom: {+ color: 'green',+ },+ });+ expect(getColumnData(5)).toEqual({+ custom: undefined,+ });++ await commandService.executeCommand(UndoCommand.id);+ expect(getColumnData(1)).toEqual({+ hd: BooleanNumber.FALSE,+ });+ expect(getColumnData(2)).toBeUndefined();+ expect(getColumnData(5)).toEqual({+ custom: {+ key: 'value',+ },+ });++ await commandService.executeCommand(RedoCommand.id);+ expect(getColumnData(1)).toEqual({+ hd: BooleanNumber.FALSE,+ custom: {+ color: 'red',+ },+ });+ expect(getColumnData(2)).toEqual({+ custom: {+ color: 'green',+ },+ });+ expect(getColumnData(5)).toEqual({+ custom: undefined,+ });+ });+});diff --git a/packages/sheets/src/commands/commands/__tests__/set-row-data.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-row-data.command.spec.tsnew file mode 100644index 0000000000..f529dfc1ea--- /dev/null+++ b/packages/sheets/src/commands/commands/__tests__/set-row-data.command.spec.ts@@ -0,0 +1,156 @@+/**+ * 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 { Injector, IWorkbookData, Univer, Workbook } from '@univerjs/core';+import type { ISetRowDataCommandParams } from '../set-row-data.command';++import { BooleanNumber, ICommandService, IUniverInstanceService, LocaleType, RedoCommand, UndoCommand, UniverInstanceType } from '@univerjs/core';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { SetRowDataMutation } from '../../mutations/set-row-data.mutation';+import { SetRowDataCommand } from '../set-row-data.command';+import { createCommandTestBed } from './create-command-test-bed';++const TEST_WORKBOOK_DATA_DEMO = (): IWorkbookData => {+ return {+ id: 'test',+ appVersion: '3.0.0-alpha',+ sheets: {+ sheet1: {+ id: 'sheet1',+ name: 'sheet1',+ cellData: {+ 0: {+ 0: {+ v: 'A1',+ },+ 1: {+ v: 'A2',+ },+ },+ },+ columnData: {+ 1: {+ hd: BooleanNumber.FALSE,+ },+ },+ rowData: {+ 1: {+ hd: BooleanNumber.FALSE,+ },+ 5: {+ custom: {+ key: 'value',+ },+ },+ },+ },+ },+ locale: LocaleType.ZH_CN,+ name: '',+ sheetOrder: [],+ styles: {},+ };+};++describe('test set row data commands', () => {+ let univer: Univer;+ let get: Injector['get'];+ let commandService: ICommandService;++ function getRowData(row: number) {+ const worksheet = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet()!;+ return worksheet.getRowManager().getRow(row);+ }++ beforeEach(() => {+ const testBed = createCommandTestBed(TEST_WORKBOOK_DATA_DEMO());+ univer = testBed.univer;+ get = testBed.get;++ commandService = get(ICommandService);+ commandService.registerCommand(SetRowDataCommand);+ commandService.registerCommand(SetRowDataMutation);+ });++ afterEach(() => univer.dispose());++ it('should set properties on rows', async () => {+ expect(getRowData(1)).toEqual({ hd: BooleanNumber.FALSE });++ const params: ISetRowDataCommandParams = {+ unitId: 'test',+ subUnitId: 'sheet1',+ rowData: {+ 1: {+ custom: {+ color: 'red',+ },+ },+ 2: {+ custom: {+ color: 'green',+ },+ },+ 5: {+ custom: undefined,+ },+ },+ };+ await commandService.executeCommand(SetRowDataCommand.id, params);++ expect(getRowData(1)).toEqual({+ hd: BooleanNumber.FALSE,+ custom: {+ color: 'red',+ },+ });+ expect(getRowData(2)).toEqual({+ custom: {+ color: 'green',+ },+ });+ expect(getRowData(5)).toEqual({+ custom: undefined,+ });++ await commandService.executeCommand(UndoCommand.id);+ expect(getRowData(1)).toEqual({+ hd: BooleanNumber.FALSE,+ });+ expect(getRowData(2)).toBeUndefined();+ expect(getRowData(5)).toEqual({+ custom: {+ key: 'value',+ },+ });++ await commandService.executeCommand(RedoCommand.id);+ expect(getRowData(1)).toEqual({+ hd: BooleanNumber.FALSE,+ custom: {+ color: 'red',+ },+ });+ expect(getRowData(2)).toEqual({+ custom: {+ color: 'green',+ },+ });+ expect(getRowData(5)).toEqual({+ custom: undefined,+ });+ });+});diff --git a/packages/sheets/src/commands/commands/__tests__/set-worksheet-default-style.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-worksheet-default-style.command.spec.tsnew file mode 100644index 0000000000..425819e722--- /dev/null+++ b/packages/sheets/src/commands/commands/__tests__/set-worksheet-default-style.command.spec.ts@@ -0,0 +1,70 @@+/**+ * 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 { Injector, Univer, Workbook } from '@univerjs/core';+import { ICommandService, IUniverInstanceService, RedoCommand, UndoCommand, UniverInstanceType } from '@univerjs/core';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';++import { SetWorksheetDefaultStyleMutation } from '../../mutations/set-worksheet-default-style.mutation';+import { SetWorksheetDefaultStyleCommand } from '../set-worksheet-default-style.command';+import { createCommandTestBed } from './create-command-test-bed';++describe('Test set worksheet default style commands', () => {+ let univer: Univer;+ let get: Injector['get'];+ let commandService: ICommandService;++ beforeEach(() => {+ const testBed = createCommandTestBed();+ univer = testBed.univer;+ get = testBed.get;++ commandService = get(ICommandService);+ commandService.registerCommand(SetWorksheetDefaultStyleCommand);+ commandService.registerCommand(SetWorksheetDefaultStyleMutation);+ });++ afterEach(() => {+ univer.dispose();+ });++ describe('set worksheet default style', () => {+ it('correct situation', async () => {+ const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;+ const worksheet = workbook.getSheetBySheetId('sheet1');+ if (!workbook) throw new Error('This is an error');++ const defaultStyle = { fontSize: 12, fontFamily: 'Arial' };++ expect(+ await commandService.executeCommand(SetWorksheetDefaultStyleCommand.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ defaultStyle,+ })+ ).toBeTruthy();++ expect(worksheet?.getDefaultCellStyle()).toEqual(defaultStyle);++ // undo;+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();+ expect(worksheet?.getDefaultCellStyle()).toBeUndefined();+ // redo+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();+ expect(worksheet?.getDefaultCellStyle()).toEqual(defaultStyle);+ });+ });+});diff --git a/packages/sheets/src/commands/commands/set-col-data.command.ts b/packages/sheets/src/commands/commands/set-col-data.command.tsnew file mode 100644index 0000000000..0d2d8fc78b--- /dev/null+++ b/packages/sheets/src/commands/commands/set-col-data.command.ts@@ -0,0 +1,72 @@+/**+ * 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 { IAccessor, IColumnData, ICommand, IObjectArrayPrimitiveType, Nullable } from '@univerjs/core';+import type { ISetColDataMutationParams } from '../mutations/set-col-data.mutation';++import type { ISheetCommandSharedParams } from '../utils/interface';+import {+ CommandType,+ ICommandService,+ IUndoRedoService,+ IUniverInstanceService,+} from '@univerjs/core';+import { SetColDataMutation, SetColDataMutationFactory } from '../mutations/set-col-data.mutation';+import { getSheetCommandTarget } from './utils/target-util';++export type IColumnProperties = Omit<IColumnData, 'w' | 'hd'>;++export interface ISetColDataCommandParams extends Partial<ISheetCommandSharedParams> {+ columnData: IObjectArrayPrimitiveType<Nullable<IColumnProperties>>;+}++export const SetColDataCommand: ICommand = {+ type: CommandType.COMMAND,+ id: 'sheet.command.set-col-data',+ handler: (accessor: IAccessor, params: ISetColDataCommandParams) => {+ const commandService = accessor.get(ICommandService);+ const undoRedoService = accessor.get(IUndoRedoService);+ const univerInstanceService = accessor.get(IUniverInstanceService);++ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { columnData } = params;+ const { unitId, subUnitId, worksheet } = target;++ const redoMutationParams: ISetColDataMutationParams = {+ subUnitId,+ unitId,+ columnData,+ };++ const undoMutationParams = SetColDataMutationFactory(redoMutationParams, worksheet);++ const result = commandService.syncExecuteCommand(SetColDataMutation.id, redoMutationParams);++ if (result) {+ undoRedoService.pushUndoRedo({+ unitID: unitId,+ undoMutations: [{ id: SetColDataMutation.id, params: undoMutationParams }],+ redoMutations: [{ id: SetColDataMutation.id, params: redoMutationParams }],+ });++ return true;+ }++ return false;+ },+};diff --git a/packages/sheets/src/commands/commands/set-row-data.command.ts b/packages/sheets/src/commands/commands/set-row-data.command.tsnew file mode 100644index 0000000000..b891338c83--- /dev/null+++ b/packages/sheets/src/commands/commands/set-row-data.command.ts@@ -0,0 +1,72 @@+/**+ * 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 { IAccessor, ICommand, IObjectArrayPrimitiveType, IRowData, Nullable } from '@univerjs/core';+import type { ISetRowDataMutationParams } from '../mutations/set-row-data.mutation';++import type { ISheetCommandSharedParams } from '../utils/interface';+import {+ CommandType,+ ICommandService,+ IUndoRedoService,+ IUniverInstanceService,+} from '@univerjs/core';+import { SetRowDataMutation, SetRowDataMutationFactory } from '../mutations/set-row-data.mutation';+import { getSheetCommandTarget } from './utils/target-util';++export type IRowProperties = Omit<IRowData, 'h' | 'ia' | 'ah' | 'hd'>;++export interface ISetRowDataCommandParams extends Partial<ISheetCommandSharedParams> {+ rowData: IObjectArrayPrimitiveType<Nullable<IRowProperties>>;+}++export const SetRowDataCommand: ICommand = {+ type: CommandType.COMMAND,+ id: 'sheet.command.set-row-data',+ handler: (accessor: IAccessor, params: ISetRowDataCommandParams) => {+ const commandService = accessor.get(ICommandService);+ const undoRedoService = accessor.get(IUndoRedoService);+ const univerInstanceService = accessor.get(IUniverInstanceService);++ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { rowData } = params;+ const { unitId, subUnitId, worksheet } = target;++ const redoMutationParams: ISetRowDataMutationParams = {+ subUnitId,+ unitId,+ rowData,+ };++ const undoMutationParams = SetRowDataMutationFactory(redoMutationParams, worksheet);++ const result = commandService.syncExecuteCommand(SetRowDataMutation.id, redoMutationParams);++ if (result) {+ undoRedoService.pushUndoRedo({+ unitID: unitId,+ undoMutations: [{ id: SetRowDataMutation.id, params: undoMutationParams }],+ redoMutations: [{ id: SetRowDataMutation.id, params: redoMutationParams }],+ });++ return true;+ }++ return false;+ },+};diff --git a/packages/sheets/src/commands/commands/set-worksheet-default-style.command.ts b/packages/sheets/src/commands/commands/set-worksheet-default-style.command.tsnew file mode 100644index 0000000000..b7eb8a7201--- /dev/null+++ b/packages/sheets/src/commands/commands/set-worksheet-default-style.command.ts@@ -0,0 +1,52 @@+/**+ * 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 { IAccessor, ICommand } from '@univerjs/core';++import type { ISetWorksheetDefaultStyleMutationParams } from '../mutations/set-worksheet-default-style.mutation';+import {+ CommandType,+ ICommandService,+ IUndoRedoService,+} from '@univerjs/core';+import { SetWorksheetDefaultStyleMutation, SetWorksheetDefaultStyleMutationFactory } from '../mutations/set-worksheet-default-style.mutation';++export const SetWorksheetDefaultStyleCommand: ICommand = {+ type: CommandType.COMMAND,+ id: 'sheet.command.set-worksheet-default-style',+ handler: (accessor: IAccessor, params: ISetWorksheetDefaultStyleMutationParams) => {+ const commandService = accessor.get(ICommandService);+ const undoRedoService = accessor.get(IUndoRedoService);++ const { unitId } = params;++ const undoMutationParams = SetWorksheetDefaultStyleMutationFactory(accessor, params);++ const result = commandService.syncExecuteCommand(SetWorksheetDefaultStyleMutation.id, params);++ if (result) {+ undoRedoService.pushUndoRedo({+ unitID: unitId,+ undoMutations: [{ id: SetWorksheetDefaultStyleMutation.id, params: undoMutationParams }],+ redoMutations: [{ id: SetWorksheetDefaultStyleMutation.id, params }],+ });++ return true;+ }++ return false;+ },+};diff --git a/packages/sheets/src/commands/mutations/set-col-data.mutation.ts b/packages/sheets/src/commands/mutations/set-col-data.mutation.tsnew file mode 100644index 0000000000..8472a3cdce--- /dev/null+++ b/packages/sheets/src/commands/mutations/set-col-data.mutation.ts@@ -0,0 +1,80 @@+/**+ * 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 { IColumnData, IMutation, IObjectArrayPrimitiveType, Nullable, Worksheet } from '@univerjs/core';+import { CommandType, IUniverInstanceService } from '@univerjs/core';+import { getOldColumnData } from '../../basics/row-column-value';+import { getSheetCommandTarget } from '../commands/utils/target-util';++export interface ISetColDataMutationParams {+ unitId: string;+ subUnitId: string;+ columnData: IObjectArrayPrimitiveType<Nullable<IColumnData>>;+}++export const SetColDataMutationFactory = (+ params: ISetColDataMutationParams,+ worksheet: Worksheet+): ISetColDataMutationParams => {+ const { unitId, subUnitId, columnData } = params;++ const oldColData: IObjectArrayPrimitiveType<Nullable<IColumnData>> = {};+ const manager = worksheet.getColumnManager();++ // Loop through the column data and set the old column data+ for (const colIndex in columnData) {+ const newCol = columnData[colIndex];+ const currentCol = manager.getColumn(Number(colIndex));++ oldColData[colIndex] = getOldColumnData(currentCol, newCol);+ }++ return {+ unitId,+ subUnitId,+ columnData: oldColData,+ };+};++export const SetColDataMutation: IMutation<ISetColDataMutationParams> = {+ id: 'sheet.mutation.set-col-data',+ type: CommandType.MUTATION,+ handler: (accessor, params) => {+ const { columnData } = params;+ const univerInstanceService = accessor.get(IUniverInstanceService);+ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { worksheet } = target;+ const manager = worksheet.getColumnManager();++ // Loop through the custom column properties data and set the properties to the column+ for (const colIndex in columnData) {+ const col = columnData[colIndex];++ // Similar to setting the column width, we assume that null and undefined both have clear deletion meanings.+ if (col === null || col === undefined) {+ manager.removeColumn(Number(colIndex));+ continue;+ }++ const currentCol = manager.getColumnOrCreate(Number(colIndex));+ Object.assign(currentCol, col);+ }++ return true;+ },+};diff --git a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts b/packages/sheets/src/commands/mutations/set-range-values.mutation.tsindex 92bf0507fc..772a200459 100644--- a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts+++ b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts@@ -14,7 +14,6 @@* limitations under the License.*/-import { CommandType, IUniverInstanceService, ObjectMatrix, Tools } from '@univerjs/core';import type {IAccessor,ICellData,@@ -26,6 +25,7 @@ import type {Nullable,Workbook,} from '@univerjs/core';+import { CommandType, IUniverInstanceService, ObjectMatrix, Tools } from '@univerjs/core';import { handleStyle, transformStyle } from '../../basics/cell-style';import { getCellType } from '../../basics/cell-type';import { getCellValue, setNull } from '../../basics/cell-value';@@ -88,9 +88,6 @@ export const SetRangeValuesUndoMutationFactory = (cell.s = transformStyle(oldStyle, newStyle);- // Do not use the transformCustom method, custom will overwrite the original value- // cell.custom = transformCustom(cell.custom, newVal?.custom);-undoData.setValue(row, col, setNull(cell));});@@ -164,7 +161,6 @@ export const SetRangeValuesMutation: IMutation<ISetRangeValuesMutationParams, boif (newVal.custom !== undefined) {// Custom will overwrite the original valueoldVal.custom = newVal.custom;- // handleCustom(oldVal, newVal);}cellMatrix.setValue(row, col, Tools.removeNull(oldVal));diff --git a/packages/sheets/src/commands/mutations/set-row-data.mutation.ts b/packages/sheets/src/commands/mutations/set-row-data.mutation.tsnew file mode 100644index 0000000000..4e59771a49--- /dev/null+++ b/packages/sheets/src/commands/mutations/set-row-data.mutation.ts@@ -0,0 +1,80 @@+/**+ * 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 { IMutation, IObjectArrayPrimitiveType, IRowData, Nullable, Worksheet } from '@univerjs/core';+import { CommandType, IUniverInstanceService } from '@univerjs/core';+import { getOldRowData } from '../../basics/row-column-value';+import { getSheetCommandTarget } from '../commands/utils/target-util';++export interface ISetRowDataMutationParams {+ unitId: string;+ subUnitId: string;+ rowData: IObjectArrayPrimitiveType<Nullable<IRowData>>;+}++export const SetRowDataMutationFactory = (+ params: ISetRowDataMutationParams,+ worksheet: Worksheet+): ISetRowDataMutationParams => {+ const { unitId, subUnitId, rowData } = params;++ const oldRowData: IObjectArrayPrimitiveType<Nullable<IRowData>> = {};+ const manager = worksheet.getRowManager();++ // Loop through the row data and set the old row data+ for (const rowIndex in rowData) {+ const newRow = rowData[rowIndex];+ const currentRow = manager.getRow(Number(rowIndex));++ oldRowData[rowIndex] = getOldRowData(currentRow, newRow);+ }++ return {+ unitId,+ subUnitId,+ rowData: oldRowData,+ };+};++export const SetRowDataMutation: IMutation<ISetRowDataMutationParams> = {+ id: 'sheet.mutation.set-row-data',+ type: CommandType.MUTATION,+ handler: (accessor, params) => {+ const { rowData } = params;+ const univerInstanceService = accessor.get(IUniverInstanceService);+ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;++ const { worksheet } = target;+ const manager = worksheet.getRowManager();++ // Loop through the custom row properties data and set the properties to the row+ for (const rowIndex in rowData) {+ const row = rowData[rowIndex];++ // Similar to setting the row height, we assume that null and undefined both have clear deletion meanings.+ if (row === null || row === undefined) {+ manager.removeRow(Number(rowIndex));+ continue;+ }++ const currentRow = manager.getRowOrCreate(Number(rowIndex));+ Object.assign(currentRow, row);+ }++ return true;+ },+};diff --git a/packages/sheets/src/commands/mutations/set-worksheet-default-style.mutations.ts b/packages/sheets/src/commands/mutations/set-worksheet-default-style.mutation.tssimilarity index 86%rename from packages/sheets/src/commands/mutations/set-worksheet-default-style.mutations.tsrename to packages/sheets/src/commands/mutations/set-worksheet-default-style.mutation.tsindex 5f4e14de45..a9240b74f6 100644--- a/packages/sheets/src/commands/mutations/set-worksheet-default-style.mutations.ts+++ b/packages/sheets/src/commands/mutations/set-worksheet-default-style.mutation.ts@@ -14,14 +14,14 @@* limitations under the License.*/-import type { IAccessor, IMutation, IStyleData } from '@univerjs/core';+import type { IAccessor, IMutation, IStyleData, Nullable } from '@univerjs/core';import { CommandType, IUniverInstanceService } from '@univerjs/core';import { getSheetCommandTarget, getSheetMutationTarget } from '../commands/utils/target-util';export interface ISetWorksheetDefaultStyleMutationParams {unitId: string;subUnitId: string;- defaultStyle: string | IStyleData;+ defaultStyle: string | Nullable<IStyleData>;}export const SetWorksheetDefaultStyleMutation: IMutation<ISetWorksheetDefaultStyleMutationParams> = {@@ -45,13 +45,13 @@ export const SetWorksheetDefaultStyleMutation: IMutation<ISetWorksheetDefaultStyexport const SetWorksheetDefaultStyleMutationFactory = (accessor: IAccessor, params: ISetWorksheetDefaultStyleMutationParams) => {const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);if (!target) {- throw new Error('[SetWorksheetNameMutationFactory]: worksheet is null error!');+ throw new Error('[SetWorksheetDefaultStyleMutationFactory]: worksheet is null error!');}const { worksheet } = target;return {unitId: params.unitId,- name: worksheet.getDefaultCellStyle(),subUnitId: worksheet.getSheetId(),+ defaultStyle: worksheet.getDefaultCellStyle(),};};diff --git a/packages/sheets/src/commands/mutations/set-worksheet-row-column-style.mutation.ts b/packages/sheets/src/commands/mutations/set-worksheet-row-column-style.mutation.tsdeleted file mode 100644index e795efea53..0000000000--- a/packages/sheets/src/commands/mutations/set-worksheet-row-column-style.mutation.ts+++ /dev/null@@ -1,76 +0,0 @@-/**- * 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 { IAccessor, IMutation, IStyleData, Nullable } from '@univerjs/core';-import { CommandType, IUniverInstanceService } from '@univerjs/core';-import { getSheetCommandTarget } from '../commands/utils/target-util';--export interface ISetWorksheetRowColumnStyleMutationParamsUnit {- index: number;- style: string | Nullable<IStyleData>;-}--export interface ISetWorksheetRowColumnStyleMutationParams {- unitId: string;- subUnitId: string;- isRow: boolean;- styles: ISetWorksheetRowColumnStyleMutationParamsUnit[];-}--export const SetWorksheetRowColumnStyleMutationFactory = (accessor: IAccessor, params: ISetWorksheetRowColumnStyleMutationParams) => {- const universheet = accessor.get(IUniverInstanceService).getUniverSheetInstance(params.unitId);- if (universheet == null) {- throw new Error('[SetWorksheetRowColumnStyleMutationFactory]: universheet is null error!');- }-- const worksheet = universheet.getSheetBySheetId(params.subUnitId);- if (!worksheet) {- throw new Error('[SetWorksheetRowColumnStyleMutationFactory]: worksheet is null error!');- }-- return {- unitId: params.unitId,- subUnitId: params.subUnitId,- styles: params.styles,- isRow: params.isRow,- };-};--export const SetWorksheetRowColumnStyleMutation: IMutation<ISetWorksheetRowColumnStyleMutationParams> = {- id: 'sheet.mutation.set-worksheet-row-column-style',- type: CommandType.MUTATION,- handler: (accessor, params) => {- const { styles, isRow } = params;- const univerInstanceService = accessor.get(IUniverInstanceService);- const target = getSheetCommandTarget(univerInstanceService);- if (!target) return false;-- const { worksheet } = target;- if (!worksheet) {- return false;- }- if (isRow) {- for (const { index, style } of styles) {- worksheet.setRowStyle(index, style);- }- } else {- for (const { index, style } of styles) {- worksheet.setColumnStyle(index, style);- }- }- return true;- },-};diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.tsindex 25c5099929..7da9e971b7 100644--- a/packages/sheets/src/controllers/basic-worksheet.controller.ts+++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts@@ -52,6 +52,7 @@ import {SetBorderPositionCommand,SetBorderStyleCommand,} from '../commands/commands/set-border-command';+import { SetColDataCommand } from '../commands/commands/set-col-data.command';import {SetColHiddenCommand,SetSelectedColsVisibleCommand,@@ -62,6 +63,7 @@ import { SetFrozenCommand } from '../commands/commands/set-frozen.command';import { SetFrozenCancelCommand } from '../commands/commands/set-frozen-cancel.command';import { SetRangeProtectionCommand } from '../commands/commands/set-range-protection.command';import { SetRangeValuesCommand } from '../commands/commands/set-range-values.command';+import { SetRowDataCommand } from '../commands/commands/set-row-data.command';import {SetRowHiddenCommand,SetSelectedRowsVisibleCommand,@@ -109,21 +111,23 @@ import { RemoveColMutation, RemoveRowMutation } from '../commands/mutations/remoimport { RemoveSheetMutation } from '../commands/mutations/remove-sheet.mutation';import { RemoveWorksheetMergeMutation } from '../commands/mutations/remove-worksheet-merge.mutation';import { ReorderRangeMutation } from '../commands/mutations/reorder-range.mutation';+import { SetColDataMutation } from '../commands/mutations/set-col-data.mutation';import { SetColHiddenMutation, SetColVisibleMutation } from '../commands/mutations/set-col-visible.mutation';import { SetFrozenMutation } from '../commands/mutations/set-frozen.mutation';+import { SetRangeProtectionMutation } from '../commands/mutations/set-range-protection.mutation';import { SetRangeValuesMutation } from '../commands/mutations/set-range-values.mutation';+import { SetRowDataMutation } from '../commands/mutations/set-row-data.mutation';import { SetRowHiddenMutation, SetRowVisibleMutation } from '../commands/mutations/set-row-visible.mutation';import { SetTabColorMutation } from '../commands/mutations/set-tab-color.mutation';import { SetWorkbookNameMutation } from '../commands/mutations/set-workbook-name.mutation';import { SetWorksheetColWidthMutation } from '../commands/mutations/set-worksheet-col-width.mutation';-import { SetWorksheetDefaultStyleMutation } from '../commands/mutations/set-worksheet-default-style.mutations';+import { SetWorksheetDefaultStyleMutation } from '../commands/mutations/set-worksheet-default-style.mutation';import { SetWorksheetHideMutation } from '../commands/mutations/set-worksheet-hide.mutation';import { SetWorksheetNameMutation } from '../commands/mutations/set-worksheet-name.mutation';import { SetWorksheetOrderMutation } from '../commands/mutations/set-worksheet-order.mutation';import { SetWorksheetPermissionPointsMutation } from '../commands/mutations/set-worksheet-permission-points.mutation';import { SetWorksheetProtectionMutation } from '../commands/mutations/set-worksheet-protection.mutation';-import { SetWorksheetRowColumnStyleMutation } from '../commands/mutations/set-worksheet-row-column-style.mutation';import {SetWorksheetRowAutoHeightMutation,SetWorksheetRowHeightMutation,@@ -172,8 +176,6 @@ export class BasicWorksheetController extends Disposable implements IDisposableSetNumfmtMutation,ReorderRangeMutation,EmptyMutation,- SetWorksheetDefaultStyleMutation,- SetWorksheetRowColumnStyleMutation,] as IMutation<object>[]).forEach((mutation) => {this._commandService.registerCommand(mutation);this._dataSyncPrimaryController?.registerSyncingMutations(mutation);@@ -220,6 +222,8 @@ export class BasicWorksheetController extends Disposable implements IDisposableSetColHiddenMutation,SetColVisibleMutation,SetColWidthCommand,+ SetColDataCommand,+ SetColDataMutation,SetFrozenCancelCommand,SetFrozenCommand,SetFrozenMutation,@@ -229,6 +233,8 @@ export class BasicWorksheetController extends Disposable implements IDisposableSetRowHiddenCommand,SetRowHiddenMutation,SetRowVisibleMutation,+ SetRowDataCommand,+ SetRowDataMutation,SetSelectedColsVisibleCommand,SetSelectedRowsVisibleCommand,SetSpecificColsVisibleCommand,@@ -281,6 +287,7 @@ export class BasicWorksheetController extends Disposable implements IDisposableSetRangeProtectionMutation,ToggleCellCheckboxCommand,+ SetWorksheetDefaultStyleMutation,].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));}diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.tsindex 638d1a0506..9f53a04286 100644--- a/packages/sheets/src/index.ts+++ b/packages/sheets/src/index.ts@@ -200,8 +200,12 @@ export { DeleteRangeProtectionCommand, type IDeleteRangeProtectionCommandParamsexport { InsertDefinedNameCommand } from './commands/commands/insert-defined-name.command';export { InsertRangeMoveDownCommand, type InsertRangeMoveDownCommandParams } from './commands/commands/insert-range-move-down.command';export { InsertRangeMoveRightCommand, type InsertRangeMoveRightCommandParams } from './commands/commands/insert-range-move-right.command';-export { type ISetWorksheetDefaultStyleMutationParams, SetWorksheetDefaultStyleMutation, SetWorksheetDefaultStyleMutationFactory } from './commands/mutations/set-worksheet-default-style.mutations';-export { type ISetWorksheetRowColumnStyleMutationParams, SetWorksheetRowColumnStyleMutation, SetWorksheetRowColumnStyleMutationFactory } from './commands/mutations/set-worksheet-row-column-style.mutation';+export { type ISetWorksheetDefaultStyleMutationParams, SetWorksheetDefaultStyleMutation, SetWorksheetDefaultStyleMutationFactory } from './commands/mutations/set-worksheet-default-style.mutation';+export { SetWorksheetDefaultStyleCommand } from './commands/commands/set-worksheet-default-style.command';+export { type ISetRowDataMutationParams, SetRowDataMutation, SetRowDataMutationFactory } from './commands/mutations/set-row-data.mutation';+export { type ISetRowDataCommandParams, SetRowDataCommand } from './commands/commands/set-row-data.command';+export { type ISetColDataMutationParams, SetColDataMutation, SetColDataMutationFactory } from './commands/mutations/set-col-data.mutation';+export { type ISetColDataCommandParams, SetColDataCommand } from './commands/commands/set-col-data.command';export {type IInsertColCommandParams,type IInsertRowCommandParams,@@ -403,4 +407,5 @@ export { ScrollToCellOperation } from './commands/operations/scroll-to-cell.operexport { type ISetSelectionsOperationParams, SetSelectionsOperation } from './commands/operations/selection.operation';export { type ISetWorksheetActiveOperationParams, SetWorksheetActiveOperation } from './commands/operations/set-worksheet-active.operation';export { type IToggleCellCheckboxCommandParams, ToggleCellCheckboxCommand } from './commands/commands/toggle-checkbox.command';+// #endregion