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): merge cell custom property (#3365)

Dushusir880ef4433464
diff --git a/packages/core/src/sheets/__tests__/cell-data.spec.ts b/packages/core/src/sheets/__tests__/cell-data.spec.ts
index 1ea4798fbe..a579fdff99 100644
--- a/packages/core/src/sheets/__tests__/cell-data.spec.ts
+++ b/packages/core/src/sheets/__tests__/cell-data.spec.ts
@@ -69,6 +69,7 @@ describe('Test cell data', () => {
expect(isICellData({ f: '=SUM(1)' })).toBeTruthy();
expect(isICellData({ si: '1' })).toBeTruthy();
expect(isICellData({ t: 1 })).toBeTruthy();
+ expect(isICellData({ custom: {} })).toBeTruthy();
expect(isICellData({ a: '1' })).toBeFalsy();
expect(isICellData(null)).toBeFalsy();
diff --git a/packages/core/src/sheets/typedef.ts b/packages/core/src/sheets/typedef.ts
index c5214097cc..966e8b8946 100644
--- a/packages/core/src/sheets/typedef.ts
+++ b/packages/core/src/sheets/typedef.ts
@@ -253,7 +253,8 @@ export function isICellData(value: any): value is ICellData {
(value as ICellData).v !== undefined ||
(value as ICellData).t !== undefined ||
(value as ICellData).f !== undefined ||
- (value as ICellData).si !== undefined)
+ (value as ICellData).si !== undefined ||
+ (value as ICellData).custom !== undefined)
);
}
diff --git a/packages/sheets/src/basics/__tests__/cell-custom.spec.ts b/packages/sheets/src/basics/__tests__/cell-custom.spec.ts
new file mode 100644
index 0000000000..ddcf09a4a2
--- /dev/null
+++ b/packages/sheets/src/basics/__tests__/cell-custom.spec.ts
@@ -0,0 +1,50 @@
+/**
+ * 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/cell-custom.ts b/packages/sheets/src/basics/cell-custom.ts
new file mode 100644
index 0000000000..969fddd4d5
--- /dev/null
+++ b/packages/sheets/src/basics/cell-custom.ts
@@ -0,0 +1,78 @@
+/**
+ * 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/cell-style.ts b/packages/sheets/src/basics/cell-style.ts
index 92ac7cea2a..556910174f 100644
--- a/packages/sheets/src/basics/cell-style.ts
+++ b/packages/sheets/src/basics/cell-style.ts
@@ -34,7 +34,9 @@ export function handleStyle(styles: Styles, oldVal: ICellData, newVal: ICellData
const merge = mergeStyle(oldStyle, newVal.s ? (newVal.s as Nullable<IStyleData>) : null);
// then remove null
- merge && Tools.removeNull(merge);
+ if (merge) {
+ Tools.removeNull(merge);
+ }
if (Tools.isEmptyObject(merge)) {
delete oldVal.s;
@@ -58,17 +60,6 @@ export function handleStyle(styles: Styles, oldVal: ICellData, newVal: ICellData
* @param style
*/
export function transformStyle(oldStyle: Nullable<IStyleData>, newStyle: Nullable<IStyleData>): Nullable<IStyleData> {
- const backupStyle = transformNormalKey(oldStyle, newStyle);
- return backupStyle;
-}
-/**
- * Convert old style normal key for storage
- * @param style
- */
-function transformNormalKey(
- oldStyle: Nullable<IStyleData>,
- newStyle: Nullable<IStyleData>
-): Nullable<IStyleData> {
// If there is no newly set style, directly store the historical style
if (!newStyle || !Object.keys(newStyle).length) {
return oldStyle;
@@ -125,7 +116,7 @@ function mergeStyle(
if (newStyle === undefined) return oldStyle;
const backupStyle: Record<string, any> = Tools.deepClone(oldStyle) || {};
- if (!backupStyle) return;
+
for (const k in newStyle) {
// Do not copy cell background color to rich text background color.
if (isRichText && ['bd', 'tr', 'td', 'ht', 'vt', 'tb', 'pd', 'bg'].includes(k)) {
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 dfd371e4ca..80ab116cad 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
@@ -295,6 +295,19 @@ describe('Test set range values commands', () => {
return params;
}
+ function getParamsMore() {
+ const params: ISetRangeValuesCommandParams = {
+ value: {
+ v: 'a1',
+ custom: {
+ name: 'test',
+ },
+ },
+ };
+
+ return params;
+ }
+
function getResultParams() {
const params: ISetRangeValuesCommandParams = {
value: {
@@ -309,9 +322,31 @@ describe('Test set range values commands', () => {
return params;
}
+ function getResultParamsMore() {
+ const params: ISetRangeValuesCommandParams = {
+ value: {
+ v: 'a1',
+ t: CellValueType.STRING,
+ custom: {
+ id: 1,
+ name: 'test',
+ },
+ },
+ };
+
+ 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',
@@ -322,8 +357,8 @@ describe('Test set range values commands', () => {
expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
expect(getValue()).toStrictEqual(getResultParams().value);
- // reset
- expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(getValue()).toStrictEqual(getResultParamsMore().value);
});
it('set value in other worksheet', async () => {
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 0731158d62..0f9743b2bc 100644
--- a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
+++ b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
@@ -29,6 +29,7 @@ import { CommandType, IUniverInstanceService, ObjectMatrix, Tools } from '@unive
import { handleStyle, transformStyle } from '../../basics/cell-style';
import { getCellValue, setNull } from '../../basics/cell-value';
import { getCellType } from '../../basics/cell-type';
+import { handleCustom, transformCustom } from '../../basics/cell-custom';
/** Params of `SetRangeValuesMutation` */
export interface ISetRangeValuesMutationParams extends IMutationCommonParams {
@@ -84,10 +85,11 @@ export const SetRangeValuesUndoMutationFactory = (
const cell = Tools.deepClone(cellMatrix?.getValue(row, col)) || {}; // clone cell data,prevent modify the original data
const oldStyle = styles.getStyleByCell(cell);
// transformStyle does not accept style id
- let newStyle = styles.getStyleByCell(newVal);
- newStyle = transformStyle(oldStyle, newStyle);
+ const newStyle = styles.getStyleByCell(newVal);
- cell.s = newStyle;
+ cell.s = transformStyle(oldStyle, newStyle);
+
+ cell.custom = transformCustom(cell.custom, newVal?.custom);
undoData.setValue(row, col, setNull(cell));
});
@@ -99,10 +101,6 @@ export const SetRangeValuesUndoMutationFactory = (
} as ISetRangeValuesMutationParams;
};
-/**
- * TODO@Dushusir: Excel can display numbers with up to about 15 digits of precision. When the user inputs more than 15 digits, interception is required, but there are unknown performance risks.
- Intercept 15-digit number reference function truncateNumber
- */
export const SetRangeValuesMutation: IMutation<ISetRangeValuesMutationParams, boolean> = {
id: 'sheet.mutation.set-range-values',
@@ -164,7 +162,7 @@ export const SetRangeValuesMutation: IMutation<ISetRangeValuesMutationParams, bo
}
if (newVal.custom !== undefined) {
- oldVal.custom = newVal.custom;
+ handleCustom(oldVal, newVal);
}
cellMatrix.setValue(row, col, Tools.removeNull(oldVal));