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(clipboard): fix paste col width (#3450)

ybzky2175c49cd2df
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index e7fd5d2412..183ad98286 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -65,6 +65,7 @@ import type {
IObjectMatrixPrimitiveType,
IRange,
IRowData,
+ Nullable,
Workbook,
Worksheet,
} from '@univerjs/core';
@@ -389,6 +390,9 @@ export class SheetClipboardController extends RxDisposable {
if (willSetHeight > nowRowHeight) {
rowHeight[index + range.rows[0]] = willSetHeight;
originRowHeight[index + range.rows[0]] = nowRowHeight;
+ } else {
+ rowHeight[index + range.rows[0]] = nowRowHeight;
+ originRowHeight[index + range.rows[0]] = nowRowHeight;
}
} else {
rowHeight[index + range.rows[0]] = willSetHeight;
@@ -442,6 +446,7 @@ export class SheetClipboardController extends RxDisposable {
const defaultColumnWidth = self._configService.getConfig<number>(DEFAULT_WORKSHEET_COLUMN_WIDTH_KEY) ?? DEFAULT_WORKSHEET_COLUMN_WIDTH;
const pasteToCols = range.cols;
+ const startColumn = pasteToCols[0];
if (addingColsCount > 0) {
const addColRange = {
@@ -491,14 +496,20 @@ export class SheetClipboardController extends RxDisposable {
...targetSetColPropertyParams,
colWidth: colProperties
.slice(0, existingColsCount)
- .map((property, index) => (property.width ? Math.max(+property.width, currentSheet!.getColumnWidth(pasteToCols[index])) : defaultColumnWidth)),
+ .reduce((p, c, index) => {
+ p[index + startColumn] = c.width ? Math.max(+c.width, currentSheet!.getColumnWidth(pasteToCols[index]) ?? defaultColumnWidth) : defaultColumnWidth;
+ return p;
+ }, {} as IObjectArrayPrimitiveType<number>),
};
const undoSetColPropertyParams: ISetWorksheetColWidthMutationParams = {
...targetSetColPropertyParams,
colWidth: colProperties
.slice(0, existingColsCount)
- .map((_property, index) => currentSheet!.getColumnWidth(pasteToCols[index]) ?? defaultColumnWidth),
+ .reduce((p, c, index) => {
+ p[index + startColumn] = currentSheet!.getColumnWidth(pasteToCols[index]) ?? defaultColumnWidth;
+ return p;
+ }, {} as IObjectArrayPrimitiveType<Nullable<number>>),
};
redoMutations.push({
@@ -705,6 +716,8 @@ export class SheetClipboardController extends RxDisposable {
const currentSheet = self._getWorksheet(unitId, subUnitId);
const { range } = pasteTo;
+ const pasteToCols = range.cols;
+ const startColumn = pasteToCols[0];
// if the range is outside ot the worksheet's boundary, we should add rows
const maxColumn = currentSheet!.getMaxColumns();
const addingColsCount = range.cols[range.cols.length - 1] - maxColumn;
@@ -722,20 +735,39 @@ export class SheetClipboardController extends RxDisposable {
}],
colWidth: colProperties
.slice(0, existingColsCount)
- .map((property) => (property.width ? +property.width : defaultColumnWidth)),
+ .reduce((p, c, index) => {
+ p[index + startColumn] = c.width ? Math.max(+c.width, currentSheet!.getColumnWidth(pasteToCols[index]) ?? defaultColumnWidth) : defaultColumnWidth;
+ return p;
+ }, {} as IObjectArrayPrimitiveType<number>),
+ };
+
+ const undoSetColPropertyMutation: ISetWorksheetColWidthMutationParams = {
+ unitId: unitId!,
+ subUnitId: subUnitId!,
+ ranges: [{
+ startRow: range.rows[0],
+ endRow: Math.min(range.cols[range.cols.length - 1], maxColumn),
+ startColumn: range.cols[0],
+ endColumn: range.cols[range.cols.length - 1],
+ }],
+ colWidth: colProperties
+ .slice(0, existingColsCount)
+ .reduce((p, c, index) => {
+ p[index + startColumn] = currentSheet!.getColumnWidth(pasteToCols[index]) ?? defaultColumnWidth;
+ return p;
+ }, {} as IObjectArrayPrimitiveType<Nullable<number>>),
};
+
redoMutations.push({
id: SetWorksheetColWidthMutation.id,
params: setColPropertyMutation,
});
- redoMutations.push({
+ undoMutations.push({
id: SetWorksheetColWidthMutation.id,
- params: setColPropertyMutation,
+ params: undoSetColPropertyMutation,
});
- // TODO: add undo mutations but we cannot do it now because underlying mechanism is not ready
-
return {
redos: redoMutations,
undos: undoMutations,
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-paste-col-width.spec.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-paste-col-width.spec.ts
new file mode 100644
index 0000000000..ef6f33b0b6
--- /dev/null
+++ b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-paste-col-width.spec.ts
@@ -0,0 +1,156 @@
+/**
+ * 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 { ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE } from '@univerjs/core';
+import {
+ AddWorksheetMergeMutation,
+ RemoveWorksheetMergeMutation,
+ SetRangeValuesMutation,
+ SetSelectionsOperation,
+ SetWorksheetColWidthMutation,
+ SetWorksheetRowHeightMutation,
+ SheetsSelectionsService,
+} from '@univerjs/sheets';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import type { Injector, Univer } from '@univerjs/core';
+import { SheetSkeletonManagerService } from '../../sheet-skeleton-manager.service';
+import { ISheetClipboardService } from '../clipboard.service';
+import { parseTableRows } from '../html-to-usm/converter';
+import { clipboardTestBed } from './clipboard-test-bed';
+
+const colWidthHtml = `<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=ProgId content=Excel.Sheet><meta name=Generator content="Microsoft Excel 15"><link id=Main-File rel=Main-File href="file:////Users/yuanbin/Library/Group%20Containers/UBF8T346G9.Office/TemporaryItems/msohtmlclip/clip.htm"><link rel=File-List href="file:////Users/yuanbin/Library/Group%20Containers/UBF8T346G9.Office/TemporaryItems/msohtmlclip/clip_filelist.xml"><style><!--table{mso-displayed-decimal-separator:"\.";mso-displayed-thousand-separator:"\,";}@page{margin:1.0in .75in 1.0in .75in;mso-header-margin:.5in;mso-footer-margin:.5in;}.font5{color:windowtext;font-size:9.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;}tr{mso-height-source:auto;mso-ruby-visibility:none;}col{mso-width-source:auto;mso-ruby-visibility:none;}br{mso-data-placement:same-cell;}td{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:black;font-size:11.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:general;vertical-align:middle;border:none;mso-background-source:auto;mso-pattern:auto;mso-protection:locked visible;white-space:nowrap;mso-rotate:0;}.xl65{text-align:center;vertical-align:bottom;}ruby{ruby-align:left;}rt{color:windowtext;font-size:9.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-char-type:none;display:none;}--></style></head><body link=blue vlink=purple><table border=0 cellpadding=0 cellspacing=0 width=2573 style='border-collapse:collapse;width:1931pt'><col width=573 style='mso-width-source:userset;mso-width-alt:18346;width:430pt'><col width=415 style='mso-width-source:userset;mso-width-alt:13269;width:311pt'><col width=441 style='mso-width-source:userset;mso-width-alt:14122;width:331pt'><col width=73 span=3 style='width:55pt'><col width=713 style='mso-width-source:userset;mso-width-alt:22826;width:535pt'><col width=139 style='mso-width-source:userset;mso-width-alt:4437;width:104pt'><col width=73 style='width:55pt'><tr height=67 style='mso-height-source:userset;height:50.0pt'><!--StartFragment--><td height=67 width=573 style='height:50.0pt;width:430pt'>test col width<span style='mso-spacerun:yes'> </span></td><td width=415 style='width:311pt'>test col width</td><td width=441 style='width:331pt'></td><td colspan=2 width=146 style='mso-ignore:colspan;width:110pt'>col width</td><td class=xl65 width=73 style='width:55pt'></td><td width=713 style='width:535pt'>col width</td><td width=139 style='width:104pt'></td><td width=73 style='width:55pt'></td><!--EndFragment--></tr></table></body></html>`;
+
+const rowHeightFloatHtml = `<meta charset='utf-8'><html><head></head><body><google data-copy-id="Uv1RxY" -sheets-html-origin=""><table xmlns="http://www.w3.org/1999/xhtml" cellspacing="0" cellpadding="0" dir="ltr" style="table-layout:fixed;font-size:10pt;font-family:Arial;width:0px;border-collapse:collapse;border:none"><colgroup><col width="88"><col width="88"><col width="88"></colgroup><tbody><tr style="height: 73.546875px;"><td>qweqwe</td><td></td><td></td></tr></tbody></table></google></body></html>`;
+
+describe('Test clipboard', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+ let sheetClipboardService: ISheetClipboardService;
+ let sheetSkeletonManagerService: SheetSkeletonManagerService;
+
+ beforeEach(async () => {
+ const testBed = clipboardTestBed({
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ cellData: {
+ 0: {
+ 0: {
+ v: 1,
+ },
+ },
+ },
+ columnCount: 100,
+ rowCount: 100,
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ styles: {},
+ });
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(SetRangeValuesMutation);
+ commandService.registerCommand(SetWorksheetRowHeightMutation);
+ commandService.registerCommand(SetWorksheetColWidthMutation);
+ commandService.registerCommand(AddWorksheetMergeMutation);
+ commandService.registerCommand(RemoveWorksheetMergeMutation);
+ commandService.registerCommand(SetSelectionsOperation);
+ sheetSkeletonManagerService = get(SheetSkeletonManagerService);
+ sheetClipboardService = get(ISheetClipboardService);
+ });
+
+ afterEach(() => {
+ univer?.dispose();
+ });
+
+ describe('Test paste col width ', () => {
+ beforeEach(() => {
+ const selectionManager = get(SheetsSelectionsService);
+
+ const startRow = 10;
+ const startColumn = 10;
+ const endRow = 10;
+ const endColumn = 10;
+
+ selectionManager.addSelections([
+ {
+ range: { startRow, startColumn, endRow, endColumn, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ sheetSkeletonManagerService.setCurrent({
+ sheetId: 'sheet1',
+ });
+ });
+ it('test col width', async () => {
+ const worksheet = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1');
+ if (!worksheet) return false;
+ const res = await sheetClipboardService.legacyPaste(colWidthHtml);
+ expect(res).toBeTruthy();
+ const columnManager = worksheet.getColumnManager();
+ expect(columnManager.getColumnWidth(10)).toBe(573);
+ expect(columnManager.getColumnWidth(11)).toBe(415);
+ expect(columnManager.getColumnWidth(12)).toBe(441);
+ expect(columnManager.getColumnWidth(13)).toBe(88);
+ expect(columnManager.getColumnWidth(16)).toBe(713);
+ expect(columnManager.getColumnWidth(17)).toBe(139);
+ expect(columnManager.getColumnWidth(18)).toBe(88);
+ });
+ });
+
+ describe('Test paste row height ', () => {
+ beforeEach(() => {
+ const selectionManager = get(SheetsSelectionsService);
+
+ const startRow = 10;
+ const startColumn = 10;
+ const endRow = 10;
+ const endColumn = 10;
+
+ selectionManager.addSelections([
+ {
+ range: { startRow, startColumn, endRow, endColumn, rangeType: RANGE_TYPE.NORMAL },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ sheetSkeletonManagerService.setCurrent({
+ sheetId: 'sheet1',
+ });
+ });
+ it('test row height', async () => {
+ const worksheet = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1');
+ if (!worksheet) return false;
+ const res = parseTableRows(rowHeightFloatHtml);
+ expect(Number(res.rowProperties[0]?.height)).toBe(73);
+ const pasteRes = await sheetClipboardService.legacyPaste(rowHeightFloatHtml);
+ expect(pasteRes).toBeTruthy();
+ const rowManager = worksheet.getRowManager();
+ expect(rowManager.getRowHeight(10)).toBe(73);
+ });
+ });
+});
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index 829ebdf070..57cd7c4e5c 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -474,7 +474,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
return res;
}
- // eslint-disable-next-line max-lines-per-function
+ // eslint-disable-next-line max-lines-per-function, complexity
private async _pasteInternal(copyId: string, pasteType: string): Promise<boolean> {
// const target = this._getPastingTarget();
// const { selection, unitId, subUnitId } = target;
@@ -550,7 +550,9 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
range.rows.forEach((j) => {
const row = rowManager.getRowOrCreate(j);
- rowProperties.push({ height: `${row.ah || row.h || defaultRowHeight}` });
+ const { ah = defaultRowHeight, h = defaultRowHeight } = row;
+ const height = Math.max(ah, h);
+ rowProperties.push({ height: `${height}` });
});
if (cachedData.copyType === COPY_TYPE.CUT) {
diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
index 5acab6feef..8a0f499972 100644
--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
@@ -647,7 +647,7 @@ export class HtmlToUSMService {
* @param html raw content
* @returns
*/
-function parseTableRows(html: string): {
+export function parseTableRows(html: string): {
rowProperties: IClipboardPropertyItem[];
rowCount: number;
} {
@@ -664,7 +664,7 @@ function parseTableRows(html: string): {
const rowProperties = rowMatchesAsArray.map((rowMatch) => parseProperties(rowMatch[1])).map((properties) => {
if (!properties.height) {
const style = properties.style;
- const match = style && style.match(/height\s*:\s*(\d+)px/);
+ const match = style && style.match(/height\s*:\s*(\d+(\.\d+)?)px/);
properties.height = `${match ? Number.parseInt(match[1], 10) : DEFAULT_WORKSHEET_ROW_HEIGHT}`;
}
return properties;
diff --git a/packages/sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts b/packages/sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts
index 1020bbfc31..0bf8043cd3 100644
--- a/packages/sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts
+++ b/packages/sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { BooleanNumber, IMutation, IObjectArrayPrimitiveType, IRange, IRowAutoHeightInfo, Nullable, Worksheet } from '@univerjs/core';
import { CommandType, IUniverInstanceService } from '@univerjs/core';
+import type { BooleanNumber, IMutation, IObjectArrayPrimitiveType, IRange, IRowAutoHeightInfo, Nullable, Worksheet } from '@univerjs/core';
import { getSheetCommandTarget } from '../commands/utils/target-util';
const MAXIMUM_ROW_HEIGHT = 2000;
@@ -121,9 +121,8 @@ export const SetWorksheetRowHeightMutation: IMutation<ISetWorksheetRowHeightMuta
if (!target) return false;
const { worksheet } = target;
- const defaultRowHeight = worksheet.getConfig().defaultRowHeight;
const manager = worksheet.getRowManager();
-
+ const defaultRowHeight = worksheet.getConfig().defaultRowHeight;
for (const { startRow, endRow } of ranges) {
for (let rowIndex = startRow; rowIndex <= endRow; rowIndex++) {
const row = manager.getRowOrCreate(rowIndex);