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(sheet): ensure numfmt crash not effect all (#6201)

VicKun57c1d6814364
diff --git a/packages/sheets-numfmt/src/utils/pattern.ts b/packages/sheets-numfmt/src/utils/pattern.ts
index fb33bb2337..91f28a2a38 100644
--- a/packages/sheets-numfmt/src/utils/pattern.ts
+++ b/packages/sheets-numfmt/src/utils/pattern.ts
@@ -27,19 +27,27 @@ interface IPatternPreview {
export const getPatternPreview = (pattern: string, value: number, locale: INumfmtLocaleTag = 'en'): IPatternPreview => {
// in the source code of numfmt, the formatColor function will read the the partitions[3]
- const formatColor = numfmt.formatColor(pattern, value);
- const color = formatColor ? String(formatColor) : undefined;
- const result = numfmt.format(pattern, value, { locale, throws: false });
- if (value < 0) {
- // pay attention, controllers/numfmt.controller.ts
- // in the pattern, the negative value color may be upper case one , so if we read a color with UpperCase, we should return the color with lower case for our theme system
+ try {
+ const formatColor = numfmt.formatColor(pattern, value);
+ const color = formatColor ? String(formatColor) : undefined;
+ const result = numfmt.format(pattern, value, { locale, throws: false });
+ if (value < 0) {
+ // pay attention, controllers/numfmt.controller.ts
+ // in the pattern, the negative value color may be upper case one , so if we read a color with UpperCase, we should return the color with lower case for our theme system
+ return {
+ result,
+ color,
+ };
+ }
return {
result,
- color,
};
+ } catch (e) {
+ console.warn('getPatternPreview error:', pattern, e);
}
+
return {
- result,
+ result: String(value),
};
};