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(numfmt): do not block when syntax is not supported (#3491)

Gpound.liub8c9256bc481
diff --git a/packages/engine-formula/src/basics/format.ts b/packages/engine-formula/src/basics/format.ts
index c094779a81..aa1a1b9d64 100644
--- a/packages/engine-formula/src/basics/format.ts
+++ b/packages/engine-formula/src/basics/format.ts
@@ -25,5 +25,5 @@ import { numfmt } from '@univerjs/core';
* @returns
*/
export const getFormatPreview = (pattern: string, value: number) => {
- return numfmt.format(pattern, value);
+ return numfmt.format(pattern, value, { throws: false });
};
diff --git a/packages/sheets-data-validation/src/validators/list-validator.ts b/packages/sheets-data-validation/src/validators/list-validator.ts
index ba1aead4b0..a715379b4b 100644
--- a/packages/sheets-data-validation/src/validators/list-validator.ts
+++ b/packages/sheets-data-validation/src/validators/list-validator.ts
@@ -38,7 +38,7 @@ export function getRuleFormulaResultSet(result: Nullable<Nullable<ICellData>[][]
const value = getCellValueOrigin(cell);
if (value !== null && value !== undefined) {
if (typeof value !== 'string' && typeof cell?.s === 'object' && cell.s?.n?.pattern) {
- resultSet.add(numfmt.format(cell.s.n.pattern, value));
+ resultSet.add(numfmt.format(cell.s.n.pattern, value, { throws: false }));
return;
}
resultSet.add(value.toString());
diff --git a/packages/sheets-numfmt/src/utils/pattern.ts b/packages/sheets-numfmt/src/utils/pattern.ts
index bb57d91052..33642a0554 100644
--- a/packages/sheets-numfmt/src/utils/pattern.ts
+++ b/packages/sheets-numfmt/src/utils/pattern.ts
@@ -22,7 +22,7 @@ export const getPatternPreview = (pattern: string, value: number, _locale?: Loca
const info = numfmt.getInfo(pattern);
const locale = _locale === LocaleType.ZH_CN ? 'zh-CN' : 'en';
const negInfo = info._partitions[1];
- const result = numfmt.format(pattern, value, { locale });
+ const result = numfmt.format(pattern, value, { locale, throws: false });
if (value < 0) {
return {
result,
diff --git a/packages/sheets-ui/src/views/status-bar/CopyableStatisticItem.tsx b/packages/sheets-ui/src/views/status-bar/CopyableStatisticItem.tsx
index 1f1d33f7dc..3dc560e4f5 100644
--- a/packages/sheets-ui/src/views/status-bar/CopyableStatisticItem.tsx
+++ b/packages/sheets-ui/src/views/status-bar/CopyableStatisticItem.tsx
@@ -16,10 +16,10 @@
import { LocaleService, numfmt, useDependency } from '@univerjs/core';
import { MessageType, Tooltip } from '@univerjs/design';
-import type { IFunctionNames } from '@univerjs/engine-formula';
import { FUNCTION_NAMES_MATH, FUNCTION_NAMES_STATISTICAL, FUNCTION_NAMES_TEXT } from '@univerjs/engine-formula';
import { IClipboardInterfaceService, IMessageService } from '@univerjs/ui';
import React from 'react';
+import type { IFunctionNames } from '@univerjs/engine-formula';
import styles from './index.module.less';
@@ -89,7 +89,7 @@ export function formatNumber(item: IStatisticItem) {
}
if (pattern && allowPatternFunctions.includes(item.name)) {
- return numfmt.format(pattern, num);
+ return numfmt.format(pattern, num, { throws: false });
}
return num.toLocaleString();