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.

fix(sheet): custom will overwrite the original value (#3431)

Dushusire1042d449c64
diff --git a/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts
index 3c0e654223..df96216d3c 100644
--- a/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts
@@ -328,7 +328,7 @@ describe('Test set range values commands', () => {
v: 'a1',
t: CellValueType.STRING,
custom: {
- id: 1,
+ // id: 1,
name: 'test',
},
},
@@ -361,6 +361,115 @@ describe('Test set range values commands', () => {
expect(getValue()).toStrictEqual(getResultParamsMore().value);
});
+ it('will set range values when there is a selected range, includes custom property blank object and undefined', async () => {
+ function getParams() {
+ const params: ISetRangeValuesCommandParams = {
+ value: {
+ v: 'a1',
+ custom: { },
+ },
+ };
+
+ return params;
+ }
+
+ function getParamsMore() {
+ const params: ISetRangeValuesCommandParams = {
+ value: {
+ v: 'a1',
+ custom: undefined,
+ },
+ };
+
+ return params;
+ }
+
+ function getResultParams() {
+ const params: ISetRangeValuesCommandParams = {
+ value: {
+ v: 'a1',
+ t: CellValueType.STRING,
+ custom: { },
+ },
+ };
+
+ return params;
+ }
+
+ function getResultParamsMore() {
+ const params: ISetRangeValuesCommandParams = {
+ value: {
+ v: 'a1',
+ t: CellValueType.STRING,
+ custom: { },
+ },
+ };
+
+ return params;
+ }
+
+ expect(await commandService.executeCommand(SetRangeValuesCommand.id, getParams())).toBeTruthy();
+ expect(getValue()).toStrictEqual(getResultParams().value);
+
+ expect(await commandService.executeCommand(SetRangeValuesCommand.id, getParamsMore())).toBeTruthy();
+ expect(getValue()).toStrictEqual(getResultParamsMore().value);
+
+ // undo
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(getValue()).toStrictEqual(getResultParams().value);
+
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(getValue()).toStrictEqual({
+ v: 'A1',
+ t: CellValueType.STRING,
+ });
+
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(getValue()).toStrictEqual(getResultParams().value);
+
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(getValue()).toStrictEqual(getResultParamsMore().value);
+ });
+
+ it('will set range values when there is a selected range, includes custom property null', async () => {
+ function getParams() {
+ const params: ISetRangeValuesCommandParams = {
+ value: {
+ v: 'a1',
+ custom: null,
+ },
+ };
+
+ return params;
+ }
+
+ function getResultParams() {
+ const params: ISetRangeValuesCommandParams = {
+ value: {
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ };
+
+ return params;
+ }
+
+ expect(await commandService.executeCommand(SetRangeValuesCommand.id, getParams())).toBeTruthy();
+ expect(getValue()).toStrictEqual(getResultParams().value);
+
+ // undo
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(getValue()).toStrictEqual({
+ v: 'A1',
+ t: CellValueType.STRING,
+ });
+
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(getValue()).toStrictEqual(getResultParams().value);
+ });
+
it('set value in other worksheet', async () => {
function getParams() {
const params: ISetRangeValuesCommandParams = {
diff --git a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
index 976aeaf591..92bf0507fc 100644
--- a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
+++ b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
@@ -26,7 +26,6 @@ import type {
Nullable,
Workbook,
} from '@univerjs/core';
-import { handleCustom, transformCustom } from '../../basics/cell-custom';
import { handleStyle, transformStyle } from '../../basics/cell-style';
import { getCellType } from '../../basics/cell-type';
import { getCellValue, setNull } from '../../basics/cell-value';
@@ -89,7 +88,8 @@ export const SetRangeValuesUndoMutationFactory = (
cell.s = transformStyle(oldStyle, newStyle);
- cell.custom = transformCustom(cell.custom, newVal?.custom);
+ // 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));
});
@@ -162,7 +162,9 @@ export const SetRangeValuesMutation: IMutation<ISetRangeValuesMutationParams, bo
}
if (newVal.custom !== undefined) {
- handleCustom(oldVal, newVal);
+ // Custom will overwrite the original value
+ oldVal.custom = newVal.custom;
+ // handleCustom(oldVal, newVal);
}
cellMatrix.setValue(row, col, Tools.removeNull(oldVal));