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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix: paste row height and col width (#1330)
YuHong60bd78a2062f
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.tsindex 41c2d0b001..4d3ab58be3 100644--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts@@ -282,28 +282,32 @@ export class SheetClipboardController extends RxDisposable {if (addingRowsCount > 0) {const rowInfo: IObjectArrayPrimitiveType<IRowData> = {};rowProperties.slice(existingRowsCount).forEach((property, index) => {- const style = property?.style;- if (!style) {- return;+ const { style, height: PropertyHeight } = property || {};+ if (style) {+ const cssTextArray = style.split(';');+ let height = DEFAULT_WORKSHEET_ROW_HEIGHT;++ cssTextArray.find((css) => {+ css = css.toLowerCase();+ const key = textTrim(css.substr(0, css.indexOf(':')));+ const value = textTrim(css.substr(css.indexOf(':') + 1));+ if (key === 'height') {+ height = Number.parseFloat(value);+ return true;+ }+ return false;+ });++ rowInfo[index] = {+ h: height,+ hd: BooleanNumber.FALSE,+ };+ } else if (PropertyHeight) {+ rowInfo[index] = {+ h: Number.parseFloat(PropertyHeight),+ hd: BooleanNumber.FALSE,+ };}- const cssTextArray = style.split(';');- let height = DEFAULT_WORKSHEET_ROW_HEIGHT;-- cssTextArray.find((css) => {- css = css.toLowerCase();- const key = textTrim(css.substr(0, css.indexOf(':')));- const value = textTrim(css.substr(css.indexOf(':') + 1));- if (key === 'height') {- height = Number.parseFloat(value);- return true;- }- return false;- });-- rowInfo[index] = {- h: height,- hd: BooleanNumber.FALSE,- };});const addRowMutation: IInsertRowMutationParams = {@@ -322,25 +326,26 @@ export class SheetClipboardController extends RxDisposable {// TODO When Excel pasted, there was no width height, Do we still need to set the height?const rowHeight: IObjectArrayPrimitiveType<number> = {};rowProperties.slice(0, existingRowsCount).forEach((property, index) => {- const style = property.style;- if (!style) {- return;- }- const cssTextArray = style.split(';');- let height = DEFAULT_WORKSHEET_ROW_HEIGHT;-- cssTextArray.find((css) => {- css = css.toLowerCase();- const key = textTrim(css.substr(0, css.indexOf(':')));- const value = textTrim(css.substr(css.indexOf(':') + 1));- if (key === 'height') {- height = Number.parseFloat(value);- return true;- }- return false;- });+ const { style, height: propertyHeight } = property;+ if (style) {+ const cssTextArray = style.split(';');+ let height = DEFAULT_WORKSHEET_ROW_HEIGHT;++ cssTextArray.find((css) => {+ css = css.toLowerCase();+ const key = textTrim(css.substr(0, css.indexOf(':')));+ const value = textTrim(css.substr(css.indexOf(':') + 1));+ if (key === 'height') {+ height = Number.parseFloat(value);+ return true;+ }+ return false;+ });- rowHeight[index] = height;+ rowHeight[index + range.startRow] = height;+ } else if (propertyHeight) {+ rowHeight[index + range.startRow] = Number.parseFloat(propertyHeight);+ }});// apply row properties to the existing rowsdiff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-service.spec.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-service.spec.tsindex 21837a3504..60641540a2 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@@ -156,6 +156,12 @@ describe('Test clipboard', () => {const values = getValues(startRow, startColumn, endRow, endColumn);const styles = getStyles(startRow, startColumn, endRow, endColumn);const mergedCells = getMergedCells(startRow, startColumn, endRow, endColumn);+ const rowManager = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1')?.getRowManager();+ const rowHeight = rowManager?.getRowData()?.[0].h;+ const columnManager = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1')?.getColumnManager();+ const columnWidth = columnManager?.getColumnData()?.[0].w;+ expect (columnWidth).toBe(73);+ expect (rowHeight).toBe(81);expect(values && values[0][0]?.v).toBe('row1col2');expect(styles && styles[0][0]).toStrictEqual({bg: { rgb: 'rgb(255,0,0)' },diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.tsindex d724ef2651..62689937ba 100644--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts@@ -409,20 +409,29 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardif (!worksheet) {return false;}- const manager = worksheet.getColumnManager();+ const colManager = worksheet.getColumnManager();+ const rowManager = worksheet.getRowManager();const defaultColumnWidth = worksheet.getConfig().defaultColumnWidth;+ const defaultRowHeight = worksheet.getConfig().defaultRowHeight;const colProperties = [];+ const rowProperties = [];for (let i = startColumn; i <= endColumn; i++) {- const column = manager.getColumnOrCreate(i);+ const column = colManager.getColumnOrCreate(i);colProperties.push({ width: `${column.w || defaultColumnWidth}` });}+ for (let j = startRow; j <= endRow; j++) {+ const row = rowManager.getRowOrCreate(j);+ rowProperties.push({ height: `${row.h || defaultRowHeight}` });+ }+const pasteRes = this._pasteUSM({cellMatrix,colProperties,+ rowProperties,}, // paste data{unitId, // paste targetdiff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.tsindex 5bceb9a838..1c56e7f254 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@@ -101,7 +101,7 @@ export class HtmlToUSMService {};const rowProperties: IClipboardPropertyItem[] = [];- let colProperties: IClipboardPropertyItem[] = [];+ const colProperties: IClipboardPropertyItem[] = [];// pick tablesconst tableStrings = html.match(/<table\b[^>]*>([\s\S]*?)<\/table>/gi);const tables: IParsedTablesInfo[] = [];@@ -154,13 +154,13 @@ export class HtmlToUSMService {rowProperties: tableRowProp,} = this._parseTable(tableString!);- if (tableColProp) {- colProperties = tableColProp;- }cellMatrix &&cellMatrix.forValue((row, col, value) => {valueMatrix.setValue(curRow + row, col, value);});+ if (tableColProp) {+ colProperties.push(...tableColProp);+ }rowProperties.push(...tableRowProp);}});@@ -187,7 +187,7 @@ export class HtmlToUSMService {if (tableStrings) {tableStrings.forEach((t) => {const curRow = valueMatrix.getDataRange().endRow + 1;- const { cellMatrix } = this._parseTable(t!);+ const { cellMatrix, rowProperties: tableRowProp, colProperties: tableColProp } = this._parseTable(t!);if (cellMatrix) {cellMatrix.forValue((row, col, value) => {const { rowSpan = 1, colSpan = 1 } = value;@@ -201,14 +201,16 @@ export class HtmlToUSMService {valueMatrix.setValue(curRow + row, col, value);});}-- rowProperties.push(...rowProperties);+ if (tableColProp) {+ colProperties.push(...tableColProp);+ }+ rowProperties.push(...tableRowProp);});}}return {- rowProperties: [],- colProperties: [],+ rowProperties,+ colProperties,cellMatrix: valueMatrix,};}@@ -244,8 +246,8 @@ export class HtmlToUSMService {}});return {- rowProperties: [],- colProperties: [],+ rowProperties,+ colProperties,cellMatrix: valueMatrix,};}