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(sheets): keep Excel overflow cells unmerged (#8171)
Zerui Ge54427614f3ae
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-paste-form-excel.spec.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-paste-form-excel.spec.tsindex d69d29b669..26efb0f0b7 100644--- a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-paste-form-excel.spec.ts+++ b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-paste-form-excel.spec.ts@@ -173,6 +173,23 @@ describe('Test clipboard', () => {sheetId: 'sheet1',});});+ it('pastes Excel overflow without merging cells or shifting later columns', async () => {+ const worksheet = get(IUniverInstanceService)+ .getUnit<Workbook>('test', UniverInstanceType.UNIVER_SHEET)!+ .getSheetBySheetId('sheet1')!;+ const html = `+ <table>+ <tr><td colspan="2" style="mso-ignore:colspan">Long label</td><td>Next</td></tr>+ <tr><td>First</td><td>Second</td><td>Third</td></tr>+ </table>+ `;++ await expect(sheetClipboardService.legacyPaste(html)).resolves.toBe(true);+ expect(worksheet.getMergeData()).toEqual([]);+ expect(getValues(1, 1, 1, 3)?.[0]?.map((cell) => cell?.v)).toEqual(['Long label', undefined, 'Next']);+ expect(getValues(2, 1, 2, 3)?.[0]?.map((cell) => cell?.v)).toEqual(['First', 'Second', 'Third']);+ });+it('test style with paste cell style', async () => {const worksheet = get(IUniverInstanceService).getUnit<Workbook>('test', UniverInstanceType.UNIVER_SHEET)?.getSheetBySheetId('sheet1');if (!worksheet) return false;diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/__tests__/converter.spec.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/__tests__/converter.spec.tsindex cf44c82f04..7cae2c385c 100644--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/__tests__/converter.spec.ts+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/__tests__/converter.spec.ts@@ -184,4 +184,48 @@ describe('HtmlToUSMService', () => {expect(cellMatrix.getValue(2, 0)?.v).toBe('right ');expect(cellMatrix.getValue(3, 0)).toBeUndefined();});++ // Real Excel output keeps the marker between other declarations, and `mso-ignore` takes a property list.+ it.each([+ 'mso-ignore:colspan',+ 'color: red; MSO-IGNORE: COLSPAN;',+ 'height:14.0pt;mso-ignore:colspan;width:104pt',+ 'mso-ignore: colspan padding',+ 'mso-ignore:rowspan,colspan',+ ])('keeps Excel overflow cells separate: %s', (style) => {+ const converter = new HtmlToUSMService({ getCurrentSkeleton: () => null });+ const { cellMatrix } = converter.convert(`+ <table>+ <tr><td colspan="2" style="${style}">Long label</td><td>Next</td></tr>+ <tr><td>First</td><td>Second</td><td>Third</td></tr>+ </table>+ `);++ expect(cellMatrix.getValue(0, 0)).toMatchObject({ v: 'Long label', colSpan: 1, rowSpan: 1 });+ expect(cellMatrix.getValue(0, 1)?.v).toBeUndefined();+ expect(cellMatrix.getValue(0, 2)?.v).toBe('Next');+ expect(cellMatrix.getValue(1, 0)?.v).toBe('First');+ expect(cellMatrix.getValue(1, 1)?.v).toBe('Second');+ expect(cellMatrix.getValue(1, 2)?.v).toBe('Third');+ });++ it.each([+ '',+ 'mso-ignore:padding',+ '--mso-ignore:colspan',+ 'mso-ignore-colspan:1',+ 'mso-ignore:colspanx',+ ])('preserves real merged cells: %s', (style) => {+ const converter = new HtmlToUSMService({ getCurrentSkeleton: () => null });+ const { cellMatrix } = converter.convert(`+ <table>+ <tr><td colspan="2" rowspan="2" style="${style}">Merged</td><td>Next</td></tr>+ <tr><td>Below</td></tr>+ </table>+ `);++ expect(cellMatrix.getValue(0, 0)).toMatchObject({ v: 'Merged', colSpan: 2, rowSpan: 2 });+ expect(cellMatrix.getValue(0, 2)?.v).toBe('Next');+ expect(cellMatrix.getValue(1, 2)?.v).toBe('Below');+ });});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.tsindex 70d11aedb2..3b8b7294ba 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@@ -19,13 +19,6 @@import type { ICustomRange, IDocumentBody, IDocumentData, IStyleData, ITextRun, ITextStyle, Nullable } from '@univerjs/core';import type { SpreadsheetSkeleton } from '@univerjs/engine-render';import type { ISheetSkeletonManagerParam } from '@univerjs/sheets';-import type {- ICellDataWithSpanInfo,- IClipboardPropertyItem,- IParsedCellValueByClipboard,- IUniverSheetCopyDataModel,-} from '../type';-import type { IAfterProcessRule, IPastePlugin } from './paste-plugins/type';import {createDocumentModelWithStyle,createParagraphId,@@ -40,6 +33,13 @@ import {skipParseTagNames,} from '@univerjs/core';import { handleStringToStyle, textTrim } from '@univerjs/ui';+import type {+ ICellDataWithSpanInfo,+ IClipboardPropertyItem,+ IParsedCellValueByClipboard,+ IUniverSheetCopyDataModel,+} from '../type';+import type { IAfterProcessRule, IPastePlugin } from './paste-plugins/type';import { extractNodeStyle } from './parse-node-style';import parseToDom, { convertToCellStyle, generateParagraphs } from './utils';@@ -500,6 +500,7 @@ export class HtmlToUSMService {let cellStyle = '';const rowSpan = Number(cell.getAttribute('rowSpan')) || 1;const colSpan = Number(cell.getAttribute('colSpan')) || 1;+ const ignoreColSpan = isIgnoredColSpan(cell);cellStyle = this._getStyle(cell as HTMLElement, rowStyle);@@ -572,7 +573,8 @@ export class HtmlToUSMService {const cellValue = {rowSpan,- colSpan,+ // Excel uses ignored spans for text overflow. Keep their layout width without creating a merge.+ colSpan: ignoreColSpan ? 1 : colSpan,content: cellText,style: cellStyle,richTextParma: {@@ -991,6 +993,35 @@ function normalizePlainCellText(input: string): string {return input.replace(/[\r\n]/g, '').replace(/\u00A0/g, ' ');}+/**+ * Excel marks text-overflow spans with `mso-ignore:colspan`, meaning the span only keeps the+ * visual width and must not become a merged range. `mso-*` declarations are dropped by CSSOM,+ * so the raw attribute is the only place the marker survives.+ */+function isIgnoredColSpan(cell: Element): boolean {+ const style = cell.getAttribute('style');+ if (!style) {+ return false;+ }++ return style.split(';').some((declaration) => {+ const separator = declaration.indexOf(':');+ if (separator === -1) {+ return false;+ }++ // Compare the whole name so the `--mso-ignore` custom property does not match.+ if (textTrim(declaration.slice(0, separator)).toLowerCase() !== 'mso-ignore') {+ return false;+ }++ return textTrim(declaration.slice(separator + 1))+ .toLowerCase()+ .split(/[,\s]+/)+ .some((property) => property === 'colspan');+ });+}+function extractStyleProperty(styleString?: string, propertyName?: string) {if (!styleString || !propertyName) return null;const regex = new RegExp(`(${propertyName}\\s*:\\s*[^;]+);`, 'i');