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 value from excel incorrect (#4659)
siamd2323e40c032
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.tsindex d4966ff07a..02ee9584fd 100644--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts@@ -76,7 +76,7 @@ import { UniverPastePlugin } from './html-to-usm/paste-plugins/plugin-univer';import { WordPastePlugin } from './html-to-usm/paste-plugins/plugin-word';import { COPY_TYPE } from './type';import { USMToHtmlService } from './usm-to-html/convertor';-import { convertTextToTable, discreteRangeContainsRange, htmlIsFromExcel, mergeSetRangeValues, rangeIntersectWithDiscreteRange } from './utils';+import { convertTextToTable, discreteRangeContainsRange, htmlContainsImage, htmlIsFromExcel, mergeSetRangeValues, rangeIntersectWithDiscreteRange } from './utils';export const PREDEFINED_HOOK_NAME = {DEFAULT_COPY: 'default-copy',@@ -270,27 +270,12 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard? await item.getType(HTML_CLIPBOARD_MIME_TYPE).then((blob) => blob && blob.text()): '';- const isFromExcel = htmlIsFromExcel(html);-- // clipboard item from excel may contain image, so we need to check if the clipboard item is from excelconst imageIndex = types.findIndex((type) => imageMimeTypeSet.has(type));- if (imageIndex !== -1 && !isFromExcel) {- const imageMimeType = types[imageIndex]!;- const imageBlob = await item.getType(imageMimeType);-- if (imageBlob) {- const file = new File(- [imageBlob],- `clipboard-image.${IMAGE_MIME_TO_EXTENSION[imageMimeType as keyof typeof IMAGE_MIME_TO_EXTENSION]}`,- { type: imageMimeType });-- return this._pasteFiles([file], pasteType);- }- }- if (html) {+ const shouldUseHTMLPaste = imageIndex === -1 || !htmlContainsImage(html);+ if (html && shouldUseHTMLPaste) {// Firstly see if the html content is from Excel- if (this._platformService.isWindows && isFromExcel) {+ if (this._platformService.isWindows && htmlIsFromExcel(html)) {this._notificationService.show({type: 'warning',title: this._localeService.t('clipboard.shortCutNotify.title'),@@ -303,6 +288,22 @@ export class SheetClipboardService extends Disposable implements ISheetClipboardreturn this._pasteHTML(html, pasteType);}+ // clipboard item from excel may contain image, so we need to check if the clipboard item is from excel+ if (imageIndex !== -1) {+ const imageMimeType = types[imageIndex]!;+ const imageBlob = await item.getType(imageMimeType);++ if (imageBlob) {+ const imageExtension = IMAGE_MIME_TO_EXTENSION[imageMimeType as keyof typeof IMAGE_MIME_TO_EXTENSION];+ const file = new File(+ [imageBlob],+ `clipboard-image.${imageExtension}`,+ { type: imageMimeType });++ return this._pasteFiles([file], pasteType);+ }+ }+if (text) {return this._pastePlainText(text, pasteType);}diff --git a/packages/sheets-ui/src/services/clipboard/utils.ts b/packages/sheets-ui/src/services/clipboard/utils.tsindex 6b48e93803..d7f2816d08 100644--- a/packages/sheets-ui/src/services/clipboard/utils.ts+++ b/packages/sheets-ui/src/services/clipboard/utils.ts@@ -118,6 +118,19 @@ export function htmlIsFromExcel(html: string): boolean {return excelMarkers.some((marker) => marker.test(html));}+export function htmlContainsImage(html: string): boolean {+ if (!html) {+ return false;+ }++ // test the image tag is base64 image+ const base64ImageRegex = /<img[^>]*src\s*=\s*["']data:image\/[^;]+;base64,[^"']*["'][^>]*>/i; ;++ const images = (html.match(base64ImageRegex) || []);++ return images.length > 0;+}+export function mergeCellValues(...cellValues: IObjectMatrixPrimitiveType<Nullable<ICellData>>[]) {if (cellValues.length === 1) {return cellValues[0];