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: fix merge cell bug (#4898)

wpxp12345679bf4b83df63
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts
index ad78b41038..af3a2bf434 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts
@@ -15,6 +15,7 @@
*/
import type { IDisposable, Injector, IRange, Univer, Workbook } from '@univerjs/core';
+import type { IConfirmPartMethodOptions } from '@univerjs/ui';
import {
ICommandService,
IUniverInstanceService,
@@ -53,7 +54,7 @@ import {
SetSelectionsOperation,
SheetsSelectionsService,
} from '@univerjs/sheets';
-import { type IConfirmPartMethodOptions, IConfirmService } from '@univerjs/ui';
+import { IConfirmService } from '@univerjs/ui';
import { Subject } from 'rxjs';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
@@ -465,6 +466,50 @@ describe('Test add worksheet merge commands', () => {
expect(res.getValue(0, 2)?.p).toBeTruthy();
expect(res.getValue(2, 2)).toBeNull();
});
+
+ it('test merge range first cell is blank, second cell is percent number format', async () => {
+ const univerInstanceService = get(IUniverInstanceService);
+ const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
+ const worksheet = workbook.getActiveSheet();
+ if (!worksheet) throw new Error('No active sheet found');
+
+ expect(worksheet.getCellRaw(20, 0)).toBeUndefined();
+ expect(worksheet.getCellRaw(20, 1)).toStrictEqual({
+ v: 2,
+ t: 2,
+ s: {
+ n: {
+ pattern: '0%',
+ },
+ },
+ });
+
+ const selectionManager = get(SheetsSelectionsService);
+ selectionManager.addSelections([
+ {
+ range: {
+ startRow: 20,
+ startColumn: 0,
+ endRow: 21,
+ endColumn: 1,
+ rangeType: RANGE_TYPE.NORMAL,
+ },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ const commandService = get(ICommandService);
+ await commandService.executeCommand(AddWorksheetMergeAllCommand.id);
+
+ expect(worksheet.getCellRaw(20, 0)?.v).toStrictEqual(2);
+ expect(worksheet.getCellStyle(20, 0)).toStrictEqual({
+ n: {
+ pattern: '0%',
+ },
+ });
+ expect(worksheet.getCellRaw(20, 1)).toBeUndefined();
+ });
});
describe('with insert or remove col', () => {
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/create-command-test-bed.ts b/packages/sheets-ui/src/commands/commands/__tests__/create-command-test-bed.ts
index 1ebcb2f066..74614c9b5a 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/create-command-test-bed.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/create-command-test-bed.ts
@@ -18,6 +18,7 @@ import type { Dependency, IWorkbookData, Workbook } from '@univerjs/core';
import type { IRenderContext } from '@univerjs/engine-render';
import {
BooleanNumber,
+ CellValueType,
DisposableCollection,
ILogService,
Inject,
@@ -67,6 +68,18 @@ const getTestWorkbookDataDemo = (): IWorkbookData => {
v: 'A2',
},
},
+ // 20 test merge cell, do not change value here
+ 20: {
+ 1: {
+ v: 2,
+ t: CellValueType.NUMBER,
+ s: {
+ n: {
+ pattern: '0%',
+ },
+ },
+ },
+ },
},
columnData: {
1: {
diff --git a/packages/sheets-ui/src/common/utils.ts b/packages/sheets-ui/src/common/utils.ts
index 9966330e1d..303772849c 100644
--- a/packages/sheets-ui/src/common/utils.ts
+++ b/packages/sheets-ui/src/common/utils.ts
@@ -90,7 +90,7 @@ export function getClearContentMutationParamsForRanges(
export function getClearContentMutationParamForRange(worksheet: Worksheet, range: IRange): ObjectMatrix<Nullable<ICellData>> {
const { startRow, startColumn, endColumn, endRow } = range;
- const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn, CellModeEnum.Intercepted);
+ const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn, CellModeEnum.Raw);
const redoMatrix = new ObjectMatrix<Nullable<ICellData>>();
let leftTopCellValue: Nullable<ICellData> = null;
cellMatrix.forValue((row, col, cellData) => {
diff --git a/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts b/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts
index 2cff945070..72c082f2ac 100644
--- a/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts
+++ b/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts
@@ -106,7 +106,7 @@ function getClearContentMutationParamsForRanges(
function getClearContentMutationParamForRange(worksheet: Worksheet, range: IRange): ObjectMatrix<Nullable<ICellData>> {
const { startRow, startColumn, endColumn, endRow } = range;
- const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn, CellModeEnum.Intercepted);
+ const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn, CellModeEnum.Raw);
const redoMatrix = new ObjectMatrix<Nullable<ICellData>>();
cellMatrix.forValue((row, col, cellData) => {
if (cellData && (row !== startRow || col !== startColumn)) {