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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(text): update (#6140)
Univer693e9d21d151
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.tsindex 38829b0f4c..e5b6744176 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@@ -54,6 +54,26 @@ describe('Test text function', () => {expect(getObjectValue(result)).toBe('0');});+ it('Text is string format dddd (align with current year)', () => {+ // Current year+ const year = new Date().getFullYear();++ // September 6th of the current year (JS months are 0-based, so use 8)+ const jsDate = new Date(year, 8, 6);++ // Compute the expected weekday in English using JS+ const expectedWeekday = jsDate.toLocaleDateString('en-US', {+ weekday: 'long',+ });++ const text1 = StringValueObject.create('06-Sep');+ const formatText = StringValueObject.create('dddd');++ const result = testFunction.calculate(text1, formatText);++ expect(getObjectValue(result)).toBe(expectedWeekday);+ });+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]]),diff --git a/packages/engine-formula/src/functions/text/text/index.ts b/packages/engine-formula/src/functions/text/text/index.tsindex 7452dcdab4..9caf280973 100644--- a/packages/engine-formula/src/functions/text/text/index.ts+++ b/packages/engine-formula/src/functions/text/text/index.ts@@ -16,7 +16,7 @@import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';import type { BaseValueObject } from '../../../engine/value-object/base-value-object';-import { isRealNum } from '@univerjs/core';+import { getNumfmtParseValueFilter, isRealNum } from '@univerjs/core';import { ErrorType } from '../../../basics/error-type';import { getFormatPreview } from '../../../basics/format';import { expandArrayValueObject } from '../../../engine/utils/array-object';@@ -82,8 +82,18 @@ export class Text extends BaseFunction {textValueNumber = 0;}- if (textValue.isString() && isRealNum(textValueNumber)) {- textValueNumber = Number(textValueNumber);+ if (textValue.isString()) {+ if (isRealNum(textValueNumber)) {+ textValueNumber = Number(textValueNumber);+ } else {+ // The text parameter needs to check if it can be converted to a number format. If convertible, format it as the converted number.+ // The format text does not need this check and is processed directly as a string.+ const parsedValue = getNumfmtParseValueFilter(`${textValueNumber}`);++ if (parsedValue && parsedValue.v != null && typeof parsedValue.v === 'number') {+ textValueNumber = parsedValue.v;+ }+ }}if (formatTextValue.isNull()) {