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(sheets-ui): recalculate auto row height after paste (#7770)

wpxp123456fdf5eed0d111
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard.service.spec.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard.service.spec.ts
index e017522473..bfa6681323 100644
--- a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard.service.spec.ts
+++ b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard.service.spec.ts
@@ -48,10 +48,11 @@ import {
SheetsSelectionsService,
WorkbookEditablePermission,
} from '@univerjs/sheets';
-import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
SheetPermissionInterceptorClipboardController,
} from '../../../controllers/permission/sheet-permission-interceptor-clipboard.controller';
+import { SheetSkeletonManagerService } from '../../sheet-skeleton-manager.service';
import { ISheetClipboardService, PREDEFINED_HOOK_NAME_PASTE } from '../clipboard.service';
import { COPY_TYPE } from '../type';
import { clipboardTestBed } from './clipboard-test-bed';
@@ -243,6 +244,67 @@ describe('Test clipboard', () => {
expect(getValues(0, 1, 0, 1)).toEqual(originalPeer);
});
+ it('recalculates auto height when pasting into a row that already has a measured auto height', async () => {
+ const row = 30;
+ const worksheet = get(IUniverInstanceService)
+ .getUnit<Workbook>('test', UniverInstanceType.UNIVER_SHEET)!
+ .getSheetBySheetId('sheet1')!;
+ worksheet.getRowManager().getRowOrCreate(row).ah = 24;
+ const skeleton = get(SheetSkeletonManagerService).ensureSkeleton('sheet1')!;
+ vi.spyOn(skeleton, 'calculateAutoHeightInRange').mockReturnValue([{ row, autoHeight: 34 }]);
+ const copyId = 'existing-auto-height';
+ sheetClipboardService.copyContentCache().set(copyId, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ range: { rows: [0], cols: [0] },
+ matrix: new ObjectMatrix({ 0: { 0: { v: 'line 1\nline 2' } } }),
+ copyType: COPY_TYPE.COPY,
+ });
+
+ expect(await sheetClipboardService.pasteByCopyId(copyId, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ range: { startRow: row, endRow: row, startColumn: 0, endColumn: 0 },
+ })).toBe(true);
+ expect(worksheet.getRowManager().getRow(row)?.ah).toBe(34);
+ expect(await commandService.executeCommand(UndoCommand.id)).toBe(true);
+ expect(worksheet.getRowManager().getRow(row)?.ah).toBe(24);
+ expect(await commandService.executeCommand(RedoCommand.id)).toBe(true);
+ expect(worksheet.getRowManager().getRow(row)?.ah).toBe(34);
+ });
+
+ it.each([
+ { name: 'explicit', row: 31, isLegacy: false },
+ { name: 'legacy', row: 32, isLegacy: true },
+ ])('preserves a $name manually sized row when pasting', async ({ row, isLegacy }) => {
+ const worksheet = get(IUniverInstanceService)
+ .getUnit<Workbook>('test', UniverInstanceType.UNIVER_SHEET)!
+ .getSheetBySheetId('sheet1')!;
+ const rowData = worksheet.getRowManager().getRowOrCreate(row);
+ rowData.h = 40;
+ if (!isLegacy) {
+ rowData.ia = BooleanNumber.FALSE;
+ }
+ const skeleton = get(SheetSkeletonManagerService).ensureSkeleton('sheet1')!;
+ const calculateAutoHeight = vi.spyOn(skeleton, 'calculateAutoHeightInRange');
+ const copyId = 'manual-row-height';
+ sheetClipboardService.copyContentCache().set(copyId, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ range: { rows: [0], cols: [0] },
+ matrix: new ObjectMatrix({ 0: { 0: { v: 'line 1\nline 2' } } }),
+ copyType: COPY_TYPE.COPY,
+ });
+
+ expect(await sheetClipboardService.pasteByCopyId(copyId, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ range: { startRow: row, endRow: row, startColumn: 0, endColumn: 0 },
+ })).toBe(true);
+ expect(calculateAutoHeight).not.toHaveBeenCalled();
+ expect(worksheet.getRowHeight(row)).toBe(40);
+ });
+
it.each(['disposed', 'read-only'] as const)('does not redirect a delayed paste when its source becomes %s', async (state) => {
const clipboardService = get(ISheetClipboardService);
get(SheetPermissionInterceptorClipboardController);
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index 9f898f408d..35806d75c8 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -48,6 +48,7 @@ import type {
IUniverSheetCopyDataModel,
} from './type';
import {
+ BooleanNumber,
CellModeEnum,
cloneCellDataWithSpanAndDisplay,
createIdentifier,
@@ -1226,11 +1227,13 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
const pasteRangeStartColumn = cols[0];
const pasteRangeEndColumn = cols[cols.length - 1];
- // If there are rows without row height set in the pasted range, we need to set them to auto height
+ // Recalculate auto-height rows while preserving explicit and legacy manual row heights.
const needAutoHeight: IRowAutoHeightInfo[] = [];
for (let r = pasteRangeStartRow; r <= pasteRangeEndRow; r++) {
const rowData = rowManager.getRow(r);
- if (!rowData) {
+ const hasLegacyManualHeight = rowData?.ia == null && rowData?.ah == null && typeof rowData?.h === 'number';
+ const isManualHeight = rowData?.ia === BooleanNumber.FALSE || hasLegacyManualHeight;
+ if (!isManualHeight) {
const autoHeight = skeleton.calculateAutoHeightInRange([
{
startRow: r,