diff --git a/packages/engine-formula/src/engine/utils/__tests__/object-covert.spec.ts b/packages/engine-formula/src/engine/utils/__tests__/object-covert.spec.ts deleted file mode 100644 index 70ae4628658f..000000000000 --- a/packages/engine-formula/src/engine/utils/__tests__/object-covert.spec.ts +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright 2023-present DreamNum Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { describe, expect, it } from 'vitest'; - -import { convertTonNumber } from '../object-covert'; -import { BooleanValueObject } from '../../value-object/primitive-object'; - -describe('Test object cover', () => { - it('Function convertTonNumber', () => { - expect(convertTonNumber(BooleanValueObject.create(true)).getValue()).toBe(1); - expect(convertTonNumber(BooleanValueObject.create(false)).getValue()).toBe(0); - }); -}); diff --git a/packages/engine-formula/src/engine/utils/__tests__/value-object.spec.ts b/packages/engine-formula/src/engine/utils/__tests__/value-object.spec.ts new file mode 100644 index 000000000000..2f44cd38f660 --- /dev/null +++ b/packages/engine-formula/src/engine/utils/__tests__/value-object.spec.ts @@ -0,0 +1,58 @@ +/** + * Copyright 2023-present DreamNum Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, expect, it } from 'vitest'; + +import { cellToRange } from '@univerjs/core'; +import { convertTonNumber, isSingleValueObject } from '../value-object'; +import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../value-object/primitive-object'; +import { ErrorValueObject } from '../../value-object/base-value-object'; +import { ErrorType } from '../../../basics/error-type'; +import { ArrayValueObject } from '../../value-object/array-value-object'; +import { CellReferenceObject } from '../../reference-object/cell-reference-object'; +import { RowReferenceObject } from '../../reference-object/row-reference-object'; +import { ColumnReferenceObject } from '../../reference-object/column-reference-object'; +import { RangeReferenceObject } from '../../reference-object/range-reference-object'; + +describe('Test object cover', () => { + it('Function convertTonNumber', () => { + expect(convertTonNumber(BooleanValueObject.create(true)).getValue()).toBe(1); + expect(convertTonNumber(BooleanValueObject.create(false)).getValue()).toBe(0); + }); + + it('Function isSingleValueObject', () => { + expect(isSingleValueObject(BooleanValueObject.create(true))).toBe(true); + expect(isSingleValueObject(BooleanValueObject.create(false))).toBe(true); + expect(isSingleValueObject(NumberValueObject.create(1))).toBe(true); + expect(isSingleValueObject(StringValueObject.create('Univer'))).toBe(true); + expect(isSingleValueObject(NullValueObject.create())).toBe(true); + expect(isSingleValueObject(ErrorValueObject.create(ErrorType.VALUE))).toBe(true); + + expect(isSingleValueObject(ArrayValueObject.create(/*ts*/ `{ + 1, 3; + 8, 7 + }`))).toBe(false); + expect(isSingleValueObject(ArrayValueObject.create(/*ts*/ `{ + 1 + }`))).toBe(true); + + expect(isSingleValueObject(new CellReferenceObject('A1'))).toBe(true); + expect(isSingleValueObject(new RowReferenceObject('1:1'))).toBe(false); + expect(isSingleValueObject(new ColumnReferenceObject('A:A'))).toBe(false); + expect(isSingleValueObject(new RangeReferenceObject(cellToRange(0, 1)))).toBe(true); + expect(isSingleValueObject(new RangeReferenceObject({ startRow: 0, endRow: 1, startColumn: 0, endColumn: 1 }))).toBe(false); + }); +}); diff --git a/packages/engine-formula/src/engine/utils/object-covert.ts b/packages/engine-formula/src/engine/utils/object-covert.ts deleted file mode 100644 index a19fca15dba3..000000000000 --- a/packages/engine-formula/src/engine/utils/object-covert.ts +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright 2023-present DreamNum Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import type { BaseValueObject } from '../value-object/base-value-object'; -import { NumberValueObject } from '../value-object/primitive-object'; - -export function convertTonNumber(valueObject: BaseValueObject) { - const currentValue = valueObject.getValue(); - let result = 0; - if (currentValue) { - result = 1; - } - return NumberValueObject.create(result); -} diff --git a/packages/engine-formula/src/engine/utils/value-object.ts b/packages/engine-formula/src/engine/utils/value-object.ts new file mode 100644 index 000000000000..168edabfb65a --- /dev/null +++ b/packages/engine-formula/src/engine/utils/value-object.ts @@ -0,0 +1,55 @@ +/** + * Copyright 2023-present DreamNum Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import type { BaseReferenceObject, FunctionVariantType } from '../reference-object/base-reference-object'; +import type { ArrayValueObject } from '../value-object/array-value-object'; +import type { BaseValueObject } from '../value-object/base-value-object'; +import { NumberValueObject } from '../value-object/primitive-object'; + +export function convertTonNumber(valueObject: BaseValueObject) { + const currentValue = valueObject.getValue(); + let result = 0; + if (currentValue) { + result = 1; + } + return NumberValueObject.create(result); +} + +export function isSingleValueObject(valueObject: FunctionVariantType) { + if (valueObject.isArray() && (valueObject as ArrayValueObject).getRowCount() === 1 && (valueObject as ArrayValueObject).getColumnCount() === 1) { + return true; + } + + if (valueObject.isReferenceObject()) { + if ((valueObject as BaseReferenceObject).isCell()) { + return true; + } + + if ((valueObject as BaseReferenceObject).getRowCount() === 1 && (valueObject as BaseReferenceObject).getColumnCount() === 1) { + return true; + } + + return false; + } + + valueObject = valueObject as BaseValueObject; + + if (valueObject.isString() || valueObject.isNumber() || valueObject.isBoolean() || valueObject.isError() || valueObject.isNull()) { + return true; + } + + return false; +} diff --git a/packages/engine-formula/src/functions/base-function.ts b/packages/engine-formula/src/functions/base-function.ts index 4b9f7939b54d..08084cc08f72 100644 --- a/packages/engine-formula/src/functions/base-function.ts +++ b/packages/engine-formula/src/functions/base-function.ts @@ -26,7 +26,7 @@ import { ArrayOrderSearchType } from '../engine/utils/compare'; import type { ArrayValueObject } from '../engine/value-object/array-value-object'; import { type BaseValueObject, ErrorValueObject } from '../engine/value-object/base-value-object'; import { NullValueObject, NumberValueObject, type PrimitiveValueType } from '../engine/value-object/primitive-object'; -import { convertTonNumber } from '../engine/utils/object-covert'; +import { convertTonNumber } from '../engine/utils/value-object'; import { createNewArray } from '../engine/utils/array-object'; import { serializeRangeToRefString } from '../engine/utils/reference'; import { REFERENCE_REGEX_SINGLE_COLUMN, REFERENCE_REGEX_SINGLE_ROW, REFERENCE_SINGLE_RANGE_REGEX } from '../basics/regex'; diff --git a/packages/engine-formula/src/functions/lookup/index/index.ts b/packages/engine-formula/src/functions/lookup/index/index.ts index 4572d7c50862..cf8eff13f52d 100644 --- a/packages/engine-formula/src/functions/lookup/index/index.ts +++ b/packages/engine-formula/src/functions/lookup/index/index.ts @@ -27,7 +27,6 @@ import { BaseFunction } from '../../base-function'; * * =INDEX(A2:A5,2,1):A1 same as =A1:A3 * - * TODO@Dushusir: Promote public type to ObjectClassType */ export class Index extends BaseFunction { override needsReferenceObject = true; diff --git a/packages/engine-formula/src/functions/lookup/vlookup/index.ts b/packages/engine-formula/src/functions/lookup/vlookup/index.ts index e3a8a08dd2ee..2e6951469b38 100644 --- a/packages/engine-formula/src/functions/lookup/vlookup/index.ts +++ b/packages/engine-formula/src/functions/lookup/vlookup/index.ts @@ -22,6 +22,7 @@ import type { BaseValueObject } from '../../../engine/value-object/base-value-ob import { ErrorValueObject } from '../../../engine/value-object/base-value-object'; import { BooleanValueObject } from '../../../engine/value-object/primitive-object'; import { BaseFunction } from '../../base-function'; +import { isSingleValueObject } from '../../../engine/utils/value-object'; export class Vlookup extends BaseFunction { override calculate( @@ -59,7 +60,9 @@ export class Vlookup extends BaseFunction { // When neither lookupValue nor rangeLookup is an array, but colIndexNum is an array, expansion is allowed based on the value of colIndexNum. However, if an error is encountered in subsequent matching, the first error needs to be returned (not the entire array) // Otherwise colIndexNum takes the first value - if (!lookupValue.isArray() && !rangeLookup.isArray() && colIndexNum.isArray()) { + if (isSingleValueObject(lookupValue) && isSingleValueObject(rangeLookup) && colIndexNum.isArray()) { + lookupValue = lookupValue.isArray() ? (lookupValue as ArrayValueObject).getFirstCell() : lookupValue; + const rangeLookupValue = this.getZeroOrOneByOneDefault(rangeLookup); if (rangeLookupValue == null) {