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.

feat: optimize the display of numbers that lose precision (#5242)

wpxp1234562a57b57325b1
diff --git a/examples/src/sheets/main.ts b/examples/src/sheets/main.ts
index 6f95b8d4c7..367ca4a971 100644
--- a/examples/src/sheets/main.ts
+++ b/examples/src/sheets/main.ts
@@ -107,10 +107,7 @@ function createNewInstance() {
autoHeightForMergedCells: true,
});
univer.registerPlugin(UniverSheetsUIPlugin);
- univer.registerPlugin(UniverSheetsNumfmtPlugin, {
- disableTextFormatAlert: true,
- disableTextFormatMark: true,
- });
+ univer.registerPlugin(UniverSheetsNumfmtPlugin);
univer.registerPlugin(UniverSheetsZenEditorPlugin);
univer.registerPlugin(UniverFormulaEnginePlugin, { notExecuteFormula: true });
univer.registerPlugin(UniverSheetsFormulaPlugin, { notExecuteFormula: true });
diff --git a/packages/core/src/common/number.ts b/packages/core/src/common/number.ts
index ccbd9ff2c6..73c627a5fc 100644
--- a/packages/core/src/common/number.ts
+++ b/packages/core/src/common/number.ts
@@ -26,3 +26,12 @@ export function isSafeNumeric(str: string): boolean {
return Number(str) <= Number.MAX_SAFE_INTEGER;
}
+
+/**
+ * Whether the numeric string will lose precision when converted to a number.
+ * e.g. '123456789123456789' -> 123456789123456780
+ * e.g. '1212121212121212.2345' -> 1212121212121212.2
+ */
+export function willLoseNumericPrecision(str: string): boolean {
+ return Number(str) > Number.MAX_SAFE_INTEGER || str.length >= 18;
+}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index a14a031061..a7a4eb7b56 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -35,7 +35,7 @@ export { AsyncInterceptorManager, composeInterceptors, createAsyncInterceptorKey
export type { Serializable } from './common/json';
export { MemoryCursor } from './common/memory-cursor';
export { mixinClass } from './common/mixin';
-export { isNumeric, isSafeNumeric } from './common/number';
+export { isNumeric, isSafeNumeric, willLoseNumericPrecision } from './common/number';
export { Registry, RegistryAsMap } from './common/registry';
export { requestImmediateMacroTask } from './common/request-immediate-macro-task';
export { type ISequenceExecuteResult, sequence, sequenceAsync } from './common/sequence';
diff --git a/packages/engine-formula/src/engine/utils/math-kit.ts b/packages/engine-formula/src/engine/utils/math-kit.ts
index 39e096e1ec..2c8c4b5985 100644
--- a/packages/engine-formula/src/engine/utils/math-kit.ts
+++ b/packages/engine-formula/src/engine/utils/math-kit.ts
@@ -188,6 +188,12 @@ export function lessThanOrEquals(a: number, b: number): boolean {
* @returns
*/
export function strip(num: number, precision = 15) {
+ // If the integer part of the number is greater than or equal to the precision, return the number directly
+ // e.g. 1212121212121212 should not be displayed as 1212121212121210
+ if (Math.floor(Math.abs(num)).toString().length >= precision) {
+ return num;
+ }
+
return Number.parseFloat(num.toPrecision(precision));
}
diff --git a/packages/sheets-numfmt-ui/src/controllers/__tests__/editor.controller.spec.ts b/packages/sheets-numfmt-ui/src/controllers/__tests__/editor.controller.spec.ts
index 160621ab42..c192ff31f4 100644
--- a/packages/sheets-numfmt-ui/src/controllers/__tests__/editor.controller.spec.ts
+++ b/packages/sheets-numfmt-ui/src/controllers/__tests__/editor.controller.spec.ts
@@ -16,7 +16,7 @@
import type { ICellDataForSheetInterceptor, Injector, Univer, Workbook, Worksheet } from '@univerjs/core';
import type { ISetNumfmtMutationParams, ISheetLocation } from '@univerjs/sheets';
-import { CellModeEnum, createInterceptorKey, ICommandService, InterceptorManager, IUniverInstanceService, LocaleType, UniverInstanceType } from '@univerjs/core';
+import { CellModeEnum, CellValueType, createInterceptorKey, ICommandService, InterceptorManager, IUniverInstanceService, LocaleType, UniverInstanceType } from '@univerjs/core';
import { SetNumfmtMutation, SheetInterceptorService } from '@univerjs/sheets';
import { SheetsNumfmtCellContentController } from '@univerjs/sheets-numfmt';
@@ -155,7 +155,6 @@ describe('test editor', () => {
col: 0,
origin: cellData,
};
-
const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
// The date-time drop is a numeric value, not a literal string
expect(result?.v).toBe(0.5231712962962963);
@@ -318,6 +317,120 @@ describe('test editor', () => {
expect(result?.p).toEqual(richTextParams);
expect(result?.v).toBeFalsy();
});
+
+ it('edit number 123456789123456789', () => {
+ const sheetInterceptorService = testBed.get(SheetInterceptorService);
+ const cellData = { v: '123456789123456789' };
+ const location = {
+ workbook,
+ worksheet,
+ unitId,
+ subUnitId,
+ row: 0,
+ col: 0,
+ origin: cellData,
+ };
+
+ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
+
+ expect(result?.v).toBe('123456789123456789');
+ expect(result?.t).toBe(CellValueType.FORCE_STRING);
+ });
+
+ it('edit number 1212121212121212.2345', () => {
+ const sheetInterceptorService = testBed.get(SheetInterceptorService);
+ const cellData = { v: '1212121212121212.2345' };
+ const location = {
+ workbook,
+ worksheet,
+ unitId,
+ subUnitId,
+ row: 0,
+ col: 0,
+ origin: cellData,
+ };
+
+ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
+
+ expect(result?.v).toBe('1212121212121212.2345');
+ expect(result?.t).toBe(CellValueType.FORCE_STRING);
+ });
+
+ it('edit number 1212121212121212', () => {
+ const sheetInterceptorService = testBed.get(SheetInterceptorService);
+ const cellData = { v: '1212121212121212' };
+ const location = {
+ workbook,
+ worksheet,
+ unitId,
+ subUnitId,
+ row: 0,
+ col: 0,
+ origin: cellData,
+ };
+
+ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
+
+ expect(result?.v).toBe(1212121212121212);
+ expect(result?.t).toBe(CellValueType.NUMBER);
+ });
+
+ it('edit number 1e3', () => {
+ const sheetInterceptorService = testBed.get(SheetInterceptorService);
+ const cellData = { v: '1e3' };
+ const location = {
+ workbook,
+ worksheet,
+ unitId,
+ subUnitId,
+ row: 0,
+ col: 0,
+ origin: cellData,
+ };
+
+ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
+
+ expect(result?.v).toBe(1000);
+ expect(result?.t).toBe(CellValueType.NUMBER);
+ });
+
+ it('edit number 001', () => {
+ const sheetInterceptorService = testBed.get(SheetInterceptorService);
+ const cellData = { v: '001' };
+ const location = {
+ workbook,
+ worksheet,
+ unitId,
+ subUnitId,
+ row: 0,
+ col: 0,
+ origin: cellData,
+ };
+
+ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
+
+ expect(result?.v).toBe(1);
+ expect(result?.t).toBe(CellValueType.NUMBER);
+ });
+
+ it('edit number +123.', () => {
+ const sheetInterceptorService = testBed.get(SheetInterceptorService);
+ const cellData = { v: '+123.' };
+ const location = {
+ workbook,
+ worksheet,
+ unitId,
+ subUnitId,
+ row: 0,
+ col: 0,
+ origin: cellData,
+ };
+
+ const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
+
+ expect(result?.v).toBe(123);
+ expect(result?.t).toBe(CellValueType.NUMBER);
+ });
});
describe('test get cell text/plain', () => {
diff --git a/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts b/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts
index 057f3482e6..5a4139441e 100644
--- a/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts
+++ b/packages/sheets-numfmt-ui/src/controllers/numfmt.editor.controller.ts
@@ -31,6 +31,7 @@ import {
Optional,
toDisposable,
UniverInstanceType,
+ willLoseNumericPrecision,
} from '@univerjs/core';
import { isTextFormat } from '@univerjs/engine-numfmt';
import {
@@ -45,7 +46,6 @@ import {
SheetInterceptorService,
transformCellsToRange,
} from '@univerjs/sheets';
-
import { getPatternType } from '@univerjs/sheets-numfmt';
import { IEditorBridgeService } from '@univerjs/sheets-ui';
@@ -136,14 +136,14 @@ export class NumfmtEditorController extends Disposable {
* @private
* @memberof NumfmtService
*/
-
+ // eslint-disable-next-line max-lines-per-function
private _initInterceptorEditorEnd() {
this.disposeWithMe(
toDisposable(
this._sheetInterceptorService.writeCellInterceptor.intercept(AFTER_CELL_EDIT, {
- // eslint-disable-next-line complexity
+ // eslint-disable-next-line complexity
handler: (value, context, next) => {
- // clear the effect
+ // clear the effect
this._collectEffectMutation.clean();
const currentNumfmtValue = this._numfmtService.getValue(
context.unitId,
@@ -166,7 +166,7 @@ export class NumfmtEditorController extends Disposable {
return next(value);
}
- // if the cell is text format or force string, do not convert the value
+ // if the cell is text format or force string, do not convert the value
if (isTextFormat(currentNumfmtValue?.pattern) || value.t === CellValueType.FORCE_STRING) {
return next(value);
}
@@ -189,6 +189,18 @@ export class NumfmtEditorController extends Disposable {
}
if (numfmtInfo) {
+ // If the numeric string will lose precision when converted to a number, set the cell type to force string
+ // e.g. 123456789123456789
+ // e.g. 1212121212121212.2345
+ if (!numfmtInfo.z && willLoseNumericPrecision(content)) {
+ return next({
+ ...value,
+ p: undefined,
+ v: content,
+ t: CellValueType.FORCE_STRING,
+ });
+ }
+
if (numfmtInfo.z) {
this._collectEffectMutation.add(
context.unitId,
@@ -200,7 +212,9 @@ export class NumfmtEditorController extends Disposable {
}
);
}
+
const v = Number(numfmtInfo.v);
+
return next({ ...value, p: undefined, v, t: CellValueType.NUMBER });
}
// else if (['date', 'time', 'datetime', 'percent'].includes(currentNumfmtType) || !isNumeric(content)) {
diff --git a/packages/sheets-ui/src/controllers/clipboard/utils.ts b/packages/sheets-ui/src/controllers/clipboard/utils.ts
index 407fef4d70..994b58945d 100644
--- a/packages/sheets-ui/src/controllers/clipboard/utils.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/utils.ts
@@ -27,7 +27,7 @@ import type {
import type { ICellDataWithSpanInfo, ICopyPastePayload, ISheetDiscreteRangeLocation } from '../../services/clipboard/type';
import type { IDiscreteRange } from '../utils/range-tools';
-import { cellToRange, CellValueType, CustomRangeType, DEFAULT_STYLES, generateRandomId, IUniverInstanceService, numfmt, ObjectMatrix, Range, Rectangle, Tools } from '@univerjs/core';
+import { cellToRange, CellValueType, CustomRangeType, DEFAULT_STYLES, generateRandomId, IUniverInstanceService, numfmt, ObjectMatrix, Range, Rectangle, Tools, willLoseNumericPrecision } from '@univerjs/core';
import { isTextFormat } from '@univerjs/engine-numfmt';
import { DEFAULT_PADDING_DATA } from '@univerjs/engine-render';
import {
@@ -351,7 +351,14 @@ export function getSetCellValueMutations(
const content = String(value.v);
const numfmtValue = numfmt.parseValue(content);
if (numfmtValue?.v !== undefined && typeof numfmtValue.v === 'number') {
- cellValue.v = numfmtValue.v;
+ // If the numeric string will lose precision when converted to a number, set the cell type to force string
+ // e.g. 123456789123456789
+ // e.g. 1212121212121212.2345
+ if (!numfmtValue.z && willLoseNumericPrecision(content)) {
+ cellValue.t = CellValueType.FORCE_STRING;
+ } else {
+ cellValue.v = numfmtValue.v;
+ }
}
}
}
diff --git a/packages/sheets-ui/src/controllers/config.schema.ts b/packages/sheets-ui/src/controllers/config.schema.ts
index 3da2c97fe2..947aeaee54 100644
--- a/packages/sheets-ui/src/controllers/config.schema.ts
+++ b/packages/sheets-ui/src/controllers/config.schema.ts
@@ -98,6 +98,16 @@ export interface IUniverSheetsUIConfig {
framework: 'react' | 'vue3';
};
+ /**
+ * Whether to disable the force string alert.
+ * @default false
+ */
+ disableForceStringAlert?: boolean;
+ /**
+ * Whether to disable the force string mark.
+ * @default false
+ */
+ disableForceStringMark?: boolean;
}
export const defaultPluginConfig: IUniverSheetsUIConfig = {
diff --git a/packages/sheets-ui/src/controllers/force-string-alert-render.controller.ts b/packages/sheets-ui/src/controllers/force-string-alert-render.controller.ts
index b57f70608c..8fd5d915f8 100644
--- a/packages/sheets-ui/src/controllers/force-string-alert-render.controller.ts
+++ b/packages/sheets-ui/src/controllers/force-string-alert-render.controller.ts
@@ -16,10 +16,12 @@
import type { Workbook } from '@univerjs/core';
import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
-import { CellValueType, Disposable, Inject, isRealNum, LocaleService } from '@univerjs/core';
+import type { IUniverSheetsUIConfig } from './config.schema';
+import { CellValueType, Disposable, IConfigService, Inject, isRealNum, LocaleService } from '@univerjs/core';
import { IZenZoneService } from '@univerjs/ui';
import { CellAlertManagerService, CellAlertType } from '../services/cell-alert-manager.service';
import { HoverManagerService } from '../services/hover-manager.service';
+import { SHEETS_UI_PLUGIN_CONFIG_KEY } from './config.schema';
const ALERT_KEY = 'SHEET_FORCE_STRING_ALERT';
@@ -29,7 +31,8 @@ export class ForceStringAlertRenderController extends Disposable implements IRen
@Inject(HoverManagerService) private readonly _hoverManagerService: HoverManagerService,
@Inject(CellAlertManagerService) private readonly _cellAlertManagerService: CellAlertManagerService,
@Inject(LocaleService) private readonly _localeService: LocaleService,
- @IZenZoneService private readonly _zenZoneService: IZenZoneService
+ @IZenZoneService private readonly _zenZoneService: IZenZoneService,
+ @IConfigService private readonly _configService: IConfigService
) {
super();
this._init();
@@ -63,6 +66,11 @@ export class ForceStringAlertRenderController extends Disposable implements IRen
return;
}
+ // If the user has disabled the force string alert, do not show it
+ if (this._configService.getConfig<IUniverSheetsUIConfig>(SHEETS_UI_PLUGIN_CONFIG_KEY)?.disableForceStringAlert) {
+ return;
+ }
+
this._cellAlertManagerService.showAlert({
type: CellAlertType.ERROR,
title: this._localeService.t('info.error'),
diff --git a/packages/sheets-ui/src/controllers/force-string-render.controller.ts b/packages/sheets-ui/src/controllers/force-string-render.controller.ts
index e10420b97b..ce08e577aa 100644
--- a/packages/sheets-ui/src/controllers/force-string-render.controller.ts
+++ b/packages/sheets-ui/src/controllers/force-string-render.controller.ts
@@ -16,16 +16,19 @@
import type { Workbook } from '@univerjs/core';
import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
-import { CellValueType, Inject, InterceptorEffectEnum, isRealNum, RxDisposable } from '@univerjs/core';
+import type { IUniverSheetsUIConfig } from './config.schema';
+import { CellValueType, IConfigService, Inject, InterceptorEffectEnum, isRealNum, RxDisposable } from '@univerjs/core';
import { isTextFormat } from '@univerjs/engine-numfmt';
import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';
+import { SHEETS_UI_PLUGIN_CONFIG_KEY } from './config.schema';
export class ForceStringRenderController extends RxDisposable implements IRenderModule {
constructor(
private readonly _context: IRenderContext<Workbook>,
@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,
- @Inject(SheetInterceptorService) private readonly _sheetInterceptorService: SheetInterceptorService
+ @Inject(SheetInterceptorService) private readonly _sheetInterceptorService: SheetInterceptorService,
+ @IConfigService private readonly _configService: IConfigService
) {
super();
@@ -65,6 +68,11 @@ export class ForceStringRenderController extends RxDisposable implements IRender
return next(cell);
}
+ // If the user has disabled the force string mark, do not show it
+ if (this._configService.getConfig<IUniverSheetsUIConfig>(SHEETS_UI_PLUGIN_CONFIG_KEY)?.disableForceStringMark) {
+ return next(cell);
+ }
+
return next({
...cell,
markers: {
diff --git a/packages/sheets-ui/src/services/auto-fill/tools.ts b/packages/sheets-ui/src/services/auto-fill/tools.ts
index 6fc36a10ad..b53b21687c 100644
--- a/packages/sheets-ui/src/services/auto-fill/tools.ts
+++ b/packages/sheets-ui/src/services/auto-fill/tools.ts
@@ -471,11 +471,13 @@ export function fillExtendNumber(data: Array<Nullable<ICellData>>, len: number,
removeCellCustom(d);
const last = `${data[data.length - 1]?.v}`;
+ if (!last) continue;
+
const match = last?.match(reg);
const lastTxt = match?.[match.length - 1];
+ if (!lastTxt) continue;
const num = Math.abs(Number(lastTxt) + step * i);
- if (!last || !lastTxt) continue;
const lastIndex = last.lastIndexOf(lastTxt);
const valueTxt = last.substr(0, lastIndex) + num.toString() + last.substr(lastIndex + lastTxt.length);
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 bfea56f2fe..f07e94009a 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
@@ -15,7 +15,7 @@
*/
import type { ICellData, Injector, IStyleData, Nullable, Univer } from '@univerjs/core';
-import { ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE } from '@univerjs/core';
+import { CellValueType, ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE } from '@univerjs/core';
import { DEFAULT_TEXT_FORMAT_EXCEL } from '@univerjs/engine-numfmt';
import {
AddWorksheetMergeMutation,
@@ -32,7 +32,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 } from './constant';
+import { excelSample, excelSample2, excelSample3 } from './constant';
describe('Test clipboard', () => {
let univer: Univer;
@@ -324,5 +324,35 @@ describe('Test clipboard', () => {
pd: { t: 0, b: 2, l: 2, r: 2 },
});
});
+
+ it('copy number 123456789123456789', 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(excelSample3);
+ expect(res).toBeTruthy();
+
+ // check the values
+ const cellValue = getValues(0, 0, 0, 0)?.[0]?.[0];
+ expect(cellValue?.v).toStrictEqual('123456789123456789');
+ expect(cellValue?.t).toStrictEqual(CellValueType.FORCE_STRING);
+ });
});
});
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/constant.ts b/packages/sheets-ui/src/services/clipboard/__tests__/constant.ts
index 71d542caa2..d7d2b38a37 100644
--- a/packages/sheets-ui/src/services/clipboard/__tests__/constant.ts
+++ b/packages/sheets-ui/src/services/clipboard/__tests__/constant.ts
@@ -19,6 +19,9 @@ export const excelSample = '<html xmlns:v="urn:schemas-microsoft-com:vml"\r\nxml
// copy value is 000123456
export const excelSample2 = '<html xmlns: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:middle;\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:"\\@";}\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=115 style=\'border-collapse:\r\n collapse;width:86pt\'>\r\n <col width=115 style=\'mso-width-source:userset;mso-width-alt:3680;width:86pt\'>\r\n <tr height=19 style=\'height:14.25pt\'>\r\n\x3C!--StartFragment-->\r\n <td height=19 class=xl65 width=115 style=\'height:14.25pt;width:86pt\'>000123456</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 123456789123456789
+export const excelSample3 = '<html xmlns: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:middle;\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:"\\@";}\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=115 style=\'border-collapse:\r\n collapse;width:86pt\'>\r\n <col width=115 style=\'mso-width-source:userset;mso-width-alt:3680;width:86pt\'>\r\n <tr height=19 style=\'height:14.25pt\'>\r\n\x3C!--StartFragment-->\r\n <td height=19 class=xl65 width=115 style=\'height:14.25pt;width:86pt\'>123456789123456789</td>\r\n\x3C!--EndFragment-->\r\n </tr>\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/src/basics/cell-type.ts b/packages/sheets/src/basics/cell-type.ts
index 85a28e6184..edb5e0783d 100644
--- a/packages/sheets/src/basics/cell-type.ts
+++ b/packages/sheets/src/basics/cell-type.ts
@@ -15,7 +15,7 @@
*/
import type { CellValue, ICellData, IStyleData, Nullable, Styles } from '@univerjs/core';
-import { CellValueType, isBooleanString, isRealNum } from '@univerjs/core';
+import { CellValueType, isBooleanString, isRealNum, willLoseNumericPrecision } from '@univerjs/core';
import { isTextFormat } from '@univerjs/engine-numfmt';
/**
@@ -89,10 +89,20 @@ export function checkCellValueType(v: Nullable<CellValue>, type: Nullable<CellVa
return CellValueType.BOOLEAN;
}
+ // If the numeric string will lose precision when converted to a number, set the cell type to force string
+ // e.g. 123456789123456789
+ // e.g. 1212121212121212.2345
+ if (willLoseNumericPrecision(v)) {
+ return CellValueType.FORCE_STRING;
+ }
+
return CellValueType.NUMBER;
- } else if (isBooleanString(v)) {
+ }
+
+ if (isBooleanString(v)) {
return CellValueType.BOOLEAN;
}
+
return CellValueType.STRING;
}
@@ -100,6 +110,7 @@ export function checkCellValueType(v: Nullable<CellValue>, type: Nullable<CellVa
if ((v === 0 || v === 1) && type === CellValueType.BOOLEAN) {
return CellValueType.BOOLEAN;
}
+
return CellValueType.NUMBER;
}