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 horizontal merge wrong value (#3545)

ybzkyb1ea6c3c98c9
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 175fff30d1..5d789950f7 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
@@ -268,6 +268,59 @@ describe('Test add worksheet merge commands', () => {
expect(result).toBeFalsy();
});
});
+
+ describe('horizontal with other merge cell', () => {
+ it('test only the upper left conner has a value', async () => {
+ const selectionManager = get(SheetsSelectionsService);
+ selectionManager.addSelections([
+ {
+ range: { startRow: 10, startColumn: 2, endColumn: 2, endRow: 12, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ function getMerge(): IRange[] | undefined {
+ return get(IUniverInstanceService)
+ .getUniverSheetInstance('test')
+ ?.getSheetBySheetId('sheet1')
+ ?.getConfig()
+ .mergeData;
+ }
+
+ expect(getMerge()?.length).toBe(0);
+ expect(await commandService.executeCommand(AddWorksheetMergeAllCommand.id)).toBeTruthy();
+ expect(getMerge()?.length).toBe(1);
+ commandService.executeCommand(SetRangeValuesMutation.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ cellValue: {
+ 10: {
+ 2: {
+ v: 1234,
+ },
+ },
+ },
+ });
+
+ selectionManager.clear();
+ selectionManager.addSelections([
+ {
+ range: { startRow: 10, startColumn: 1, endColumn: 2, endRow: 12, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+ expect(await commandService.executeCommand(AddWorksheetMergeHorizontalCommand.id)).toBeTruthy();
+ expect(getMerge()?.length).toBe(3);
+
+ const worksheet = get(IUniverInstanceService).getUnit<Workbook>('test')?.getSheetBySheetId('sheet1');
+ if (worksheet) {
+ expect(worksheet.getCellMatrix().getValue(10, 1)?.v).toBe(1234);
+ expect(worksheet.getCellMatrix().getValue(10, 2)?.v).toBeUndefined();
+ }
+ });
+ });
});
describe('cancel merge', () => {
diff --git a/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts b/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts
index c7e98e17dc..148359e76f 100644
--- a/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts
+++ b/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts
@@ -14,7 +14,8 @@
* limitations under the License.
*/
-import type { IAccessor, ICommand, IMutationInfo, IRange, Workbook } from '@univerjs/core';
+/* eslint-disable max-lines-per-function */
+
import {
CommandType,
Dimension,
@@ -23,9 +24,9 @@ import {
IUniverInstanceService,
LocaleService,
sequenceExecute,
+ Tools,
UniverInstanceType,
} from '@univerjs/core';
-import type { IAddWorksheetMergeMutationParams, IRemoveWorksheetMergeMutationParams } from '@univerjs/sheets';
import {
AddMergeRedoSelectionsOperationFactory,
AddMergeUndoMutationFactory,
@@ -36,6 +37,8 @@ import {
RemoveWorksheetMergeMutation, SheetsSelectionsService,
} from '@univerjs/sheets';
import { IConfirmService } from '@univerjs/ui';
+import type { IAccessor, ICommand, IMutationInfo, IRange, Workbook } from '@univerjs/core';
+import type { IAddWorksheetMergeMutationParams, IRemoveWorksheetMergeMutationParams } from '@univerjs/sheets';
import { checkCellContentInRanges, getClearContentMutationParamsForRanges } from '../../common/utils';
import { getMergeableSelectionsByType, MergeType } from './utils/selection-utils';
@@ -88,12 +91,12 @@ export const AddWorksheetMergeCommand: ICommand = {
const removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {
unitId,
subUnitId,
- ranges,
+ ranges: Tools.deepClone(ranges),
};
const addMergeMutationParams: IAddWorksheetMergeMutationParams = {
unitId,
subUnitId,
- ranges,
+ ranges: Tools.deepClone(ranges),
};
// prepare undo mutations
diff --git a/packages/sheets-ui/src/common/utils.ts b/packages/sheets-ui/src/common/utils.ts
index 656ff75a3f..6c041ab7a5 100644
--- a/packages/sheets-ui/src/common/utils.ts
+++ b/packages/sheets-ui/src/common/utils.ts
@@ -94,7 +94,7 @@ export function getClearContentMutationParamForRange(worksheet: Worksheet, range
const redoMatrix = new ObjectMatrix<Nullable<ICellData>>();
let leftTopCellValue: Nullable<ICellData> = null;
cellMatrix.forValue((row, col, cellData) => {
- if (cellData) {
+ if (cellData && row >= startRow && col >= startColumn) {
if (!leftTopCellValue && worksheet.cellHasValue(cellData)) {
leftTopCellValue = cellData;
}
diff --git a/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts b/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts
index 4191238b6e..d876bcc9f1 100644
--- a/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts
+++ b/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { CommandType, IUniverInstanceService } from '@univerjs/core';
+import { CommandType, IUniverInstanceService, Tools } from '@univerjs/core';
import type { IAccessor, IMutation } from '@univerjs/core';
import type {
@@ -36,7 +36,7 @@ export const AddMergeUndoMutationFactory = (
return {
unitId: params.unitId,
subUnitId: params.subUnitId,
- ranges: params.ranges,
+ ranges: Tools.deepClone(params.ranges),
};
};