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(nunfmt): #1034 Error moving multiple data formats (#1036)

Gpound.liu2114128e68e0
diff --git a/packages/sheets-numfmt/src/__tests__/utils.test.ts b/packages/sheets-numfmt/src/__tests__/utils.test.ts
index bd1f59a5a4..5bbe6ca1c3 100644
--- a/packages/sheets-numfmt/src/__tests__/utils.test.ts
+++ b/packages/sheets-numfmt/src/__tests__/utils.test.ts
@@ -104,7 +104,7 @@ describe('test numfmt utils function', () => {
endColumn: 5,
},
]);
- expect((result as any)[1].params.values.a.ranges).toEqual([
+ expect((result as any)[1].params.values['1'].ranges).toEqual([
{
startRow: 2,
endRow: 2,
@@ -119,4 +119,101 @@ describe('test numfmt utils function', () => {
},
]);
});
+
+ it('mergeNumfmtMutations multiple data formats of different types', () => {
+ const result = mergeNumfmtMutations([
+ {
+ id: SetNumfmtMutation.id,
+ params: {
+ values: {
+ a: { ranges: [cellToRange(2, 3), cellToRange(2, 4), cellToRange(4, 5)] },
+ },
+ refMap: { a: { pattern: 'a', type: '' } },
+ },
+ },
+ {
+ id: RemoveNumfmtMutation.id,
+ params: {
+ ranges: [cellToRange(2, 3), cellToRange(2, 4), cellToRange(4, 5)],
+ },
+ },
+ {
+ id: SetNumfmtMutation.id,
+ params: {
+ values: {
+ a: { ranges: [cellToRange(4, 6), cellToRange(4, 5)] },
+ },
+ refMap: { a: { pattern: 'b', type: '' } },
+ },
+ },
+ ]);
+ expect(result).toEqual([
+ {
+ id: 'sheet.mutation.remove.numfmt',
+ params: {
+ ranges: [
+ {
+ startRow: 2,
+ endRow: 2,
+ startColumn: 3,
+ endColumn: 4,
+ },
+ {
+ startRow: 4,
+ endRow: 4,
+ startColumn: 5,
+ endColumn: 5,
+ },
+ ],
+ unitId: undefined,
+ subUnitId: undefined,
+ },
+ },
+ {
+ id: 'sheet.mutation.set.numfmt',
+ params: {
+ values: {
+ '1': {
+ ranges: [
+ {
+ startRow: 2,
+ endRow: 2,
+ startColumn: 3,
+ endColumn: 4,
+ },
+ {
+ startRow: 4,
+ endRow: 4,
+ startColumn: 5,
+ endColumn: 5,
+ },
+ ],
+ },
+ '2': {
+ ranges: [
+ {
+ startRow: 4,
+ endRow: 4,
+ startColumn: 5,
+ endColumn: 6,
+ },
+ ],
+ },
+ },
+ refMap: {
+ '1': {
+ pattern: 'a',
+ type: '',
+ },
+ '2': {
+ pattern: 'b',
+ type: '',
+ },
+ },
+ unitId: undefined,
+ subUnitId: undefined,
+ },
+ },
+ ]);
+ });
});
diff --git a/packages/sheets-numfmt/src/utils/mutation.ts b/packages/sheets-numfmt/src/utils/mutation.ts
index 949d5c52c7..5583d46836 100644
--- a/packages/sheets-numfmt/src/utils/mutation.ts
+++ b/packages/sheets-numfmt/src/utils/mutation.ts
@@ -15,6 +15,7 @@
*/
import type { IMutationInfo } from '@univerjs/core';
+import { Tools } from '@univerjs/core';
import type { IRemoveNumfmtMutationParams, ISetNumfmtMutationParams } from '@univerjs/sheets';
import { rangeMerge, RemoveNumfmtMutation, SetNumfmtMutation } from '@univerjs/sheets';
@@ -35,26 +36,36 @@ export const mergeNumfmtMutations = (list: IMutationInfo[]) => {
},
{
ranges: [],
- unitId: setMutation[0].unitId,
- subUnitId: setMutation[0].subUnitId,
+ unitId: removeMutation[0].unitId,
+ subUnitId: removeMutation[0].subUnitId,
} as IRemoveNumfmtMutationParams
);
params.ranges = rangeMerge(params.ranges);
result.push({ id: RemoveNumfmtMutation.id, params });
}
+ const findKeyFromObj = (obj: Record<string, any>, item: any) => {
+ const keys = Object.keys(obj);
+ const index = keys.findIndex((key) => {
+ const value = obj[key];
+ return Tools.diffValue(value, item);
+ });
+ return keys[index];
+ };
if (setMutation[0]) {
const params = setMutation.reduce(
(res, cur) => {
Object.keys(cur.values).forEach((key) => {
const curValue = cur.values[key];
const curRef = cur.refMap[key];
- if (res.values[key]) {
- res.values[key].ranges.push(...curValue.ranges);
+ const index = findKeyFromObj(res.refMap, curRef);
+ if (index) {
+ res.values[index].ranges.push(...curValue.ranges);
} else {
- res.values[key] = {
+ const newIndex = Math.max(...Object.keys(res.refMap).map(Number), 0) + 1;
+ res.values[newIndex] = {
ranges: curValue.ranges,
};
- res.refMap[key] = curRef;
+ res.refMap[newIndex] = curRef;
}
});
return res;