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(core): preserve escaped line separators in number formats (#7717)

Univerf1719b203490
diff --git a/packages/core/src/shared/numfmt/__tests__/format-api.spec.ts b/packages/core/src/shared/numfmt/__tests__/format-api.spec.ts
index aa49917693..8a6c244bbe 100644
--- a/packages/core/src/shared/numfmt/__tests__/format-api.spec.ts
+++ b/packages/core/src/shared/numfmt/__tests__/format-api.spec.ts
@@ -23,6 +23,14 @@ import { describe, expect, it } from 'vitest';
import * as numfmt from '../api';
describe('local numfmt API', () => {
+ it.each(['\n', '\r', '\u2028', '\u2029'])('preserves escaped line separators in Excel formats (%j)', (separator) => {
+ const pattern = `[$-409]m/d\\${separator}h:mm;@`;
+ expect(numfmt.isValidFormat(pattern)).toBe(true);
+ expect(numfmt.format(pattern, 44137.5)).toBe(`11/2${separator}12:00`);
+ expect(numfmt.format(`0\\${separator}0`, 12)).toBe(`1${separator}2`);
+ expect(numfmt.format('0\\n', 12)).toBe('12n');
+ });
+
it('exposes only the planned runtime surface', () => {
expect(Object.keys(numfmt).sort()).toEqual([
'addLocale',
diff --git a/packages/core/src/shared/numfmt/tokenize.ts b/packages/core/src/shared/numfmt/tokenize.ts
index 477eebc997..94752f9b64 100644
--- a/packages/core/src/shared/numfmt/tokenize.ts
+++ b/packages/core/src/shared/numfmt/tokenize.ts
@@ -89,7 +89,7 @@ const tokenHandlers: readonly TokenHandler[] = [
// conditionally allow these open ended directions?
[TOKEN_MODIFIER, /^\[([^\]]+)\]/, 1],
[TOKEN_AMPM, /^(?:AM\/PM|am\/pm|A\/P)/, 0],
- [TOKEN_ESCAPED, /^\\(.)/, 1],
+ [TOKEN_ESCAPED, /^\\([\s\S])/, 1],
[TOKEN_STRING, /^"([^"]*?)"/, 1],
[TOKEN_SKIP, /^_(\\.|.)/, 1],
// Google Sheets and Excel diverge on "e": Excel only accepts E.