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(formula): fix some bugs (#3470)
wpxp1234567c66482f69b3
diff --git a/packages/engine-formula/src/functions/text/textafter/__test__/index.spec.ts b/packages/engine-formula/src/functions/text/textafter/__test__/index.spec.tsindex 42160e3e94..b386ec0749 100644--- a/packages/engine-formula/src/functions/text/textafter/__test__/index.spec.ts+++ b/packages/engine-formula/src/functions/text/textafter/__test__/index.spec.ts@@ -16,11 +16,13 @@import { describe, expect, it } from 'vitest';+import { ErrorType } from '../../../../basics/error-type';+import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';+import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';+import { NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';+import { getObjectValue } from '../../../__tests__/create-function-test-bed';import { FUNCTION_NAMES_TEXT } from '../../function-names';import { Textafter } from '../index';-import { NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';-import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';-import { ErrorType } from '../../../../basics/error-type';describe('Test textafter function', () => {const testFunction = new Textafter(FUNCTION_NAMES_TEXT.TEXTAFTER);@@ -30,7 +32,7 @@ describe('Test textafter function', () => {const text = StringValueObject.create('***TRUETRUEFALSE');const delimiter = StringValueObject.create('T');const result = testFunction.calculate(text, delimiter);- expect(result.getValue()).toBe('RUETRUEFALSE');+ expect(getObjectValue(result)).toBe('RUETRUEFALSE');});it('delimiter value is empty string', () => {@@ -38,11 +40,19 @@ describe('Test textafter function', () => {const delimiter = StringValueObject.create('');const instanceNum = NumberValueObject.create(1);const result = testFunction.calculate(text, delimiter, instanceNum);- expect(result.getValue()).toBe('***TRUETRUEFALSE');+ expect(getObjectValue(result)).toBe('***TRUETRUEFALSE');const instanceNum2 = NumberValueObject.create(-1);const result2 = testFunction.calculate(text, delimiter, instanceNum2);- expect(result2.getValue()).toBe('');+ expect(getObjectValue(result2)).toBe('');++ const delimiter2 = ErrorValueObject.create(ErrorType.NAME);+ const result3 = testFunction.calculate(text, delimiter2, instanceNum);+ expect(getObjectValue(result3)).toBe(ErrorType.NAME);++ const delimiter3 = StringValueObject.create('TTTTTTTTTTTTTTTTTTTTT');+ const result4 = testFunction.calculate(text, delimiter3, instanceNum);+ expect(getObjectValue(result4)).toBe(ErrorType.NA);});it('instanceNum value test', () => {@@ -50,19 +60,31 @@ describe('Test textafter function', () => {const delimiter = StringValueObject.create('T');const instanceNum = NumberValueObject.create(1);const result = testFunction.calculate(text, delimiter, instanceNum);- expect(result.getValue()).toBe('RUETRUEFALSE');+ expect(getObjectValue(result)).toBe('RUETRUEFALSE');const instanceNum2 = NumberValueObject.create(2);const result2 = testFunction.calculate(text, delimiter, instanceNum2);- expect(result2.getValue()).toBe('RUEFALSE');+ expect(getObjectValue(result2)).toBe('RUEFALSE');const instanceNum3 = NumberValueObject.create(3);const result3 = testFunction.calculate(text, delimiter, instanceNum3);- expect(result3.getValue()).toBe(ErrorType.NA);+ expect(getObjectValue(result3)).toBe(ErrorType.NA);const instanceNum4 = NumberValueObject.create(17);const result4 = testFunction.calculate(text, delimiter, instanceNum4);- expect(result4.getValue()).toBe(ErrorType.VALUE);+ expect(getObjectValue(result4)).toBe(ErrorType.VALUE);++ const instanceNum5 = ErrorValueObject.create(ErrorType.NAME);+ const result5 = testFunction.calculate(text, delimiter, instanceNum5);+ expect(getObjectValue(result5)).toBe(ErrorType.NAME);++ const instanceNum6 = StringValueObject.create('test');+ const result6 = testFunction.calculate(text, delimiter, instanceNum6);+ expect(getObjectValue(result6)).toBe(ErrorType.VALUE);++ const instanceNum7 = NullValueObject.create();+ const result7 = testFunction.calculate(text, delimiter, instanceNum7);+ expect(getObjectValue(result7)).toBe('RUETRUEFALSE');});it('matchMode value 0 or 1', () => {@@ -71,15 +93,15 @@ describe('Test textafter function', () => {const instanceNum = NumberValueObject.create(1);const matchMode = NumberValueObject.create(-1);const result = testFunction.calculate(text, delimiter, instanceNum, matchMode);- expect(result.getValue()).toBe(ErrorType.VALUE);+ expect(getObjectValue(result)).toBe(ErrorType.VALUE);const matchMode2 = NumberValueObject.create(0);const result2 = testFunction.calculate(text, delimiter, instanceNum, matchMode2);- expect(result2.getValue()).toBe(ErrorType.NA);+ expect(getObjectValue(result2)).toBe(ErrorType.NA);const matchMode3 = NumberValueObject.create(1);const result3 = testFunction.calculate(text, delimiter, instanceNum, matchMode3);- expect(result3.getValue()).toBe('RUETRUEFALSE');+ expect(getObjectValue(result3)).toBe('RUETRUEFALSE');});it('matchEnd value 0 or 1', () => {@@ -89,15 +111,15 @@ describe('Test textafter function', () => {const matchMode = NumberValueObject.create(1);const matchEnd = NumberValueObject.create(0);const result = testFunction.calculate(text, delimiter, instanceNum, matchMode, matchEnd);- expect(result.getValue()).toBe(ErrorType.NA);+ expect(getObjectValue(result)).toBe(ErrorType.NA);const matchEnd2 = NumberValueObject.create(1);const result2 = testFunction.calculate(text, delimiter, instanceNum, matchMode, matchEnd2);- expect(result2.getValue()).toBe('');+ expect(getObjectValue(result2)).toBe('');const instanceNum3 = NumberValueObject.create(-3);const result3 = testFunction.calculate(text, delimiter, instanceNum3, matchMode, matchEnd2);- expect(result3.getValue()).toBe('***TRUETRUEFALSE');+ expect(getObjectValue(result3)).toBe('***TRUETRUEFALSE');});it('ifNotFound value test', () => {@@ -108,7 +130,7 @@ describe('Test textafter function', () => {const matchEnd = NumberValueObject.create(0);const ifNotFound = StringValueObject.create('not found');const result = testFunction.calculate(text, delimiter, instanceNum, matchMode, matchEnd, ifNotFound);- expect(result.getValue()).toBe('not found');+ expect(getObjectValue(result)).toBe('not found');});it('Value is array', () => {@@ -140,10 +162,84 @@ describe('Test textafter function', () => {const matchEnd = NumberValueObject.create(0);const ifNotFound = StringValueObject.create('not found');const result = testFunction.calculate(text, delimiter, instanceNum, matchMode, matchEnd, ifNotFound);- expect(transformToValue(result.getArrayValue())).toStrictEqual([+ expect(getObjectValue(result)).toStrictEqual([['not found', 'not found', 'not found', ErrorType.VALUE, 'not found'],['not found', 'RUE', 'not found', ErrorType.VALUE, 'RUETRUEFALSE'],]);});++ it('More test', () => {+ let text = StringValueObject.create('150克*8袋');+ let delimiter = StringValueObject.create('*');+ let result = testFunction.calculate(text, delimiter);+ expect(getObjectValue(result)).toBe('8袋');++ text = StringValueObject.create('150克#8袋');+ delimiter = StringValueObject.create('#');+ result = testFunction.calculate(text, delimiter);+ expect(getObjectValue(result)).toBe('8袋');++ text = StringValueObject.create('150克&8袋');+ delimiter = StringValueObject.create('&');+ result = testFunction.calculate(text, delimiter);+ expect(getObjectValue(result)).toBe('8袋');++ text = StringValueObject.create('150克@8袋');+ delimiter = StringValueObject.create('@');+ result = testFunction.calculate(text, delimiter);+ expect(getObjectValue(result)).toBe('8袋');++ text = StringValueObject.create('150克.8袋');+ delimiter = StringValueObject.create('.');+ result = testFunction.calculate(text, delimiter);+ expect(getObjectValue(result)).toBe('8袋');++ const text2 = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ ['150克*@*8袋'],+ ['150克@*8袋'],+ ['150克#8袋'],+ ]),+ rowCount: 3,+ columnCount: 1,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ const delimiter2 = ArrayValueObject.create('{"*","@","#"}');+ result = testFunction.calculate(text2, delimiter2);+ expect(getObjectValue(result)).toStrictEqual([+ ['@*8袋'],+ ['*8袋'],+ ['8袋'],+ ]);++ const instanceNum = NumberValueObject.create(-1);+ result = testFunction.calculate(text2, delimiter2, instanceNum);+ expect(getObjectValue(result)).toStrictEqual([+ ['8袋'],+ ['8袋'],+ ['8袋'],+ ]);++ const delimiter3 = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ [null, '袋', '粒', '瓶', ErrorType.NAME],+ ]),+ rowCount: 1,+ columnCount: 5,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ result = testFunction.calculate(text2, delimiter3);+ expect(getObjectValue(result)).toStrictEqual([+ [ErrorType.NAME],+ [ErrorType.NAME],+ [ErrorType.NAME],+ ]);+ });});});diff --git a/packages/engine-formula/src/functions/text/textafter/index.ts b/packages/engine-formula/src/functions/text/textafter/index.tsindex 449f141f77..13968e93ba 100644--- a/packages/engine-formula/src/functions/text/textafter/index.ts+++ b/packages/engine-formula/src/functions/text/textafter/index.ts@@ -16,10 +16,10 @@import { ErrorType } from '../../../basics/error-type';import { expandArrayValueObject } from '../../../engine/utils/array-object';-import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';import { BooleanValueObject, NumberValueObject, StringValueObject } from '../../../engine/value-object/primitive-object';import { BaseFunction } from '../../base-function';+import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';export class Textafter extends BaseFunction {override minParams = 2;@@ -27,12 +27,6 @@ export class Textafter extends BaseFunction {override maxParams = 6;override calculate(text: BaseValueObject, delimiter: BaseValueObject, instanceNum?: BaseValueObject, matchMode?: BaseValueObject, matchEnd?: BaseValueObject, ifNotFound?: BaseValueObject) {- let _delimiter = delimiter;-- if (_delimiter.isArray()) {- _delimiter = (_delimiter as ArrayValueObject).get(0, 0) as BaseValueObject;- }-let instanceNumIsNull = false; // special handlelet _instanceNum = instanceNum ?? NumberValueObject.create(1);@@ -68,7 +62,7 @@ export class Textafter extends BaseFunction {const matchEndArray = expandArrayValueObject(maxRowLength, maxColumnLength, _matchEnd, ErrorValueObject.create(ErrorType.NA));const ifNotFoundArray = expandArrayValueObject(maxRowLength, maxColumnLength, _ifNotFound, ErrorValueObject.create(ErrorType.NA));- const resultArray = this._getResultArray(textArray, _delimiter, instanceNumArray, matchModeArray, matchEndArray, ifNotFoundArray, instanceNumIsNull, onlyThreeVariant);+ const resultArray = this._getResultArray(textArray, delimiter, instanceNumArray, matchModeArray, matchEndArray, ifNotFoundArray, instanceNumIsNull, onlyThreeVariant);if (maxRowLength === 1 && maxColumnLength === 1) {return (resultArray as ArrayValueObject).get(0, 0) as StringValueObject;@@ -79,7 +73,7 @@ export class Textafter extends BaseFunction {private _getResultArray(textArray: ArrayValueObject,- delimiterObject: BaseValueObject,+ delimiter: BaseValueObject,instanceNumArray: ArrayValueObject,matchModeArray: ArrayValueObject,matchEndArray: ArrayValueObject,@@ -94,14 +88,19 @@ export class Textafter extends BaseFunction {const ifNotFoundObject = ifNotFoundArray.get(rowIndex, columnIndex) as BaseValueObject;// variant error order (text > instanceNum > matchMode > matchEnd > delimiter)- const _variantsError = this._checkVariantsError(textObject, instanceNumObject, matchModeObject, matchEndObject, delimiterObject);+ const _variantsError = this._checkVariantsError(textObject, instanceNumObject, matchModeObject, matchEndObject);if (_variantsError.isError()) {return _variantsError;}const textValue = this._getStringValue(textObject);- const delimiterValue = this._getStringValue(delimiterObject);++ const delimiterValue = this._getDelimiterValue(delimiter);++ if (delimiterValue instanceof ErrorValueObject) {+ return delimiterValue;+ }const _variantsNumberFloorValue = this._getVariantsNumberFloorValue(instanceNumObject, matchModeObject, matchEndObject);@@ -117,7 +116,7 @@ export class Textafter extends BaseFunction {// When searching with an empty delimiter value, TEXTAFTER matches immediately.// It returns the entire text when searching from the front (if instance_num is positive) and empty text when searching from the end (if instance_num is negative).- if (delimiterValue === '') {+ if (delimiterValue.includes('')) {if (instanceNumValue > 0) {return StringValueObject.create(textValue);} else {@@ -130,7 +129,7 @@ export class Textafter extends BaseFunction {return ErrorValueObject.create(ErrorType.VALUE);}- if (delimiterValue.length > textValue.length) {+ if (delimiterValue.every((item) => item.length > textValue.length)) {return ErrorValueObject.create(ErrorType.NA);}@@ -166,6 +165,39 @@ export class Textafter extends BaseFunction {return value;}+ private _getDelimiterValue(delimiter: BaseValueObject): string[] | ErrorValueObject {+ const delimiterValue: string[] = [];++ if (delimiter.isArray()) {+ let isError = false;+ let errorObject = ErrorValueObject.create(ErrorType.VALUE);++ (delimiter as ArrayValueObject).iterator((delimiterObject) => {+ const _delimiterObject = delimiterObject as BaseValueObject;++ if (_delimiterObject.isError()) {+ isError = true;+ errorObject = _delimiterObject as ErrorValueObject;+ return false;+ }++ delimiterValue.push(this._getStringValue(_delimiterObject));+ });++ if (isError) {+ return errorObject;+ }+ } else {+ if (delimiter.isError()) {+ return delimiter as ErrorValueObject;+ }++ delimiterValue.push(this._getStringValue(delimiter));+ }++ return delimiterValue;+ }+private _getVariantsNumberFloorValue(...variants: BaseValueObject[]) {const values: number[] = [];@@ -190,21 +222,62 @@ export class Textafter extends BaseFunction {private _getResult(textValue: string,- delimiterValue: string,+ delimiterValue: string[],instanceNumValue: number,matchModeValue: number,matchEndValue: number,ifNotFoundObject: BaseValueObject,onlyThreeVariant: boolean): BaseValueObject {- const matchNum = textValue.match(new RegExp(delimiterValue, `g${!matchModeValue ? '' : 'i'}`));+ let substrText = !matchModeValue ? textValue : textValue.toLocaleLowerCase();+ const _delimiterValue = !matchModeValue ? delimiterValue : delimiterValue.map((item) => item.toLocaleLowerCase());++ let resultIndex = 0;+ let matchNum = 0;+ let preDelimiterLength = 0;++ for (let i = 0; i < Math.abs(instanceNumValue); i++) {+ if (instanceNumValue < 0) {+ const delimiterItem = _delimiterValue.map((item) => {+ return {+ index: substrText.lastIndexOf(item),+ length: item.length,+ };+ }).filter((item) => item.index !== -1).sort((a, b) => b.index - a.index)[0];++ if (!delimiterItem) {+ break;+ }++ resultIndex = delimiterItem.index;+ substrText = substrText.substr(0, delimiterItem.index);+ preDelimiterLength = delimiterItem.length;+ matchNum++;+ } else {+ const delimiterItem = _delimiterValue.map((item) => {+ return {+ index: substrText.indexOf(item),+ length: item.length,+ };+ }).filter((item) => item.index !== -1).sort((a, b) => a.index - b.index)[0];++ if (!delimiterItem) {+ break;+ }++ resultIndex += delimiterItem.index + preDelimiterLength;+ substrText = substrText.substr(delimiterItem.index + delimiterItem.length);+ preDelimiterLength = delimiterItem.length;+ matchNum++;+ }+ }// only three variant and if instance_num is greater than the number of occurrences of delimiter. returns a #N/A error- if (matchNum && matchNum.length < Math.abs(instanceNumValue) && onlyThreeVariant) {+ if (matchNum && matchNum < Math.abs(instanceNumValue) && onlyThreeVariant) {return ErrorValueObject.create(ErrorType.NA);}- if (!matchNum || matchNum.length < Math.abs(instanceNumValue)) {+ if (!matchNum || matchNum < Math.abs(instanceNumValue)) {if (matchEndValue) {if (instanceNumValue > 0) {return StringValueObject.create('');@@ -216,24 +289,7 @@ export class Textafter extends BaseFunction {return ifNotFoundObject;}- let substrText = !matchModeValue ? textValue : textValue.toLocaleLowerCase();- const _delimiterValue = !matchModeValue ? delimiterValue : delimiterValue.toLocaleLowerCase();-- let resultIndex = 0;-- for (let i = 0; i < Math.abs(instanceNumValue); i++) {- if (instanceNumValue < 0) {- const index = substrText.lastIndexOf(_delimiterValue);- resultIndex = index;- substrText = substrText.substr(0, index);- } else {- const index = substrText.indexOf(_delimiterValue);- resultIndex += (index + i * _delimiterValue.length);- substrText = substrText.substr(index + _delimiterValue.length);- }- }-- const result = textValue.substr(resultIndex + _delimiterValue.length);+ const result = textValue.substr(resultIndex + preDelimiterLength);return StringValueObject.create(result);}diff --git a/packages/engine-formula/src/functions/text/textbefore/__test__/index.spec.ts b/packages/engine-formula/src/functions/text/textbefore/__test__/index.spec.tsindex dcb8818e74..c360a99478 100644--- a/packages/engine-formula/src/functions/text/textbefore/__test__/index.spec.ts+++ b/packages/engine-formula/src/functions/text/textbefore/__test__/index.spec.ts@@ -16,11 +16,13 @@import { describe, expect, it } from 'vitest';+import { ErrorType } from '../../../../basics/error-type';+import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';+import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';+import { NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';+import { getObjectValue } from '../../../__tests__/create-function-test-bed';import { FUNCTION_NAMES_TEXT } from '../../function-names';import { Textbefore } from '../index';-import { NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';-import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';-import { ErrorType } from '../../../../basics/error-type';describe('Test textbefore function', () => {const testFunction = new Textbefore(FUNCTION_NAMES_TEXT.TEXTBEFORE);@@ -30,7 +32,7 @@ describe('Test textbefore function', () => {const text = StringValueObject.create('***TRUETRUEFALSE');const delimiter = StringValueObject.create('T');const result = testFunction.calculate(text, delimiter);- expect(result.getValue()).toBe('***');+ expect(getObjectValue(result)).toBe('***');});it('delimiter value is empty string', () => {@@ -38,11 +40,19 @@ describe('Test textbefore function', () => {const delimiter = StringValueObject.create('');const instanceNum = NumberValueObject.create(1);const result = testFunction.calculate(text, delimiter, instanceNum);- expect(result.getValue()).toBe('');+ expect(getObjectValue(result)).toBe('');const instanceNum2 = NumberValueObject.create(-1);const result2 = testFunction.calculate(text, delimiter, instanceNum2);- expect(result2.getValue()).toBe('***TRUETRUEFALSE');+ expect(getObjectValue(result2)).toBe('***TRUETRUEFALSE');++ const delimiter2 = ErrorValueObject.create(ErrorType.NAME);+ const result3 = testFunction.calculate(text, delimiter2, instanceNum);+ expect(getObjectValue(result3)).toBe(ErrorType.NAME);++ const delimiter3 = StringValueObject.create('TTTTTTTTTTTTTTTTTTTTT');+ const result4 = testFunction.calculate(text, delimiter3, instanceNum);+ expect(getObjectValue(result4)).toBe(ErrorType.NA);});it('instanceNum value test', () => {@@ -50,19 +60,31 @@ describe('Test textbefore function', () => {const delimiter = StringValueObject.create('T');const instanceNum = NumberValueObject.create(1);const result = testFunction.calculate(text, delimiter, instanceNum);- expect(result.getValue()).toBe('***');+ expect(getObjectValue(result)).toBe('***');const instanceNum2 = NumberValueObject.create(2);const result2 = testFunction.calculate(text, delimiter, instanceNum2);- expect(result2.getValue()).toBe('***TRUE');+ expect(getObjectValue(result2)).toBe('***TRUE');const instanceNum3 = NumberValueObject.create(3);const result3 = testFunction.calculate(text, delimiter, instanceNum3);- expect(result3.getValue()).toBe(ErrorType.NA);+ expect(getObjectValue(result3)).toBe(ErrorType.NA);const instanceNum4 = NumberValueObject.create(17);const result4 = testFunction.calculate(text, delimiter, instanceNum4);- expect(result4.getValue()).toBe(ErrorType.VALUE);+ expect(getObjectValue(result4)).toBe(ErrorType.VALUE);++ const instanceNum5 = ErrorValueObject.create(ErrorType.NAME);+ const result5 = testFunction.calculate(text, delimiter, instanceNum5);+ expect(getObjectValue(result5)).toBe(ErrorType.NAME);++ const instanceNum6 = StringValueObject.create('test');+ const result6 = testFunction.calculate(text, delimiter, instanceNum6);+ expect(getObjectValue(result6)).toBe(ErrorType.VALUE);++ const instanceNum7 = NullValueObject.create();+ const result7 = testFunction.calculate(text, delimiter, instanceNum7);+ expect(getObjectValue(result7)).toBe('***');});it('matchMode value 0 or 1', () => {@@ -71,15 +93,15 @@ describe('Test textbefore function', () => {const instanceNum = NumberValueObject.create(1);const matchMode = NumberValueObject.create(-1);const result = testFunction.calculate(text, delimiter, instanceNum, matchMode);- expect(result.getValue()).toBe(ErrorType.VALUE);+ expect(getObjectValue(result)).toBe(ErrorType.VALUE);const matchMode2 = NumberValueObject.create(0);const result2 = testFunction.calculate(text, delimiter, instanceNum, matchMode2);- expect(result2.getValue()).toBe(ErrorType.NA);+ expect(getObjectValue(result2)).toBe(ErrorType.NA);const matchMode3 = NumberValueObject.create(1);const result3 = testFunction.calculate(text, delimiter, instanceNum, matchMode3);- expect(result3.getValue()).toBe('***');+ expect(getObjectValue(result3)).toBe('***');});it('matchEnd value 0 or 1', () => {@@ -89,15 +111,15 @@ describe('Test textbefore function', () => {const matchMode = NumberValueObject.create(1);const matchEnd = NumberValueObject.create(0);const result = testFunction.calculate(text, delimiter, instanceNum, matchMode, matchEnd);- expect(result.getValue()).toBe(ErrorType.NA);+ expect(getObjectValue(result)).toBe(ErrorType.NA);const matchEnd2 = NumberValueObject.create(1);const result2 = testFunction.calculate(text, delimiter, instanceNum, matchMode, matchEnd2);- expect(result2.getValue()).toBe('***TRUETRUEFALSE');+ expect(getObjectValue(result2)).toBe('***TRUETRUEFALSE');const instanceNum3 = NumberValueObject.create(-3);const result3 = testFunction.calculate(text, delimiter, instanceNum3, matchMode, matchEnd2);- expect(result3.getValue()).toBe('');+ expect(getObjectValue(result3)).toBe('');});it('ifNotFound value test', () => {@@ -108,7 +130,7 @@ describe('Test textbefore function', () => {const matchEnd = NumberValueObject.create(0);const ifNotFound = StringValueObject.create('not found');const result = testFunction.calculate(text, delimiter, instanceNum, matchMode, matchEnd, ifNotFound);- expect(result.getValue()).toBe('not found');+ expect(getObjectValue(result)).toBe('not found');});it('Value is array', () => {@@ -140,10 +162,84 @@ describe('Test textbefore function', () => {const matchEnd = NumberValueObject.create(0);const ifNotFound = StringValueObject.create('not found');const result = testFunction.calculate(text, delimiter, instanceNum, matchMode, matchEnd, ifNotFound);- expect(transformToValue(result.getArrayValue())).toStrictEqual([+ expect(getObjectValue(result)).toStrictEqual([['not found', 'not found', 'not found', ErrorType.VALUE, 'not found'],['not found', '', 'not found', ErrorType.VALUE, '***'],]);});++ it('More test', () => {+ let text = StringValueObject.create('150克*8袋');+ let delimiter = StringValueObject.create('*');+ let result = testFunction.calculate(text, delimiter);+ expect(getObjectValue(result)).toBe('150克');++ text = StringValueObject.create('150克#8袋');+ delimiter = StringValueObject.create('#');+ result = testFunction.calculate(text, delimiter);+ expect(getObjectValue(result)).toBe('150克');++ text = StringValueObject.create('150克&8袋');+ delimiter = StringValueObject.create('&');+ result = testFunction.calculate(text, delimiter);+ expect(getObjectValue(result)).toBe('150克');++ text = StringValueObject.create('150克@8袋');+ delimiter = StringValueObject.create('@');+ result = testFunction.calculate(text, delimiter);+ expect(getObjectValue(result)).toBe('150克');++ text = StringValueObject.create('150克.8袋');+ delimiter = StringValueObject.create('.');+ result = testFunction.calculate(text, delimiter);+ expect(getObjectValue(result)).toBe('150克');++ const text2 = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ ['150克*8袋粒袋'],+ ['150克*8粒袋'],+ ['150克*8瓶'],+ ]),+ rowCount: 3,+ columnCount: 1,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ const delimiter2 = ArrayValueObject.create('{"袋","粒","瓶"}');+ result = testFunction.calculate(text2, delimiter2);+ expect(getObjectValue(result)).toStrictEqual([+ ['150克*8'],+ ['150克*8'],+ ['150克*8'],+ ]);++ const instanceNum = NumberValueObject.create(-1);+ result = testFunction.calculate(text2, delimiter2, instanceNum);+ expect(getObjectValue(result)).toStrictEqual([+ ['150克*8袋粒'],+ ['150克*8粒'],+ ['150克*8'],+ ]);++ const delimiter3 = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ [null, '袋', '粒', '瓶', ErrorType.NAME],+ ]),+ rowCount: 1,+ columnCount: 5,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ result = testFunction.calculate(text2, delimiter3);+ expect(getObjectValue(result)).toStrictEqual([+ [ErrorType.NAME],+ [ErrorType.NAME],+ [ErrorType.NAME],+ ]);+ });});});diff --git a/packages/engine-formula/src/functions/text/textbefore/index.ts b/packages/engine-formula/src/functions/text/textbefore/index.tsindex e34b4e8d86..1cffbcf636 100644--- a/packages/engine-formula/src/functions/text/textbefore/index.ts+++ b/packages/engine-formula/src/functions/text/textbefore/index.ts@@ -16,10 +16,10 @@import { ErrorType } from '../../../basics/error-type';import { expandArrayValueObject } from '../../../engine/utils/array-object';-import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';import { BooleanValueObject, NumberValueObject, StringValueObject } from '../../../engine/value-object/primitive-object';import { BaseFunction } from '../../base-function';+import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';export class Textbefore extends BaseFunction {override minParams = 2;@@ -27,12 +27,6 @@ export class Textbefore extends BaseFunction {override maxParams = 6;override calculate(text: BaseValueObject, delimiter: BaseValueObject, instanceNum?: BaseValueObject, matchMode?: BaseValueObject, matchEnd?: BaseValueObject, ifNotFound?: BaseValueObject) {- let _delimiter = delimiter;-- if (_delimiter.isArray()) {- _delimiter = (_delimiter as ArrayValueObject).get(0, 0) as BaseValueObject;- }-let instanceNumIsNull = false; // special handlelet _instanceNum = instanceNum ?? NumberValueObject.create(1);@@ -68,7 +62,7 @@ export class Textbefore extends BaseFunction {const matchEndArray = expandArrayValueObject(maxRowLength, maxColumnLength, _matchEnd, ErrorValueObject.create(ErrorType.NA));const ifNotFoundArray = expandArrayValueObject(maxRowLength, maxColumnLength, _ifNotFound, ErrorValueObject.create(ErrorType.NA));- const resultArray = this._getResultArray(textArray, _delimiter, instanceNumArray, matchModeArray, matchEndArray, ifNotFoundArray, instanceNumIsNull, onlyThreeVariant);+ const resultArray = this._getResultArray(textArray, delimiter, instanceNumArray, matchModeArray, matchEndArray, ifNotFoundArray, instanceNumIsNull, onlyThreeVariant);if (maxRowLength === 1 && maxColumnLength === 1) {return (resultArray as ArrayValueObject).get(0, 0) as StringValueObject;@@ -79,7 +73,7 @@ export class Textbefore extends BaseFunction {private _getResultArray(textArray: ArrayValueObject,- delimiterObject: BaseValueObject,+ delimiter: BaseValueObject,instanceNumArray: ArrayValueObject,matchModeArray: ArrayValueObject,matchEndArray: ArrayValueObject,@@ -94,14 +88,19 @@ export class Textbefore extends BaseFunction {const ifNotFoundObject = ifNotFoundArray.get(rowIndex, columnIndex) as BaseValueObject;// variant error order (text > instanceNum > matchMode > matchEnd > delimiter)- const _variantsError = this._checkVariantsError(textObject, instanceNumObject, matchModeObject, matchEndObject, delimiterObject);+ const _variantsError = this._checkVariantsError(textObject, instanceNumObject, matchModeObject, matchEndObject);if (_variantsError.isError()) {return _variantsError;}const textValue = this._getStringValue(textObject);- const delimiterValue = this._getStringValue(delimiterObject);++ const delimiterValue = this._getDelimiterValue(delimiter);++ if (delimiterValue instanceof ErrorValueObject) {+ return delimiterValue;+ }const _variantsNumberFloorValue = this._getVariantsNumberFloorValue(instanceNumObject, matchModeObject, matchEndObject);@@ -117,7 +116,7 @@ export class Textbefore extends BaseFunction {// When searching with an empty delimiter value, TEXTBEFORE matches immediately.// It returns empty text when searching from the front (if instance_num is positive) and the entire text when searching from the end (if instance_num is negative).- if (delimiterValue === '') {+ if (delimiterValue.includes('')) {if (instanceNumValue > 0) {return StringValueObject.create('');} else {@@ -130,7 +129,7 @@ export class Textbefore extends BaseFunction {return ErrorValueObject.create(ErrorType.VALUE);}- if (delimiterValue.length > textValue.length) {+ if (delimiterValue.every((item) => item.length > textValue.length)) {return ErrorValueObject.create(ErrorType.NA);}@@ -166,6 +165,39 @@ export class Textbefore extends BaseFunction {return value;}+ private _getDelimiterValue(delimiter: BaseValueObject): string[] | ErrorValueObject {+ const delimiterValue: string[] = [];++ if (delimiter.isArray()) {+ let isError = false;+ let errorObject = ErrorValueObject.create(ErrorType.VALUE);++ (delimiter as ArrayValueObject).iterator((delimiterObject) => {+ const _delimiterObject = delimiterObject as BaseValueObject;++ if (_delimiterObject.isError()) {+ isError = true;+ errorObject = _delimiterObject as ErrorValueObject;+ return false;+ }++ delimiterValue.push(this._getStringValue(_delimiterObject));+ });++ if (isError) {+ return errorObject;+ }+ } else {+ if (delimiter.isError()) {+ return delimiter as ErrorValueObject;+ }++ delimiterValue.push(this._getStringValue(delimiter));+ }++ return delimiterValue;+ }+private _getVariantsNumberFloorValue(...variants: BaseValueObject[]) {const values: number[] = [];@@ -190,21 +222,61 @@ export class Textbefore extends BaseFunction {private _getResult(textValue: string,- delimiterValue: string,+ delimiterValue: string[],instanceNumValue: number,matchModeValue: number,matchEndValue: number,ifNotFoundObject: BaseValueObject,onlyThreeVariant: boolean): BaseValueObject {- const matchNum = textValue.match(new RegExp(delimiterValue, `g${!matchModeValue ? '' : 'i'}`));+ let substrText = !matchModeValue ? textValue : textValue.toLocaleLowerCase();+ const _delimiterValue = !matchModeValue ? delimiterValue : delimiterValue.map((item) => item.toLocaleLowerCase());- // only three variant and if instance_num is greater than the number of occurrences of delimiter. returns a #N/A error- if (matchNum && matchNum.length < Math.abs(instanceNumValue) && onlyThreeVariant) {+ let resultIndex = 0;+ let matchNum = 0;+ let preDelimiterLength = 0;++ for (let i = 0; i < Math.abs(instanceNumValue); i++) {+ if (instanceNumValue < 0) {+ const delimiterItem = _delimiterValue.map((item) => {+ return {+ index: substrText.lastIndexOf(item),+ length: item.length,+ };+ }).filter((item) => item.index !== -1).sort((a, b) => b.index - a.index)[0];++ if (!delimiterItem) {+ break;+ }++ resultIndex = delimiterItem.index;+ substrText = substrText.substr(0, delimiterItem.index);+ matchNum++;+ } else {+ const delimiterItem = _delimiterValue.map((item) => {+ return {+ index: substrText.indexOf(item),+ length: item.length,+ };+ }).filter((item) => item.index !== -1).sort((a, b) => a.index - b.index)[0];++ if (!delimiterItem) {+ break;+ }++ resultIndex += delimiterItem.index + preDelimiterLength;+ substrText = substrText.substr(delimiterItem.index + delimiterItem.length);+ preDelimiterLength = delimiterItem.length;+ matchNum++;+ }+ }++ // only three variant and if instance_num is greater than the number of occurrences of delimiter. returns a #N/A error+ if (matchNum && matchNum < Math.abs(instanceNumValue) && onlyThreeVariant) {return ErrorValueObject.create(ErrorType.NA);}- if (!matchNum || matchNum.length < Math.abs(instanceNumValue)) {+ if (!matchNum || matchNum < Math.abs(instanceNumValue)) {if (matchEndValue) {if (instanceNumValue > 0) {return StringValueObject.create(textValue);@@ -216,23 +288,6 @@ export class Textbefore extends BaseFunction {return ifNotFoundObject;}- let substrText = !matchModeValue ? textValue : textValue.toLocaleLowerCase();- const _delimiterValue = !matchModeValue ? delimiterValue : delimiterValue.toLocaleLowerCase();-- let resultIndex = 0;-- for (let i = 0; i < Math.abs(instanceNumValue); i++) {- if (instanceNumValue < 0) {- const index = substrText.lastIndexOf(_delimiterValue);- resultIndex = index;- substrText = substrText.substr(0, index);- } else {- const index = substrText.indexOf(_delimiterValue);- resultIndex += (index + i * _delimiterValue.length);- substrText = substrText.substr(index + _delimiterValue.length);- }- }-const result = textValue.substr(0, resultIndex);return StringValueObject.create(result);diff --git a/packages/engine-formula/src/functions/text/textsplit/__test__/index.spec.ts b/packages/engine-formula/src/functions/text/textsplit/__test__/index.spec.tsindex 7be3a718bb..88a93022d9 100644--- a/packages/engine-formula/src/functions/text/textsplit/__test__/index.spec.ts+++ b/packages/engine-formula/src/functions/text/textsplit/__test__/index.spec.ts@@ -16,12 +16,13 @@import { describe, expect, it } from 'vitest';-import { FUNCTION_NAMES_TEXT } from '../../function-names';-import { Textsplit } from '../index';-import { BooleanValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';-import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';import { ErrorType } from '../../../../basics/error-type';+import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';+import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';+import { getObjectValue } from '../../../__tests__/create-function-test-bed';+import { FUNCTION_NAMES_TEXT } from '../../function-names';+import { Textsplit } from '../index';describe('Test textsplit function', () => {const testFunction = new Textsplit(FUNCTION_NAMES_TEXT.TEXTSPLIT);@@ -31,7 +32,7 @@ describe('Test textsplit function', () => {const text = StringValueObject.create('Dakota Lennon Sanchez');const colDelimiter = StringValueObject.create(' ');const result = testFunction.calculate(text, colDelimiter);- expect(transformToValue(result.getArrayValue())).toStrictEqual([+ expect(getObjectValue(result)).toStrictEqual([['Dakota', 'Lennon', 'Sanchez'],]);@@ -39,22 +40,75 @@ describe('Test textsplit function', () => {const colDelimiter2 = StringValueObject.create(',');const rowDelimiter2 = StringValueObject.create(';');const result2 = testFunction.calculate(text2, colDelimiter2, rowDelimiter2);- expect(transformToValue(result2.getArrayValue())).toStrictEqual([+ expect(getObjectValue(result2)).toStrictEqual([[1, 2, 3],[4, 5, 6],]);});- it('delimiter value is empty string', () => {+ it('delimiter value test', () => {const text = StringValueObject.create('To be or not to be');const colDelimiter = StringValueObject.create('');const result = testFunction.calculate(text, colDelimiter);- expect(result.getValue()).toBe(ErrorType.VALUE);+ expect(getObjectValue(result)).toBe(ErrorType.VALUE);const colDelimiter2 = StringValueObject.create(' ');const rowDelimiter2 = StringValueObject.create('');const result2 = testFunction.calculate(text, colDelimiter2, rowDelimiter2);- expect(result2.getValue()).toBe(ErrorType.VALUE);+ expect(getObjectValue(result2)).toBe(ErrorType.VALUE);++ const colDelimiter3 = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ [null, 'or', ErrorType.NAME],+ ]),+ rowCount: 1,+ columnCount: 3,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ const result3 = testFunction.calculate(text, colDelimiter3);+ expect(getObjectValue(result3)).toBe(ErrorType.VALUE);++ const colDelimiter4 = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ ['or', true, ErrorType.NAME, null],+ ]),+ rowCount: 1,+ columnCount: 4,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ const result4 = testFunction.calculate(text, colDelimiter4);+ expect(getObjectValue(result4)).toBe(ErrorType.NAME);++ const colDelimiter5 = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ ['', ErrorType.NAME, null],+ ]),+ rowCount: 1,+ columnCount: 3,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ const result5 = testFunction.calculate(text, colDelimiter5);+ expect(getObjectValue(result5)).toBe(ErrorType.VALUE);++ const colDelimiter6 = NullValueObject.create();+ const result6 = testFunction.calculate(text, colDelimiter6);+ expect(getObjectValue(result6)).toBe(ErrorType.VALUE);++ const colDelimiter7 = StringValueObject.create('or');+ const rowDelimiter7 = NullValueObject.create();+ const result7 = testFunction.calculate(text, colDelimiter7, rowDelimiter7);+ expect(getObjectValue(result7)).toStrictEqual([+ ['To be ', ' not to be'],+ ]);});it('ignoreEmpty value is true', () => {@@ -63,12 +117,16 @@ describe('Test textsplit function', () => {const rowDelimiter = StringValueObject.create('.');const ignoreEmpty = BooleanValueObject.create(true);const result = testFunction.calculate(text, colDelimiter, rowDelimiter, ignoreEmpty);- expect(transformToValue(result.getArrayValue())).toStrictEqual([+ expect(getObjectValue(result)).toStrictEqual([['Do', ErrorType.NA, ErrorType.NA, ErrorType.NA],['Or', 'do', 'not', ErrorType.NA],['There', 'is', 'no', 'try'],['-Anonymous', ErrorType.NA, ErrorType.NA, ErrorType.NA],]);++ const ignoreEmpty2 = StringValueObject.create('test');+ const result2 = testFunction.calculate(text, colDelimiter, rowDelimiter, ignoreEmpty2);+ expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);});it('matchMode value 0 or 1', () => {@@ -78,11 +136,11 @@ describe('Test textsplit function', () => {const ignoreEmpty = BooleanValueObject.create(true);const matchMode = NumberValueObject.create(-1);const result = testFunction.calculate(text, colDelimiter, rowDelimiter, ignoreEmpty, matchMode);- expect(result.getValue()).toBe(ErrorType.VALUE);+ expect(getObjectValue(result)).toBe(ErrorType.VALUE);const matchMode2 = NumberValueObject.create(0);const result2 = testFunction.calculate(text, colDelimiter, rowDelimiter, ignoreEmpty, matchMode2);- expect(transformToValue(result2.getArrayValue())).toStrictEqual([+ expect(getObjectValue(result2)).toStrictEqual([['D', '.', ErrorType.NA],['Or', ErrorType.NA, ErrorType.NA],['d', ErrorType.NA, ErrorType.NA],@@ -96,7 +154,7 @@ describe('Test textsplit function', () => {const matchMode3 = NumberValueObject.create(1);const result3 = testFunction.calculate(text, colDelimiter, rowDelimiter, ignoreEmpty, matchMode3);- expect(transformToValue(result3.getArrayValue())).toStrictEqual([+ expect(getObjectValue(result3)).toStrictEqual([['D', '.', ErrorType.NA],['r', ErrorType.NA, ErrorType.NA],['d', ErrorType.NA, ErrorType.NA],@@ -107,6 +165,10 @@ describe('Test textsplit function', () => {['ry.', ErrorType.NA, ErrorType.NA],['-An', 'nym', 'us'],]);++ const matchMode4 = StringValueObject.create('test');+ const result4 = testFunction.calculate(text, colDelimiter, rowDelimiter, ignoreEmpty, matchMode4);+ expect(getObjectValue(result4)).toStrictEqual(ErrorType.VALUE);});it('padWith value test', () => {@@ -117,7 +179,59 @@ describe('Test textsplit function', () => {const matchMode = NumberValueObject.create(0);const padWith = ErrorValueObject.create(ErrorType.NULL);const result = testFunction.calculate(text, colDelimiter, rowDelimiter, ignoreEmpty, matchMode, padWith);- expect(transformToValue(result.getArrayValue())).toStrictEqual([+ expect(getObjectValue(result)).toStrictEqual([+ ['D', '.', ErrorType.NULL],+ ['Or', ErrorType.NULL, ErrorType.NULL],+ ['d', ErrorType.NULL, ErrorType.NULL],+ ['n', '.', ErrorType.NULL],+ ['There', ErrorType.NULL, ErrorType.NULL],+ ['is', ErrorType.NULL, ErrorType.NULL],+ ['n', ErrorType.NULL, ErrorType.NULL],+ ['ry.', ErrorType.NULL, ErrorType.NULL],+ ['-An', 'nym', 'us'],+ ]);++ const padWith2 = BooleanValueObject.create(false);+ const result2 = testFunction.calculate(text, colDelimiter, rowDelimiter, ignoreEmpty, matchMode, padWith2);+ expect(getObjectValue(result2)).toStrictEqual([+ ['D', '.', false],+ ['Or', false, false],+ ['d', false, false],+ ['n', '.', false],+ ['There', false, false],+ ['is', false, false],+ ['n', false, false],+ ['ry.', false, false],+ ['-An', 'nym', 'us'],+ ]);++ const padWith3 = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ ['D', '.', false],+ ]),+ rowCount: 1,+ columnCount: 3,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ const result3 = testFunction.calculate(text, colDelimiter, rowDelimiter, ignoreEmpty, matchMode, padWith3);+ expect(getObjectValue(result3)).toStrictEqual(ErrorType.VALUE);++ const padWith4 = ArrayValueObject.create({+ calculateValueList: transformToValueObject([+ [ErrorType.NULL],+ ]),+ rowCount: 1,+ columnCount: 1,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ const result4 = testFunction.calculate(text, colDelimiter, rowDelimiter, ignoreEmpty, matchMode, padWith4);+ expect(getObjectValue(result4)).toStrictEqual([['D', '.', ErrorType.NULL],['Or', ErrorType.NULL, ErrorType.NULL],['d', ErrorType.NULL, ErrorType.NULL],@@ -133,11 +247,11 @@ describe('Test textsplit function', () => {it('Value is array', () => {const text = new ArrayValueObject({calculateValueList: transformToValueObject([- ['Dakota Lennon Sanchez 0'],- ['To be or not to be'],+ ['Dakota Lennon Sanchez 0', null, true],+ ['To be or not to be', false, ErrorType.NAME],]),rowCount: 2,- columnCount: 1,+ columnCount: 3,unitId: '',sheetId: '',row: 0,@@ -145,9 +259,46 @@ describe('Test textsplit function', () => {});const colDelimiter = StringValueObject.create(' ');const result = testFunction.calculate(text, colDelimiter);- expect(transformToValue(result.getArrayValue())).toStrictEqual([- ['Dakota'],- ['To'],+ expect(getObjectValue(result)).toStrictEqual([+ ['Dakota', ErrorType.VALUE, true],+ ['To', false, ErrorType.NAME],+ ]);+ });++ it('More test', () => {+ let text = StringValueObject.create('150克*8袋');+ let colDelimiter = StringValueObject.create('*');+ let result = testFunction.calculate(text, colDelimiter);+ expect(getObjectValue(result)).toStrictEqual([+ ['150克', '8袋'],+ ]);++ text = StringValueObject.create('150克#8袋');+ colDelimiter = StringValueObject.create('#');+ result = testFunction.calculate(text, colDelimiter);+ expect(getObjectValue(result)).toStrictEqual([+ ['150克', '8袋'],+ ]);++ text = StringValueObject.create('150克&8袋');+ colDelimiter = StringValueObject.create('&');+ result = testFunction.calculate(text, colDelimiter);+ expect(getObjectValue(result)).toStrictEqual([+ ['150克', '8袋'],+ ]);++ text = StringValueObject.create('150克@8袋');+ colDelimiter = StringValueObject.create('@');+ result = testFunction.calculate(text, colDelimiter);+ expect(getObjectValue(result)).toStrictEqual([+ ['150克', '8袋'],+ ]);++ text = StringValueObject.create('150克.8袋');+ colDelimiter = StringValueObject.create('.');+ result = testFunction.calculate(text, colDelimiter);+ expect(getObjectValue(result)).toStrictEqual([+ ['150克', '8袋'],]);});});diff --git a/packages/engine-formula/src/functions/text/textsplit/index.ts b/packages/engine-formula/src/functions/text/textsplit/index.tsindex 6d3d64e694..99b67425ac 100644--- a/packages/engine-formula/src/functions/text/textsplit/index.ts+++ b/packages/engine-formula/src/functions/text/textsplit/index.ts@@ -34,7 +34,7 @@ export class Textsplit extends BaseFunction {const { _variant: _colDelimiter, values: colDelimiterValue } = this._getStringValues(colDelimiter);- const { _variant, values: rowDelimiterValue } = this._getStringValues(_rowDelimiter);+ const { _variant, values: rowDelimiterValue } = this._getStringValues(_rowDelimiter, false);_rowDelimiter = _variant;const maxRowLength = Math.max(@@ -71,7 +71,7 @@ export class Textsplit extends BaseFunction {}}- private _getStringValues(variant: BaseValueObject) {+ private _getStringValues(variant: BaseValueObject, isNotNull = true) {let _variant = variant;const values: string[] = [];@@ -82,7 +82,7 @@ export class Textsplit extends BaseFunction {return false;}- if (variantObject?.isNull()) {+ if (variantObject?.isNull() && isNotNull) {_variant = ErrorValueObject.create(ErrorType.VALUE);return false;}@@ -97,6 +97,10 @@ export class Textsplit extends BaseFunction {values.push(value);});} else {+ if (_variant.isNull() && isNotNull) {+ _variant = ErrorValueObject.create(ErrorType.VALUE);+ }+const value = this._getRegExpStringValue(_variant);if (value === '') {diff --git a/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts b/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.tsindex c0b3d10349..83120ea7f1 100644--- a/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts+++ b/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts@@ -369,7 +369,7 @@ describe('Test insert function operation', () => {expect(await commandService.executeCommand(MoveRangeCommand.id, params)).toBeTruthy();const values = getValues(5, 2, 5, 2);- expect(values).toStrictEqual([[{ f: '=SUM(A1:B2)' }]]);+ expect(values).toStrictEqual([[{ f: '=SUM(D1:E2)' }]]);expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();const valuesUndo = getValues(5, 2, 5, 2);@@ -377,7 +377,7 @@ describe('Test insert function operation', () => {expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();const valuesRedo = getValues(5, 2, 5, 2);- expect(valuesRedo).toStrictEqual([[{ f: '=SUM(A1:B2)' }]]);+ expect(valuesRedo).toStrictEqual([[{ f: '=SUM(D1:E2)' }]]);});it('Move range, update position', async () => {diff --git a/packages/sheets-formula/src/controllers/formula-alert-render.controller.ts b/packages/sheets-formula/src/controllers/formula-alert-render.controller.tsindex 40bc1038ab..05d9321529 100644--- a/packages/sheets-formula/src/controllers/formula-alert-render.controller.ts+++ b/packages/sheets-formula/src/controllers/formula-alert-render.controller.ts@@ -14,10 +14,11 @@* limitations under the License.*/-import type { Workbook } from '@univerjs/core';import { Disposable, Inject, isICellData, LocaleService } from '@univerjs/core';import { ErrorType } from '@univerjs/engine-formula';import { CellAlertManagerService, CellAlertType, HoverManagerService } from '@univerjs/sheets-ui';+import { IZenZoneService } from '@univerjs/ui';+import type { Workbook } from '@univerjs/core';import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';import { extractFormulaError } from './utils/utils';@@ -43,7 +44,8 @@ export class FormulaAlertRenderController extends Disposable implements IRenderMprivate readonly _context: IRenderContext<Workbook>,@Inject(HoverManagerService) private readonly _hoverManagerService: HoverManagerService,@Inject(CellAlertManagerService) private readonly _cellAlertManagerService: CellAlertManagerService,- @Inject(LocaleService) private readonly _localeService: LocaleService+ @Inject(LocaleService) private readonly _localeService: LocaleService,+ @IZenZoneService private readonly _zenZoneService: IZenZoneService) {super();this._init();@@ -51,6 +53,7 @@ export class FormulaAlertRenderController extends Disposable implements IRenderMprivate _init() {this._initCellAlertPopup();+ this._initZenService();}private _initCellAlertPopup() {@@ -99,4 +102,12 @@ export class FormulaAlertRenderController extends Disposable implements IRenderMthis._cellAlertManagerService.removeAlert(ALERT_KEY);}));}++ private _initZenService() {+ this.disposeWithMe(this._zenZoneService.visible$.subscribe((visible) => {+ if (visible) {+ this._cellAlertManagerService.removeAlert(ALERT_KEY);+ }+ }));+ }}diff --git a/packages/sheets-formula/src/controllers/update-formula.controller.ts b/packages/sheets-formula/src/controllers/update-formula.controller.tsindex 9691ba6538..eaa7b1d8ee 100644--- a/packages/sheets-formula/src/controllers/update-formula.controller.ts+++ b/packages/sheets-formula/src/controllers/update-formula.controller.ts@@ -57,6 +57,7 @@ import {handleIRemoveCol,handleIRemoveRow,handleMoveCols,+ handleMoveRange,handleMoveRows,InsertColCommand,InsertRangeMoveDownCommand,@@ -887,6 +888,7 @@ export class UpdateFormulaController extends Disposable {return { newFormulaData };}+ // eslint-disable-next-lineprivate _getNewRangeByMoveParam(unitRangeWidthOffset: IUnitRangeWithOffset,formulaReferenceMoveParam: IFormulaReferenceMoveParam,@@ -920,31 +922,31 @@ export class UpdateFormulaController extends Disposable {const sequenceRange = Rectangle.moveOffset(unitRange, refOffsetX, refOffsetY);let newRange: Nullable<IRange> = null;- if (type === FormulaReferenceMoveType.MoveRange) { // array move range not need handle- // if (from == null || to == null) {- // return;- // }+ if (type === FormulaReferenceMoveType.MoveRange) {+ if (from == null || to == null) {+ return;+ }- // const moveEdge = this._checkMoveEdge(sequenceRange, from);+ const moveEdge = this._checkMoveEdge(sequenceRange, from);- // const remainRange = Rectangle.getIntersects(sequenceRange, from);+ const remainRange = Rectangle.getIntersects(sequenceRange, from);- // if (remainRange == null) {- // return;- // }+ if (remainRange == null || moveEdge !== OriginRangeEdgeType.ALL) {+ return;+ }- // const operators = handleMoveRange(- // { id: EffectRefRangId.MoveRangeCommandId, params: { toRange: to, fromRange: from } },- // remainRange- // );+ const operators = handleMoveRange(+ { id: EffectRefRangId.MoveRangeCommandId, params: { toRange: to, fromRange: from } },+ remainRange+ );- // const result = runRefRangeMutations(operators, remainRange);+ const result = runRefRangeMutations(operators, remainRange);- // if (result == null) {- // return ErrorType.REF;- // }+ if (result == null) {+ return ErrorType.REF;+ }- // newRange = this._getMoveNewRange(moveEdge, result, from, to, sequenceRange, remainRange);+ newRange = this._getMoveNewRange(moveEdge, result, from, to, sequenceRange, remainRange);} else if (type === FormulaReferenceMoveType.MoveRows) {if (from == null || to == null) {return;@@ -1259,6 +1261,7 @@ export class UpdateFormulaController extends Disposable {* @param originRange* @param fromRange*/+ // eslint-disable-next-lineprivate _checkMoveEdge(originRange: IRange, fromRange: IRange): Nullable<OriginRangeEdgeType> {const { startRow, startColumn, endRow, endColumn } = originRange;@@ -1329,6 +1332,7 @@ export class UpdateFormulaController extends Disposable {* @param remain "The range subtracted from the initial range by 'from'.* @returns*/+ // eslint-disable-next-lineprivate _getMoveNewRange(moveEdge: Nullable<OriginRangeEdgeType>,result: IRange,diff --git a/packages/sheets-ui/src/services/auto-fill/rules.ts b/packages/sheets-ui/src/services/auto-fill/rules.tsindex a3c989543f..6399bcd176 100644--- a/packages/sheets-ui/src/services/auto-fill/rules.ts+++ b/packages/sheets-ui/src/services/auto-fill/rules.ts@@ -14,8 +14,8 @@* limitations under the License.*/-import type { Workbook } from '@univerjs/core';import { CellValueType, Direction, IUniverInstanceService, numfmt } from '@univerjs/core';+import type { Workbook } from '@univerjs/core';import {chineseToNumber,@@ -33,13 +33,17 @@ import {isLoopSeries,matchExtendNumber,} from './tools';-import type { IAutoFillRule } from './type';import { APPLY_TYPE, DATA_TYPE } from './type';+import type { IAutoFillRule } from './type';export const dateRule: IAutoFillRule = {type: DATA_TYPE.DATE,priority: 1100,match: (cellData, accessor) => {+ if (cellData?.f || cellData?.si) {+ return false;+ }+if ((typeof cellData?.v === 'number' || cellData?.t === CellValueType.NUMBER)&& cellData.s) {if (typeof cellData.s === 'string') {