Skip to content

Commit

Permalink
fix(formula): vlookup handle single value object
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Mar 28, 2024
1 parent 3ac0139 commit c69f563
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 57 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
});
});
27 changes: 0 additions & 27 deletions packages/engine-formula/src/engine/utils/object-covert.ts

This file was deleted.

55 changes: 55 additions & 0 deletions packages/engine-formula/src/engine/utils/value-object.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion packages/engine-formula/src/functions/base-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c69f563

Please sign in to comment.