fix(clipboard): fix pasted data with number format error case (#6257)

This commit is contained in:
wpxp123456
2025-12-06 20:40:51 +08:00
committed by GitHub
parent e941d9e5a0
commit 300e60ba7e
7 changed files with 14 additions and 23 deletions
@@ -160,10 +160,6 @@ export function parseFormattedDate(value: string) {
return numfmt.parseDate(value); return numfmt.parseDate(value);
} }
export function parseFormattedValue(value: string) {
return numfmt.parseValue(value);
}
export function parseFormattedTime(value: string) { export function parseFormattedTime(value: string) {
return numfmt.parseTime(value); return numfmt.parseTime(value);
} }
@@ -15,7 +15,8 @@
*/ */
import type { BaseValueObject } from '../../../engine/value-object/base-value-object'; 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 { ErrorType } from '../../../basics/error-type';
import { getFractionalPart } from '../../../engine/utils/math-kit'; import { getFractionalPart } from '../../../engine/utils/math-kit';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object'; import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
@@ -46,7 +47,7 @@ export class Timevalue extends BaseFunction {
if (timeTextObject.isString()) { if (timeTextObject.isString()) {
const value = `${timeTextObject.getValue()}`; const value = `${timeTextObject.getValue()}`;
const parsedTime = parseFormattedValue(value); const parsedTime = getNumfmtParseValueFilter(value);
if (parsedTime) { if (parsedTime) {
let { v, z } = parsedTime; let { v, z } = parsedTime;
@@ -130,7 +130,6 @@ function getNumberCellValue(sheet: Worksheet, row: number, col: number) {
return null; return null;
} }
if (typeof v === 'string' && t === CellValueType.NUMBER) { 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(sheet.getCellRaw(row, col)!.v);
} }
return Number(v); return Number(v);
@@ -42,9 +42,7 @@ import type {
ISheetDiscreteRangeLocation, ISheetDiscreteRangeLocation,
} from '../../services/clipboard/type'; } from '../../services/clipboard/type';
import type { IScrollStateWithSearchParam } from '../../services/scroll-manager.service'; import type { IScrollStateWithSearchParam } from '../../services/scroll-manager.service';
import type { IUniverSheetsUIConfig } from '../config.schema'; import type { IUniverSheetsUIConfig } from '../config.schema';
import { import {
BooleanNumber, BooleanNumber,
DEFAULT_WORKSHEET_COLUMN_WIDTH, DEFAULT_WORKSHEET_COLUMN_WIDTH,
@@ -52,6 +50,7 @@ import {
DEFAULT_WORKSHEET_ROW_HEIGHT, DEFAULT_WORKSHEET_ROW_HEIGHT,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
extractPureTextFromCell, extractPureTextFromCell,
getNumfmtParseValueFilter,
handleStyleToString, handleStyleToString,
ICommandService, ICommandService,
IConfigService, IConfigService,
@@ -61,16 +60,13 @@ import {
isFormulaString, isFormulaString,
IUniverInstanceService, IUniverInstanceService,
LocaleService, LocaleService,
numfmt,
ObjectMatrix, ObjectMatrix,
RxDisposable, RxDisposable,
Tools, Tools,
UniverInstanceType, UniverInstanceType,
} from '@univerjs/core'; } from '@univerjs/core';
import { MessageType } from '@univerjs/design'; import { MessageType } from '@univerjs/design';
import { convertBodyToHtml, DocSelectionRenderService } from '@univerjs/docs-ui'; import { convertBodyToHtml, DocSelectionRenderService } from '@univerjs/docs-ui';
import { IRenderManagerService, withCurrentTypeOfRenderer } from '@univerjs/engine-render'; import { IRenderManagerService, withCurrentTypeOfRenderer } from '@univerjs/engine-render';
import { import {
InsertColMutation, InsertColMutation,
@@ -635,7 +631,7 @@ export class SheetClipboardController extends RxDisposable {
}, },
}; };
} else { } else {
const pattern = numfmt.parseNumber(text); const pattern = getNumfmtParseValueFilter(text);
if (pattern?.z) { if (pattern?.z) {
cellValue = { cellValue = {
[range.rows[0]]: { [range.rows[0]]: {
@@ -35,9 +35,9 @@ import {
CustomRangeType, CustomRangeType,
DEFAULT_STYLES, DEFAULT_STYLES,
generateRandomId, generateRandomId,
getNumfmtParseValueFilter,
isTextFormat, isTextFormat,
IUniverInstanceService, IUniverInstanceService,
numfmt,
ObjectMatrix, ObjectMatrix,
Range, Range,
Rectangle, Rectangle,
@@ -364,7 +364,7 @@ export function getSetCellValueMutations(
cellValue.t = CellValueType.STRING; cellValue.t = CellValueType.STRING;
} else { } else {
const content = String(value.v); const content = String(value.v);
const numfmtValue = numfmt.parseValue(content); const numfmtValue = getNumfmtParseValueFilter(content);
if (numfmtValue?.v !== undefined && typeof numfmtValue.v === 'number') { 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 // If the numeric string will lose precision when converted to a number, set the cell type to force string
// e.g. 123456789123456789 // e.g. 123456789123456789
@@ -474,7 +474,7 @@ export function getSetCellStyleMutations(
(newValue.s as IStyleData).n = style?.n; (newValue.s as IStyleData).n = style?.n;
} else { } else {
const content = String(value.v); const content = String(value.v);
const numfmtValue = numfmt.parseValue(content); const numfmtValue = getNumfmtParseValueFilter(content);
if (numfmtValue?.z) { if (numfmtValue?.z) {
if (!newValue.s) { if (!newValue.s) {
newValue.s = {}; newValue.s = {};
@@ -16,7 +16,7 @@
/* eslint-disable antfu/consistent-list-newline */ /* eslint-disable antfu/consistent-list-newline */
import type { IFunctionService, ISequenceNode, LexerTreeBuilder } from '@univerjs/engine-formula'; 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'; import { matchToken, sequenceNodeType } from '@univerjs/engine-formula';
const fullWidthToHalfWidthMap: { [key: string]: string } = { const fullWidthToHalfWidthMap: { [key: string]: string } = {
@@ -94,7 +94,7 @@ export function normalizeString(str: string, lexerTreeBuilder: LexerTreeBuilder,
} }
// Formatting Numbers // Formatting Numbers
const parsedValue = numfmt.parseValue(normalStr); const parsedValue = getNumfmtParseValueFilter(normalStr);
return parsedValue == null ? str : normalStr; return parsedValue == null ? str : normalStr;
} }
@@ -160,7 +160,7 @@ function normalizeFormulaString(str: string, normalStr: string, lexerTreeBuilder
const endIndex = node.endIndex + totalOffset + 1; const endIndex = node.endIndex + totalOffset + 1;
_normalStr = replaceString(str.slice(startIndex, endIndex), _normalStr, startIndex, endIndex); _normalStr = replaceString(str.slice(startIndex, endIndex), _normalStr, startIndex, endIndex);
} else if (node.nodeType !== sequenceNodeType.ARRAY) { } else if (node.nodeType !== sequenceNodeType.ARRAY) {
const parsedValue = numfmt.parseValue(token); const parsedValue = getNumfmtParseValueFilter(token);
if (parsedValue == null) { if (parsedValue == null) {
const startIndex = node.startIndex + totalOffset + 1; const startIndex = node.startIndex + totalOffset + 1;
@@ -19,7 +19,6 @@
import type { ICustomRange, IDocumentBody, IDocumentData, ITextRun, ITextStyle, Nullable } from '@univerjs/core'; import type { ICustomRange, IDocumentBody, IDocumentData, ITextRun, ITextStyle, Nullable } from '@univerjs/core';
import type { SpreadsheetSkeleton } from '@univerjs/engine-render'; import type { SpreadsheetSkeleton } from '@univerjs/engine-render';
import type { ISheetSkeletonManagerParam } from '../../sheet-skeleton-manager.service'; import type { ISheetSkeletonManagerParam } from '../../sheet-skeleton-manager.service';
import type { import type {
ICellDataWithSpanInfo, ICellDataWithSpanInfo,
IClipboardPropertyItem, IClipboardPropertyItem,
@@ -27,7 +26,7 @@ import type {
IUniverSheetCopyDataModel, IUniverSheetCopyDataModel,
} from '../type'; } from '../type';
import type { IAfterProcessRule, IPastePlugin } from './paste-plugins/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 { handleStringToStyle, textTrim } from '@univerjs/ui';
import { extractNodeStyle } from './parse-node-style'; import { extractNodeStyle } from './parse-node-style';
import parseToDom, { convertToCellStyle, generateParagraphs } from './utils'; 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 " * "<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')) { if (cell.innerHTML.includes('mso-spacerun:yes')) {
const cellText = cell.innerHTML.replace(/<span[^>]*mso-spacerun:yes[^>]*>[\s\S]*?<\/span>/gi, ''); const cellText = cell.innerHTML.replace(/<span[^>]*mso-spacerun:yes[^>]*>[\s\S]*?<\/span>/gi, '').replace(/\s/g, '');
const parseInfo = numfmt.parseNumber(cellText); const parseInfo = getNumfmtParseValueFilter(cellText);
if (parseInfo && parseInfo.v !== undefined && parseInfo.v !== null) { if (parseInfo && parseInfo.v !== undefined && parseInfo.v !== null) {
return { return {