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(formula): fix calculation errors in formulas, such as 1,5 (#6579)

wpxp123456a1048fb232ae
diff --git a/packages/core/src/shared/numfmt.ts b/packages/core/src/shared/numfmt.ts
index 8bdd715cd1..3d7ab38e5a 100644
--- a/packages/core/src/shared/numfmt.ts
+++ b/packages/core/src/shared/numfmt.ts
@@ -122,6 +122,7 @@ export const isPatternEqualWithoutDecimal = (patternA: string, patternB: string)
const ignoreCommonPatterns = new Set(['m d']);
const ignoreAMPMPatterns = new Set(['h:mm AM/PM', 'hh:mm AM/PM']);
+const currencySymbols = new Set(['$', '¥', '₽', '₫', 'NT$', '€', '₩', '﷼']);
/**
* Get the numfmt parse value, and filter out the parse error.
@@ -131,7 +132,7 @@ export const getNumfmtParseValueFilter = (value: string): numfmt.ParseData | nul
if (!parseData) return null;
- const { v, z } = parseData;
+ const { z } = parseData;
if (z) {
/**
@@ -152,8 +153,18 @@ export const getNumfmtParseValueFilter = (value: string): numfmt.ParseData | nul
* Verify by formatting back to string
* '1000,' => '#,##0,' ----- error
* '1000,1.00' => '#,##0.00' ----- error
+ * '$1000' => '$#,##0' ----- true
*/
- if (z.includes('#,##0') && numfmt.format(z, v) !== value) return null;
+ if (z.includes('#,##0')) {
+ if (/[.,]$/.test(value)) return null;
+
+ const normalized = value.replace(new RegExp(`^[${[...currencySymbols].join('')}]+`), '').trim();
+
+ if (normalized.includes(',')) {
+ const validGrouping = /^-?\d{1,3}(,\d{3})*(\.\d+)?$/.test(normalized);
+ if (!validGrouping) return null;
+ }
+ }
}
return parseData;
diff --git a/packages/engine-formula/src/engine/utils/numfmt-kit.ts b/packages/engine-formula/src/engine/utils/numfmt-kit.ts
index 51b47ffc3d..a709a20711 100644
--- a/packages/engine-formula/src/engine/utils/numfmt-kit.ts
+++ b/packages/engine-formula/src/engine/utils/numfmt-kit.ts
@@ -15,7 +15,7 @@
*/
import type { ICellData, Nullable, Styles } from '@univerjs/core';
-import { LocaleType, numfmt } from '@univerjs/core';
+import { getNumfmtParseValueFilter, LocaleType, numfmt } from '@univerjs/core';
import { FormulaAstLRU } from '../../basics/cache-lru';
import { operatorToken } from '../../basics/token';
@@ -352,22 +352,10 @@ export function stringIsNumberPattern(input: string) {
};
}
- const numberPattern = numfmt.parseNumber(_input);
+ const parseData = getNumfmtParseValueFilter(_input);
- if (numberPattern && numberPattern.z) {
- return setNumberPatternCache(_input, numberPattern.v as number, numberPattern.z as string);
- }
-
- const datePattern = numfmt.parseDate(_input);
-
- if (datePattern && datePattern.z) {
- return setNumberPatternCache(_input, datePattern.v as number, datePattern.z as string);
- }
-
- const timePattern = numfmt.parseTime(_input);
-
- if (timePattern && timePattern.z) {
- return setNumberPatternCache(_input, timePattern.v as number, timePattern.z as string);
+ if (parseData && parseData.z) {
+ return setNumberPatternCache(_input, parseData.v as number, parseData.z as string);
}
return {
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 0e920849a4..55f032844c 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
@@ -510,7 +510,7 @@ describe('test editor', () => {
};
const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
- expect(result?.v).toBe(excelDateSerial(new Date(new Date().getFullYear(), 1, 3)));
+ expect(result?.v).toBe(excelDateSerial(new Date(Date.UTC(new Date().getUTCFullYear(), 1, 3))));
expect(result?.t).toBe(CellValueType.NUMBER);
});
@@ -531,6 +531,42 @@ describe('test editor', () => {
expect(result?.v).toBe('1000,');
});
+
+ it('edit content 1000,1.00', () => {
+ const sheetInterceptorService = testBed.get(SheetInterceptorService);
+ const cellData = { v: '1000,1.00' };
+ 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,1.00');
+ });
+
+ it('edit content $1000', () => {
+ const sheetInterceptorService = testBed.get(SheetInterceptorService);
+ const cellData = { v: '$1000' };
+ 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);
+ });
});
describe('test get cell text/plain', () => {
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 29525ab5ed..103e49c763 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
@@ -17,7 +17,7 @@
import type { Workbook } from '@univerjs/core';
import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
import type { IUniverSheetsUIConfig } from './config.schema';
-import { CellValueType, Disposable, IConfigService, Inject, isRealNum, isTextFormat, LocaleService, numfmt } from '@univerjs/core';
+import { CellValueType, Disposable, getNumfmtParseValueFilter, IConfigService, Inject, isRealNum, isTextFormat, 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';
@@ -81,7 +81,7 @@ export class ForceStringAlertRenderController extends Disposable implements IRen
*/
if (
(cellData.t === CellValueType.FORCE_STRING || cellData.t === CellValueType.STRING) &&
- (isRealNum(cellData.v) || (typeof cellData.v === 'string' && numfmt.parseNumber(cellData.v)))
+ (isRealNum(cellData.v) || (typeof cellData.v === 'string' && getNumfmtParseValueFilter(cellData.v)))
) {
// If the user has disabled the force string alert, do not show it
if (this._configService.getConfig<IUniverSheetsUIConfig>(SHEETS_UI_PLUGIN_CONFIG_KEY)?.disableForceStringAlert) {
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 d35bf0a422..308aac9b60 100644
--- a/packages/sheets-ui/src/controllers/force-string-render.controller.ts
+++ b/packages/sheets-ui/src/controllers/force-string-render.controller.ts
@@ -17,7 +17,7 @@
import type { Workbook } from '@univerjs/core';
import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
import type { IUniverSheetsUIConfig } from './config.schema';
-import { CellValueType, IConfigService, Inject, InterceptorEffectEnum, isRealNum, isTextFormat, numfmt, RxDisposable } from '@univerjs/core';
+import { CellValueType, getNumfmtParseValueFilter, IConfigService, Inject, InterceptorEffectEnum, isRealNum, isTextFormat, RxDisposable } from '@univerjs/core';
import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';
import { SHEETS_UI_PLUGIN_CONFIG_KEY } from './config.schema';
@@ -70,7 +70,7 @@ export class ForceStringRenderController extends RxDisposable implements IRender
*/
if (
(cell?.t === CellValueType.FORCE_STRING || cell?.t === CellValueType.STRING) &&
- (isRealNum(cellRaw.v) || (typeof cellRaw.v === 'string' && numfmt.parseNumber(cellRaw.v)))
+ (isRealNum(cellRaw.v) || (typeof cellRaw.v === 'string' && getNumfmtParseValueFilter(cellRaw.v)))
) {
// If the cell is in text format, follow the logic of number format
const cellStyle = pos.workbook.getStyles().get(cellRaw.s);