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 pasted data with number format error case (#6257)

wpxp123456300e60ba7ef5
diff --git a/packages/engine-formula/src/basics/date.ts b/packages/engine-formula/src/basics/date.ts
index be3f026a4a..7624e89b41 100644
--- a/packages/engine-formula/src/basics/date.ts
+++ b/packages/engine-formula/src/basics/date.ts
@@ -160,10 +160,6 @@ export function parseFormattedDate(value: string) {
return numfmt.parseDate(value);
}
-export function parseFormattedValue(value: string) {
- return numfmt.parseValue(value);
-}
-
export function parseFormattedTime(value: string) {
return numfmt.parseTime(value);
}
diff --git a/packages/engine-formula/src/functions/date/timevalue/index.ts b/packages/engine-formula/src/functions/date/timevalue/index.ts
index e6587af834..5adac050e8 100644
--- a/packages/engine-formula/src/functions/date/timevalue/index.ts
+++ b/packages/engine-formula/src/functions/date/timevalue/index.ts
@@ -15,7 +15,8 @@
*/
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { excelDateTimeSerial, isDate, parseFormattedValue } from '../../../basics/date';
+import { getNumfmtParseValueFilter } from '@univerjs/core';
+import { excelDateTimeSerial, isDate } from '../../../basics/date';
import { ErrorType } from '../../../basics/error-type';
import { getFractionalPart } from '../../../engine/utils/math-kit';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
@@ -46,7 +47,7 @@ export class Timevalue extends BaseFunction {
if (timeTextObject.isString()) {
const value = `${timeTextObject.getValue()}`;
- const parsedTime = parseFormattedValue(value);
+ const parsedTime = getNumfmtParseValueFilter(value);
if (parsedTime) {
let { v, z } = parsedTime;
diff --git a/packages/sheets-table/src/model/filter-util/condition.ts b/packages/sheets-table/src/model/filter-util/condition.ts
index bf832382bf..f309e70d98 100644
--- a/packages/sheets-table/src/model/filter-util/condition.ts
+++ b/packages/sheets-table/src/model/filter-util/condition.ts
@@ -130,7 +130,6 @@ function getNumberCellValue(sheet: Worksheet, row: number, col: number) {
return null;
}
if (typeof v === 'string' && t === CellValueType.NUMBER) {
- // use this way to instead of numfmt.parseNumber(v as string).v as number;
return Number(sheet.getCellRaw(row, col)!.v);
}
return Number(v);
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index 42ca6fc940..2939554978 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -42,9 +42,7 @@ import type {
ISheetDiscreteRangeLocation,
} from '../../services/clipboard/type';
import type { IScrollStateWithSearchParam } from '../../services/scroll-manager.service';
-
import type { IUniverSheetsUIConfig } from '../config.schema';
-
import {
BooleanNumber,
DEFAULT_WORKSHEET_COLUMN_WIDTH,
@@ -52,6 +50,7 @@ import {
DEFAULT_WORKSHEET_ROW_HEIGHT,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
extractPureTextFromCell,
+ getNumfmtParseValueFilter,
handleStyleToString,
ICommandService,
IConfigService,
@@ -61,16 +60,13 @@ import {
isFormulaString,
IUniverInstanceService,
LocaleService,
- numfmt,
ObjectMatrix,
RxDisposable,
Tools,
UniverInstanceType,
} from '@univerjs/core';
-
import { MessageType } from '@univerjs/design';
import { convertBodyToHtml, DocSelectionRenderService } from '@univerjs/docs-ui';
-
import { IRenderManagerService, withCurrentTypeOfRenderer } from '@univerjs/engine-render';
import {
InsertColMutation,
@@ -635,7 +631,7 @@ export class SheetClipboardController extends RxDisposable {
},
};
} else {
- const pattern = numfmt.parseNumber(text);
+ const pattern = getNumfmtParseValueFilter(text);
if (pattern?.z) {
cellValue = {
[range.rows[0]]: {
diff --git a/packages/sheets-ui/src/controllers/clipboard/utils.ts b/packages/sheets-ui/src/controllers/clipboard/utils.ts
index 7acd71d4fe..94f9e3242f 100644
--- a/packages/sheets-ui/src/controllers/clipboard/utils.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/utils.ts
@@ -35,9 +35,9 @@ import {
CustomRangeType,
DEFAULT_STYLES,
generateRandomId,
+ getNumfmtParseValueFilter,
isTextFormat,
IUniverInstanceService,
- numfmt,
ObjectMatrix,
Range,
Rectangle,
@@ -364,7 +364,7 @@ export function getSetCellValueMutations(
cellValue.t = CellValueType.STRING;
} else {
const content = String(value.v);
- const numfmtValue = numfmt.parseValue(content);
+ const numfmtValue = getNumfmtParseValueFilter(content);
if (numfmtValue?.v !== undefined && typeof numfmtValue.v === 'number') {
// If the numeric string will lose precision when converted to a number, set the cell type to force string
// e.g. 123456789123456789
@@ -474,7 +474,7 @@ export function getSetCellStyleMutations(
(newValue.s as IStyleData).n = style?.n;
} else {
const content = String(value.v);
- const numfmtValue = numfmt.parseValue(content);
+ const numfmtValue = getNumfmtParseValueFilter(content);
if (numfmtValue?.z) {
if (!newValue.s) {
newValue.s = {};
diff --git a/packages/sheets-ui/src/controllers/utils/char-tools.ts b/packages/sheets-ui/src/controllers/utils/char-tools.ts
index 2261693333..d1f2dd7149 100644
--- a/packages/sheets-ui/src/controllers/utils/char-tools.ts
+++ b/packages/sheets-ui/src/controllers/utils/char-tools.ts
@@ -16,7 +16,7 @@
/* eslint-disable antfu/consistent-list-newline */
import type { IFunctionService, ISequenceNode, LexerTreeBuilder } from '@univerjs/engine-formula';
-import { LocaleType, numfmt } from '@univerjs/core';
+import { getNumfmtParseValueFilter, LocaleType, numfmt } from '@univerjs/core';
import { matchToken, sequenceNodeType } from '@univerjs/engine-formula';
const fullWidthToHalfWidthMap: { [key: string]: string } = {
@@ -94,7 +94,7 @@ export function normalizeString(str: string, lexerTreeBuilder: LexerTreeBuilder,
}
// Formatting Numbers
- const parsedValue = numfmt.parseValue(normalStr);
+ const parsedValue = getNumfmtParseValueFilter(normalStr);
return parsedValue == null ? str : normalStr;
}
@@ -160,7 +160,7 @@ function normalizeFormulaString(str: string, normalStr: string, lexerTreeBuilder
const endIndex = node.endIndex + totalOffset + 1;
_normalStr = replaceString(str.slice(startIndex, endIndex), _normalStr, startIndex, endIndex);
} else if (node.nodeType !== sequenceNodeType.ARRAY) {
- const parsedValue = numfmt.parseValue(token);
+ const parsedValue = getNumfmtParseValueFilter(token);
if (parsedValue == null) {
const startIndex = node.startIndex + totalOffset + 1;
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 d7f8d1f25a..65a1cd09db 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,7 +19,6 @@
import type { ICustomRange, IDocumentBody, IDocumentData, ITextRun, ITextStyle, Nullable } from '@univerjs/core';
import type { SpreadsheetSkeleton } from '@univerjs/engine-render';
import type { ISheetSkeletonManagerParam } from '../../sheet-skeleton-manager.service';
-
import type {
ICellDataWithSpanInfo,
IClipboardPropertyItem,
@@ -27,7 +26,7 @@ import type {
IUniverSheetCopyDataModel,
} from '../type';
import type { IAfterProcessRule, IPastePlugin } from './paste-plugins/type';
-import { CustomRangeType, DEFAULT_WORKSHEET_ROW_HEIGHT, generateRandomId, numfmt, ObjectMatrix, skipParseTagNames } from '@univerjs/core';
+import { CustomRangeType, DEFAULT_WORKSHEET_ROW_HEIGHT, generateRandomId, getNumfmtParseValueFilter, ObjectMatrix, skipParseTagNames } from '@univerjs/core';
import { handleStringToStyle, textTrim } from '@univerjs/ui';
import { extractNodeStyle } from './parse-node-style';
import parseToDom, { convertToCellStyle, generateParagraphs } from './utils';
@@ -474,8 +473,8 @@ 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, '');
- const parseInfo = numfmt.parseNumber(cellText);
+ const cellText = cell.innerHTML.replace(/<span[^>]*mso-spacerun:yes[^>]*>[\s\S]*?<\/span>/gi, '').replace(/\s/g, '');
+ const parseInfo = getNumfmtParseValueFilter(cellText);
if (parseInfo && parseInfo.v !== undefined && parseInfo.v !== null) {
return {