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 text formula calculation (#6101)

wpxp12345606693cad3050
diff --git a/packages/engine-formula/src/functions/text/text/__test__/index.spec.ts b/packages/engine-formula/src/functions/text/text/__test__/index.spec.ts
index 6bd71364b2..38829b0f4c 100644
--- a/packages/engine-formula/src/functions/text/text/__test__/index.spec.ts
+++ b/packages/engine-formula/src/functions/text/text/__test__/index.spec.ts
@@ -40,6 +40,20 @@ describe('Test text function', () => {
expect(getObjectValue(result)).toBe('0001年23月45日');
});
+ it('Text is empty string', () => {
+ const text1 = StringValueObject.create('');
+ const formatText = StringValueObject.create('0;;;\\0');
+ const result = testFunction.calculate(text1, formatText);
+ expect(getObjectValue(result)).toBe('0');
+ });
+
+ it('Text is string a', () => {
+ const text1 = StringValueObject.create('a');
+ const formatText = StringValueObject.create('0;;;\\0');
+ const result = testFunction.calculate(text1, formatText);
+ expect(getObjectValue(result)).toBe('0');
+ });
+
it('Text is array, format text is array', () => {
const text1 = new ArrayValueObject({
calculateValueList: transformToValueObject([[1, ' ', 1.23, true, false, null, 0, '100', '2.34', 'test', -3, ErrorType.NAME]]),
@@ -77,7 +91,7 @@ describe('Test text function', () => {
['1900-01-01', ' ', '1900-01-01', true, false, '1900-01-00', '1900-01-00', '1900-04-09', '1900-01-02', 'test', '1899-12-27', ErrorType.NAME],
[ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.NAME],
[ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.VALUE, ErrorType.NAME],
- ['', ' ', '', true, false, '', '', '', '', 'test', '-', ErrorType.NAME],
+ ['', '', '', true, false, '', '', '', '', 'test', '-', ErrorType.NAME],
['1', ' ', '1', true, false, '0', '0', '100', '2', 'test', '-3', ErrorType.NAME],
['101', ' ', '101', true, false, '100', '100', '1100', '102', 'test', '-103', ErrorType.NAME],
['t19000t', ' ', 't190012t', true, false, 't19000t', 't19000t', 't19000t', 't190036t', 'test', 't18990t', ErrorType.NAME],
diff --git a/packages/engine-formula/src/functions/text/text/index.ts b/packages/engine-formula/src/functions/text/text/index.ts
index 50fa934193..7452dcdab4 100644
--- a/packages/engine-formula/src/functions/text/text/index.ts
+++ b/packages/engine-formula/src/functions/text/text/index.ts
@@ -82,11 +82,7 @@ export class Text extends BaseFunction {
textValueNumber = 0;
}
- if (textValue.isString()) {
- if (!isRealNum(textValueNumber)) {
- return textValue;
- }
-
+ if (textValue.isString() && isRealNum(textValueNumber)) {
textValueNumber = Number(textValueNumber);
}