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(paste): fix some number formatted text copied from Excel was not parsed correctly (#6844)

wpxp123456f1714504ab0f
diff --git a/packages/docs-ui/src/services/clipboard/clipboard.service.ts b/packages/docs-ui/src/services/clipboard/clipboard.service.ts
index 5dc87e3b06..15a8da6d6a 100644
--- a/packages/docs-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/docs-ui/src/services/clipboard/clipboard.service.ts
@@ -55,14 +55,14 @@ import { CutContentCommand, InnerPasteCommand } from '../../commands/commands/cl
import { getCursorWhenDelete } from '../../commands/commands/doc-delete.command';
import { copyContentCache, extractId, genId } from './copy-content-cache';
import { HtmlToUDMService } from './html-to-udm/converter';
-import PastePluginLark from './html-to-udm/paste-plugins/plugin-lark';
-import PastePluginUniver from './html-to-udm/paste-plugins/plugin-univer';
-import PastePluginWord from './html-to-udm/paste-plugins/plugin-word';
+import LarkPastePlugin from './html-to-udm/paste-plugins/plugin-lark';
+import UniverPastePlugin from './html-to-udm/paste-plugins/plugin-univer';
+import WordPastePlugin from './html-to-udm/paste-plugins/plugin-word';
import { UDMToHtmlService } from './udm-to-html/convertor';
-HtmlToUDMService.use(PastePluginWord);
-HtmlToUDMService.use(PastePluginLark);
-HtmlToUDMService.use(PastePluginUniver);
+HtmlToUDMService.use(LarkPastePlugin);
+HtmlToUDMService.use(UniverPastePlugin);
+HtmlToUDMService.use(WordPastePlugin);
export interface IClipboardPropertyItem { }
diff --git a/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-lark.ts b/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-lark.ts
index f12be9759e..0bc7dd09be 100644
--- a/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-lark.ts
+++ b/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-lark.ts
@@ -18,7 +18,7 @@ import type { IPastePlugin } from './type';
import { BooleanNumber } from '@univerjs/core';
import { extractNodeStyle as getInlineStyle } from '../parse-node-style';
-const wordPastePlugin: IPastePlugin = {
+const LarkPastePlugin: IPastePlugin = {
name: 'univer-doc-paste-plugin-lark',
checkPasteType(html: string) {
return /lark-record-clipboard/i.test(html);
@@ -60,4 +60,4 @@ const wordPastePlugin: IPastePlugin = {
],
};
-export default wordPastePlugin;
+export default LarkPastePlugin;
diff --git a/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-univer.ts b/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-univer.ts
index 90896284fa..6f1c9b4f3e 100644
--- a/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-univer.ts
+++ b/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-univer.ts
@@ -18,7 +18,7 @@ import type { IParagraph } from '@univerjs/core';
import type { IPastePlugin } from './type';
import { getParagraphStyle } from '../utils';
-const univerPastePlugin: IPastePlugin = {
+const UniverPastePlugin: IPastePlugin = {
name: 'univer-doc-paste-plugin-univer',
checkPasteType(html: string) {
return /UniverNormal/i.test(html);
@@ -54,4 +54,4 @@ const univerPastePlugin: IPastePlugin = {
],
};
-export default univerPastePlugin;
+export default UniverPastePlugin;
diff --git a/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-word.ts b/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-word.ts
index 42679e2fe2..d763d84216 100644
--- a/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-word.ts
+++ b/packages/docs-ui/src/services/clipboard/html-to-udm/paste-plugins/plugin-word.ts
@@ -20,7 +20,7 @@ import { BooleanNumber } from '@univerjs/core';
import { extractNodeStyle as getInlineStyle } from '../parse-node-style';
import { getParagraphStyle } from '../utils';
-const wordPastePlugin: IPastePlugin = {
+const WordPastePlugin: IPastePlugin = {
name: 'univer-doc-paste-plugin-word',
checkPasteType(html: string) {
return /word|mso/i.test(html);
@@ -65,4 +65,4 @@ const wordPastePlugin: IPastePlugin = {
],
};
-export default wordPastePlugin;
+export default WordPastePlugin;
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.ts
index 08f034d7bb..43dadc3603 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
@@ -31,7 +31,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { SheetSkeletonManagerService } from '../../sheet-skeleton-manager.service';
import { ISheetClipboardService } from '../clipboard.service';
import { clipboardTestBed } from './clipboard-test-bed';
-import { excelSample, excelSample2, excelSample3, excelSample4, excelSample5, excelSample6 } from './constant';
+import { excelSample, excelSample2, excelSample3, excelSample4, excelSample5, excelSample6, excelSample7 } from './constant';
describe('Test clipboard', () => {
let univer: Univer;
@@ -452,5 +452,40 @@ describe('Test clipboard', () => {
expect(cellValue?.v).toMatch(/[ \u00A0]{2}1[ \u00A0]{3}3[ \u00A0]{3}/);
expect(cellValue?.t).toBe(CellValueType.STRING);
});
+
+ it('copy value is (30), (30,303,003,030), 3.14, 2015/2/2, 3000%, ¥ 1,234.57', async () => {
+ const worksheet = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1');
+ if (!worksheet) return false;
+
+ // set selection to K1:L1
+ const selectionManager = get(SheetsSelectionsService);
+ selectionManager.addSelections([
+ {
+ range: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 0,
+ endColumn: 0,
+ rangeType: RANGE_TYPE.NORMAL,
+ },
+ primary: null,
+ style: null,
+ },
+ ]);
+
+ // paste data, excelSample2 value is 000123456
+ const res = await sheetClipboardService.legacyPaste(excelSample7);
+ expect(res).toBeTruthy();
+
+ // check the values
+ const cellValues = getValues(0, 0, 5, 0);
+
+ expect(cellValues?.[0]?.[0]?.v).toBe(-30);
+ expect(cellValues?.[1]?.[0]?.v).toBe(-30303003030);
+ expect(cellValues?.[2]?.[0]?.v).toBe(3.14);
+ expect(cellValues?.[3]?.[0]?.v).toBe(42037);
+ expect(cellValues?.[4]?.[0]?.v).toBe(30);
+ expect(cellValues?.[5]?.[0]?.v).toBe(1234.57);
+ });
});
});
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/constant.ts b/packages/sheets-ui/src/services/clipboard/__tests__/constant.ts
index c68f2cfbbf..75e6aace6c 100644
--- a/packages/sheets-ui/src/services/clipboard/__tests__/constant.ts
+++ b/packages/sheets-ui/src/services/clipboard/__tests__/constant.ts
@@ -31,6 +31,17 @@ export const excelSample5 = '<html xmlns:v="urn:schemas-microsoft-com:vml"\r\nxm
// copy value is ' 1 3 ', no specific format
export const excelSample6 = '<html xmlns:v="urn:schemas-microsoft-com:vml"\r\nxmlns:o="urn:schemas-microsoft-com:office:office"\r\nxmlns:x="urn:schemas-microsoft-com:office:excel"\r\nxmlns="http://www.w3.org/TR/REC-html40">\r\n\r\n<head>\r\n<meta http-equiv=Content-Type content="text/html; charset=utf-8">\r\n<meta name=ProgId content=Excel.Sheet>\r\n<meta name=Generator content="Microsoft Excel 15">\r\n<link id=Main-File rel=Main-File\r\nhref="file:///C:/Users/26775/AppData/Local/Temp/msohtmlclip1/01/clip.htm">\r\n<link rel=File-List\r\nhref="file:///C:/Users/26775/AppData/Local/Temp/msohtmlclip1/01/clip_filelist.xml">\r\n<style>\r\n\x3C!--table\r\n\t{mso-displayed-decimal-separator:"\\.";\r\n\tmso-displayed-thousand-separator:"\\,";}\r\n@page\r\n\t{margin:.75in .7in .75in .7in;\r\n\tmso-header-margin:.3in;\r\n\tmso-footer-margin:.3in;}\r\n.font5\r\n\t{color:windowtext;\r\n\tfont-size:9.0pt;\r\n\tfont-weight:400;\r\n\tfont-style:normal;\r\n\ttext-decoration:none;\r\n\tfont-family:等线;\r\n\tmso-generic-font-family:auto;\r\n\tmso-font-charset:134;}\r\ntr\r\n\t{mso-height-source:auto;\r\n\tmso-ruby-visibility:none;}\r\ncol\r\n\t{mso-width-source:auto;\r\n\tmso-ruby-visibility:none;}\r\nbr\r\n\t{mso-data-placement:same-cell;}\r\ntd\r\n\t{padding-top:1px;\r\n\tpadding-right:1px;\r\n\tpadding-left:1px;\r\n\tmso-ignore:padding;\r\n\tcolor:black;\r\n\tfont-size:11.0pt;\r\n\tfont-weight:400;\r\n\tfont-style:normal;\r\n\ttext-decoration:none;\r\n\tfont-family:等线;\r\n\tmso-generic-font-family:auto;\r\n\tmso-font-charset:134;\r\n\tmso-number-format:General;\r\n\ttext-align:general;\r\n\tvertical-align:bottom;\r\n\tborder:none;\r\n\tmso-background-source:auto;\r\n\tmso-pattern:auto;\r\n\tmso-protection:locked visible;\r\n\twhite-space:nowrap;\r\n\tmso-rotate:0;}\r\nruby\r\n\t{ruby-align:left;}\r\nrt\r\n\t{color:windowtext;\r\n\tfont-size:9.0pt;\r\n\tfont-weight:400;\r\n\tfont-style:normal;\r\n\ttext-decoration:none;\r\n\tfont-family:等线;\r\n\tmso-generic-font-family:auto;\r\n\tmso-font-charset:134;\r\n\tmso-char-type:none;\r\n\tdisplay:none;}\r\n-->\r\n</style>\r\n</head>\r\n\r\n<body link="#467886" vlink="#96607D">\r\n\r\n<table border=0 cellpadding=0 cellspacing=0 width=69 style=\'border-collapse:\r\n collapse;width:52pt\'>\r\n <col width=69 style=\'width:52pt\'>\r\n <tr height=19 style=\'height:14.0pt\'>\r\n\x3C!--StartFragment-->\r\n <td height=19 width=69 style=\'height:14.0pt;width:52pt\'><span\r\n style=\'mso-spacerun:yes\'>  </span>1<span style=\'mso-spacerun:yes\'>  \r\n </span>3<span style=\'mso-spacerun:yes\'>   </span></td>\r\n\x3C!--EndFragment-->\r\n </tr>\r\n</table>\r\n\r\n</body>\r\n\r\n</html>\r\n';
+/**
+ * Copy value is:
+ * (30)
+ * (30,303,003,030)
+ * 3.14
+ * 2015/2/2
+ * 3000%
+ * ¥ 1,234.57
+ */
+export const excelSample7 = '<html xmlns:v="urn:schemas-microsoft-com:vml"\r\nxmlns:o="urn:schemas-microsoft-com:office:office"\r\nxmlns:x="urn:schemas-microsoft-com:office:excel"\r\nxmlns="http://www.w3.org/TR/REC-html40">\r\n\r\n<head>\r\n<meta http-equiv=Content-Type content="text/html; charset=utf-8">\r\n<meta name=ProgId content=Excel.Sheet>\r\n<meta name=Generator content="Microsoft Excel 15">\r\n<link id=Main-File rel=Main-File\r\nhref="file:///C:/Users/26775/AppData/Local/Temp/msohtmlclip1/01/clip.htm">\r\n<link rel=File-List\r\nhref="file:///C:/Users/26775/AppData/Local/Temp/msohtmlclip1/01/clip_filelist.xml">\r\n<style>\r\n\x3C!--table\r\n\t{mso-displayed-decimal-separator:"\\.";\r\n\tmso-displayed-thousand-separator:"\\,";}\r\n@page\r\n\t{margin:.75in .7in .75in .7in;\r\n\tmso-header-margin:.3in;\r\n\tmso-footer-margin:.3in;}\r\ntr\r\n\t{mso-height-source:auto;\r\n\tmso-ruby-visibility:none;}\r\ncol\r\n\t{mso-width-source:auto;\r\n\tmso-ruby-visibility:none;}\r\nbr\r\n\t{mso-data-placement:same-cell;}\r\ntd\r\n\t{padding-top:1px;\r\n\tpadding-right:1px;\r\n\tpadding-left:1px;\r\n\tmso-ignore:padding;\r\n\tcolor:black;\r\n\tfont-size:11.0pt;\r\n\tfont-weight:400;\r\n\tfont-style:normal;\r\n\ttext-decoration:none;\r\n\tfont-family:等线;\r\n\tmso-generic-font-family:auto;\r\n\tmso-font-charset:134;\r\n\tmso-number-format:General;\r\n\ttext-align:general;\r\n\tvertical-align:bottom;\r\n\tborder:none;\r\n\tmso-background-source:auto;\r\n\tmso-pattern:auto;\r\n\tmso-protection:locked visible;\r\n\twhite-space:nowrap;\r\n\tmso-rotate:0;}\r\n.xl65\r\n\t{mso-number-format:"_\\(* \\#\\,\\#\\#0_\\)\\;_\\(* \\\\\\(\\#\\,\\#\\#0\\\\\\)\\;_\\(* \\0022-\\0022??_\\)\\;_\\(\\@_\\)";}\r\n.xl66\r\n\t{mso-number-format:0%;}\r\n.xl67\r\n\t{mso-number-format:"Short Date";}\r\n.xl68\r\n\t{mso-number-format:"0\\.00_\\)\\;\\[Red\\]\\\\\\(0\\.00\\\\\\)";}\r\n.xl69\r\n\t{mso-number-format:"_ \\0022¥\\0022* \\#\\,\\#\\#0\\.00_ \\;_ \\0022¥\\0022* \\\\-\\#\\,\\#\\#0\\.00_ \\;_ \\0022¥\\0022* \\0022-\\0022??_ \\;_ \\@_ ";}\r\nruby\r\n\t{ruby-align:left;}\r\nrt\r\n\t{color:windowtext;\r\n\tfont-size:9.0pt;\r\n\tfont-weight:400;\r\n\tfont-style:normal;\r\n\ttext-decoration:none;\r\n\tfont-family:等线;\r\n\tmso-generic-font-family:auto;\r\n\tmso-font-charset:134;\r\n\tmso-char-type:none;\r\n\tdisplay:none;}\r\n-->\r\n</style>\r\n</head>\r\n\r\n<body link="#0563C1" vlink="#954F72">\r\n\r\n<table border=0 cellpadding=0 cellspacing=0 width=123 style=\'border-collapse:\r\n collapse;width:92pt\'>\r\n\x3C!--StartFragment-->\r\n <col width=123 style=\'mso-width-source:userset;mso-width-alt:3925;width:92pt\'>\r\n <tr height=19 style=\'height:14.0pt\'>\r\n <td height=19 class=xl65 width=123 style=\'height:14.0pt;width:92pt\'><span\r\n style=\'mso-spacerun:yes\'>                       </span>(30)</td>\r\n </tr>\r\n <tr height=19 style=\'height:14.0pt\'>\r\n <td height=19 class=xl65 style=\'height:14.0pt\'><span\r\n style=\'mso-spacerun:yes\'>   </span>(30,303,003,030)</td>\r\n </tr>\r\n <tr height=19 style=\'height:14.0pt\'>\r\n <td height=19 class=xl68 align=right style=\'height:14.0pt\'>3.14 </td>\r\n </tr>\r\n <tr height=19 style=\'height:14.0pt\'>\r\n <td height=19 class=xl67 align=right style=\'height:14.0pt\'>2015/2/2</td>\r\n </tr>\r\n <tr height=19 style=\'height:14.0pt\'>\r\n <td height=19 class=xl66 align=right style=\'height:14.0pt\'>3000%</td>\r\n </tr>\r\n <tr height=19 style=\'height:14.0pt\'>\r\n <td height=19 class=xl69 style=\'height:14.0pt\'><span\r\n style=\'mso-spacerun:yes\'> </span>¥<span style=\'mso-spacerun:yes\'>          \r\n </span>1,234.57 </td>\r\n </tr>\r\n\x3C!--EndFragment-->\r\n</table>\r\n\r\n</body>\r\n\r\n</html>\r\n';
+
export const googleSample = '<google-sheets-html-origin><style type="text/css">\x3C!--td {border: 1px solid #cccccc;}br {mso-data-placement:same-cell;}--></style><table xmlns="http://www.w3.org/1999/xhtml" cellspacing="0" cellpadding="0" dir="ltr" border="1" style="table-layout:fixed;font-size:10pt;font-family:Arial;width:0px;border-collapse:collapse;border:none" data-sheets-root="1"><colgroup><col width="100"/><col width="100"/><col width="100"/><col width="100"/></colgroup><tbody><tr style="height:21px;"><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;font-weight:bold;font-style:italic;" data-sheets-value="{&quot;1&quot;:2,&quot;2&quot;:&quot;Univer&quot;}">Univer</td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;background-color:#ff0000;" data-sheets-value="{&quot;1&quot;:2,&quot;2&quot;:&quot;univer&quot;}" data-sheets-textstyleruns="{&quot;1&quot;:0}{&quot;1&quot;:1,&quot;2&quot;:{&quot;2&quot;:{&quot;1&quot;:2,&quot;2&quot;:14275305},&quot;4&quot;:18,&quot;5&quot;:1,&quot;6&quot;:1}}{&quot;1&quot;:4}"><span style="font-size:10pt;font-family:Arial;font-style:normal;">u</span><span style="font-size:18pt;font-family:Arial;font-weight:bold;font-style:italic;color:#d9d2e9;">niv</span><span style="font-size:10pt;font-family:Arial;font-style:normal;">er</span></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="border-bottom:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="border-bottom:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="border-right:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="border-right:1px solid #000000;border-bottom:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;text-align:center;" rowspan="1" colspan="2" data-sheets-value="{&quot;1&quot;:2,&quot;2&quot;:&quot;univer&quot;}">univer</td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="border-bottom:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="border-bottom:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="border-right:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="border-right:1px solid #000000;border-bottom:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;text-align:center;" rowspan="2" colspan="2" data-sheets-value="{&quot;1&quot;:2,&quot;2&quot;:&quot;univer&quot;}"><span><div style="max-height:42px">univer</div></span></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="border-right:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="border-bottom:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="border-right:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="border-right:1px solid #000000;border-bottom:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:middle;background-color:#ff0000;text-align:center;" rowspan="4" colspan="1" data-sheets-value="{&quot;1&quot;:2,&quot;2&quot;:&quot;univer&quot;}"><span><div style="max-height:84px">univer</div></span></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="border-right:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="border-right:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="border-right:1px solid #000000;overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;text-align:right;" data-sheets-value="{&quot;1&quot;:3,&quot;3&quot;:45607}" data-sheets-numberformat="{&quot;1&quot;:5,&quot;2&quot;:&quot;yyyy/m/d&quot;,&quot;3&quot;:1}">2024/11/11</td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr><tr style="height:21px;"><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td><td style="overflow:hidden;padding:2px 3px 2px 3px;vertical-align:bottom;"></td></tr></tbody></table>';
// font
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 d14dce40be..5683f8ffc9 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
@@ -16,7 +16,7 @@
/* eslint-disable complexity */
-import type { ICustomRange, IDocumentBody, IDocumentData, ITextRun, ITextStyle, Nullable } from '@univerjs/core';
+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 {
@@ -26,7 +26,7 @@ import type {
IUniverSheetCopyDataModel,
} from '../type';
import type { IAfterProcessRule, IPastePlugin } from './paste-plugins/type';
-import { CustomRangeType, DEFAULT_WORKSHEET_ROW_HEIGHT, generateRandomId, getNumfmtParseValueFilter, isRealNum, ObjectMatrix, skipParseTagNames } from '@univerjs/core';
+import { CustomRangeType, DEFAULT_WORKSHEET_ROW_HEIGHT, generateRandomId, getNumfmtParseValueFilter, numfmt, ObjectMatrix, skipParseTagNames } from '@univerjs/core';
import { handleStringToStyle, textTrim } from '@univerjs/ui';
import { extractNodeStyle } from './parse-node-style';
import parseToDom, { convertToCellStyle, generateParagraphs } from './utils';
@@ -102,6 +102,9 @@ export class HtmlToUSMService {
private _dom: HTMLElement | null = null;
+ // mso-number-format is a css property used in html copied from excel to indicate the cell format, we need to parse it and apply the corresponding format in univer sheet.
+ private _msoNumfmtMap = new Map<string, string>();
+
private _getCurrentSkeleton: () => Nullable<ISheetSkeletonManagerParam>;
constructor(props: IHtmlToUSMServiceProps) {
@@ -120,6 +123,9 @@ export class HtmlToUSMService {
// Convert stylesheets to map
const style = this._dom.querySelector('style');
if (style) {
+ // Must read textContent BEFORE shadow DOM moves the element, because browsers discard mso-* properties during CSS parsing.
+ this._parseMsoNumfmtFromCssText(style.textContent ?? '');
+
const shadowHost = document.createElement('div');
const shadowRoot = shadowHost.attachShadow({ mode: 'open' });
document.body.appendChild(shadowHost);
@@ -241,6 +247,49 @@ export class HtmlToUSMService {
};
}
+ /**
+ * Parse mso-number-format from raw CSS text before the browser drops proprietary properties.
+ * Must be called with style.textContent BEFORE the style element is moved into a shadow DOM.
+ */
+ private _parseMsoNumfmtFromCssText(rawCssText: string): void {
+ // Match CSS rule blocks: selector(s) { declarations }
+ const ruleRegex = /([^{]+)\{([^}]*)\}/g;
+ let match;
+ while ((match = ruleRegex.exec(rawCssText)) !== null) {
+ const selectors = match[1].trim();
+ const declarations = match[2];
+ const numfmtMatch = declarations.match(/mso-number-format\s*:\s*("(?:[^"\\]|\\.)*"|[^;]+)/i);
+ if (!numfmtMatch) continue;
+
+ let value = numfmtMatch[1].trim();
+ // Remove surrounding CSS quotes if present
+ if (value.startsWith('"') && value.endsWith('"')) {
+ value = value.slice(1, -1);
+ }
+ const decoded = decodeMsoNumberFormat(value);
+ // A rule can have multiple comma-separated selectors
+ selectors.split(',').forEach((sel) => {
+ this._msoNumfmtMap.set(sel.trim(), decoded);
+ });
+ }
+ }
+
+ /**
+ * Get the mso-number-format value for a given DOM node,
+ * following the same priority as _getStyle: class > id > tag.
+ */
+ private _getMsoNumfmtForNode(node: HTMLElement): string | undefined {
+ for (const className of node.classList) {
+ const fmt = this._msoNumfmtMap.get(`.${className}`);
+ if (fmt) return fmt;
+ }
+ if (node.id) {
+ const fmt = this._msoNumfmtMap.get(`#${node.id}`);
+ if (fmt) return fmt;
+ }
+ return this._msoNumfmtMap.get(node.nodeName.toLowerCase());
+ }
+
private _getStyleBySelectorText(selectorText: string, cssText: string) {
const css = this._styleMap.get(selectorText)?.getPropertyValue(cssText);
if (!css) {
@@ -322,7 +371,7 @@ export class HtmlToUSMService {
}
}
- const cellValue = value?.richTextParma?.p?.body?.textRuns
+ const cellValue: ICellDataWithSpanInfo = value?.richTextParma?.p?.body?.textRuns
? {
v: value.richTextParma.v,
p: value.richTextParma.p,
@@ -336,6 +385,17 @@ export class HtmlToUSMService {
rowSpan: value.rowSpan,
colSpan: value.colSpan,
};
+
+ if (value.numfmtPattern) {
+ cellValue.s = {
+ ...(cellValue.s as IStyleData),
+ n: {
+ pattern: value.numfmtPattern,
+ },
+ };
+ cellValue.v = Number(cellValue.v);
+ }
+
valueMatrix.setValue(row, col, cellValue);
});
return {
@@ -345,6 +405,7 @@ export class HtmlToUSMService {
};
}
+ // eslint-disable-next-line max-lines-per-function
private _parseTableByHtml(htmlElement: HTMLElement, tableElIndex: number, skeleton?: SpreadsheetSkeleton) {
const cellMatrix = new ObjectMatrix<IParsedCellValueByClipboard>();
const tableEle = htmlElement.querySelectorAll('table')[tableElIndex];
@@ -393,8 +454,47 @@ export class HtmlToUSMService {
}
}
- // Determine whether it is rich text based on whether there are html tags
- const { cellText, cellRichStyle } = this._getCellTextAndRichText(cell, cellStyle, skeleton);
+ let cellText = '';
+ let cellRichStyle;
+ let numfmtPattern: string | undefined;
+
+ const pattern = this._getMsoNumfmtForNode(cell as HTMLElement);
+ if (pattern) {
+ cellText = cell.innerHTML;
+ if (cellText.includes('mso-spacerun:yes')) {
+ cellText = cleanMsoSpaceRun(cellText);
+ }
+
+ const parseData = getNumfmtParseValueFilter(cellText);
+
+ if (
+ typeof parseData?.v === 'number' &&
+ (
+ parseData.z === pattern ||
+ numfmt.format(pattern, parseData.v) === cellText
+ )
+ ) {
+ cellText = parseData.v.toString();
+ numfmtPattern = pattern;
+ } else if (parseData && parseData.z) {
+ cellText = parseData.v.toString();
+ numfmtPattern = parseData.z;
+ } else if (!parseData) {
+ const extractedNumber = extractNumber(cellText);
+
+ if (extractedNumber !== null && !Number.isNaN(extractedNumber) && numfmt.format(pattern, extractedNumber) === cellText) {
+ cellText = extractedNumber.toString();
+ numfmtPattern = pattern;
+ }
+ }
+ }
+
+ if (!numfmtPattern) {
+ // Determine whether it is rich text based on whether there are html tags
+ const cellTextAndRichText = this._getCellTextAndRichText(cell, cellStyle, skeleton);
+ cellText = cellTextAndRichText.cellText;
+ cellRichStyle = cellTextAndRichText.cellRichStyle;
+ }
const cellValue = {
rowSpan,
@@ -405,6 +505,7 @@ export class HtmlToUSMService {
p: cellRichStyle,
v: cellText,
},
+ numfmtPattern,
};
if (cellMatrix.getValue(rowIndex, colSetValueIndex)) {
@@ -473,10 +574,10 @@ export class HtmlToUSMService {
* "<span style="mso-spacerun:yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>1,234.57 "
*/
if (cell.innerHTML.includes('mso-spacerun:yes')) {
- const cellText = cell.innerHTML.replace(/<span[^>]*mso-spacerun:yes[^>]*>[\s\S]*?<\/span>/gi, '').replace(/\s/g, '');
+ const cellText = cleanMsoSpaceRun(cell.innerHTML);
const parseInfo = getNumfmtParseValueFilter(cellText);
- if (parseInfo && parseInfo.z && isRealNum(parseInfo.v)) {
+ if (parseInfo && parseInfo.z && typeof parseInfo.v === 'number') {
return {
cellText,
cellRichStyle: undefined,
@@ -637,6 +738,7 @@ export class HtmlToUSMService {
this._dom = null;
this._styleCache.clear();
this._styleMap.clear();
+ this._msoNumfmtMap.clear();
}
}
@@ -808,3 +910,54 @@ function setMergedCellStyle(
}
}
}
+
+/**
+ * Decode mso-number-format value from Excel HTML CSS.
+ * Excel escapes certain characters in CSS with backslash, and uses \0022 for double-quote.
+ * e.g. "_\(* \#\,\#\#0_\)\;_\(* \\\(\#\,\#\#0\\\)\;_\(* \0022-\0022??_\)\;_\(\@_\)"
+ * → "_(* #,##0_);_(* \(#,##0\);_(* \"-\"??_);_(@_)"
+ */
+function decodeMsoNumberFormat(value: string): string {
+ // 1. Replace Unicode escapes: \0022 → "
+ let result = value.replace(/\\([0-9a-fA-F]{4})/g, (_, hex) =>
+ String.fromCharCode(Number.parseInt(hex, 16)));
+ // 2. Replace \\ → \ (must come before single-char escapes)
+ result = result.replace(/\\\\/g, '\x00BACKSLASH\x00');
+ // 3. Unescape other backslash-escaped chars: \# \, \( \) \; \@ \. \- etc.
+ result = result.replace(/\\([^\\])/g, '$1');
+ // 4. Restore literal backslashes
+ result = result.replace(/\x00BACKSLASH\x00/g, '\\');
+ return result;
+}
+
+function cleanMsoSpaceRun(value: string): string {
+ return value
+ // remove mso-spacerun spans but keep their content
+ .replace(/<span[^>]*mso-spacerun:yes[^>]*>/gi, '')
+ .replace(/<\/span>/gi, '')
+ // remove any remaining non-breaking spaces
+ .replace(/\u00A0|&nbsp;/gi, '');
+}
+
+function extractNumber(value: string): number | null {
+ const match = value.match(/\(?-?\d[\d,]*(\.\d+)?%?\)?/);
+ if (!match) return null;
+
+ let numStr = match[0];
+
+ const isPercent = numStr.includes('%');
+
+ numStr = numStr.replace(/[,%]/g, '');
+
+ if (/^\(.*\)$/.test(numStr)) {
+ numStr = `-${numStr.slice(1, -1)}`;
+ }
+
+ let num = Number(numStr);
+
+ if (isPercent) {
+ num = num / 100;
+ }
+
+ return num;
+}
diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-lark.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-lark.ts
index 34a8ef450b..26089d0d4f 100644
--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-lark.ts
+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-lark.ts
@@ -20,7 +20,7 @@ import { BooleanNumber } from '@univerjs/core';
import { extractNodeStyle as getInlineStyle } from '../parse-node-style';
export const LarkPastePlugin: IPastePlugin = {
- name: 'univer-doc-paste-plugin-lark',
+ name: 'univer-sheet-paste-plugin-lark',
checkPasteType(html: string) {
return /lark-record-clipboard/i.test(html);
},
diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-univer.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-univer.ts
index 8b69d7616c..84ccb2b06a 100644
--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-univer.ts
+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-univer.ts
@@ -19,7 +19,7 @@ import type { IPastePlugin } from './type';
import { getParagraphStyle } from '../utils';
export const UniverPastePlugin: IPastePlugin = {
- name: 'univer-doc-paste-plugin-univer',
+ name: 'univer-sheet-paste-plugin-univer',
checkPasteType(html: string) {
return /UniverNormal/i.test(html);
},
diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-word.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-word.ts
index 07240e4989..dc7d40d940 100644
--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-word.ts
+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/paste-plugins/plugin-word.ts
@@ -21,7 +21,7 @@ import { extractNodeStyle as getInlineStyle } from '../parse-node-style';
import { getParagraphStyle } from '../utils';
export const WordPastePlugin: IPastePlugin = {
- name: 'univer-doc-paste-plugin-word',
+ name: 'univer-sheet-paste-plugin-word',
checkPasteType(html: string) {
return /word|mso/i.test(html);
},
diff --git a/packages/sheets-ui/src/services/clipboard/type.ts b/packages/sheets-ui/src/services/clipboard/type.ts
index eb61bdaa04..75f6862e3d 100644
--- a/packages/sheets-ui/src/services/clipboard/type.ts
+++ b/packages/sheets-ui/src/services/clipboard/type.ts
@@ -45,7 +45,7 @@ export interface IParsedCellValueByClipboard {
p?: IDocumentData;
v?: string;
};
-
+ numfmtPattern?: string;
}
export interface IUniverSheetCopyDataModel {