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 MATCH formula calculation (#6119)

wpxp12345610fda4ba9137
diff --git a/packages/engine-formula/src/engine/value-object/array-value-object.ts b/packages/engine-formula/src/engine/value-object/array-value-object.ts
index 81a7086ff5..01389a76f6 100644
--- a/packages/engine-formula/src/engine/value-object/array-value-object.ts
+++ b/packages/engine-formula/src/engine/value-object/array-value-object.ts
@@ -640,7 +640,8 @@ export class ArrayValueObject extends BaseValueObject {
let maxOrMinPosition: Nullable<{ row: number; column: number }>;
const _handleMatch = (itemValue: Nullable<BaseValueObject>, row: number, column: number) => {
- if (itemValue == null) {
+ // Skip the blank cells
+ if (itemValue == null || itemValue.isNull()) {
return true;
}
diff --git a/packages/engine-formula/src/functions/lookup/match/__tests__/index.spec.ts b/packages/engine-formula/src/functions/lookup/match/__tests__/index.spec.ts
index 2c63035596..184bf32879 100644
--- a/packages/engine-formula/src/functions/lookup/match/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/lookup/match/__tests__/index.spec.ts
@@ -17,9 +17,7 @@
import { describe, expect, it } from 'vitest';
import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
-import {
- NumberValueObject,
-} from '../../../../engine/value-object/primitive-object';
+import { NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { FUNCTION_NAMES_LOOKUP } from '../../function-names';
import { Match } from '../index';
@@ -141,5 +139,49 @@ describe('Test match', () => {
expect(resultObject.getValue()).toBe(2);
});
+
+ it('LookupArray has blank cell', async () => {
+ const lookupValue = NullValueObject.create();
+ const lookupArray = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, null, 4, 5],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const matchType = NumberValueObject.create(0);
+ const resultObject = testFunction.calculate(lookupValue, lookupArray, matchType);
+ expect(resultObject.getValue()).toBe(ErrorType.NA);
+
+ const lookupValue2 = StringValueObject.create('');
+ const resultObject2 = testFunction.calculate(lookupValue2, lookupArray, matchType);
+ expect(resultObject2.getValue()).toBe(ErrorType.NA);
+
+ const lookupValue3 = NumberValueObject.create(0); ;
+ const resultObject3 = testFunction.calculate(lookupValue3, lookupArray, matchType);
+ expect(resultObject3.getValue()).toBe(ErrorType.NA);
+ });
+
+ it('lookupValue is blank cell', async () => {
+ const lookupValue = NullValueObject.create();
+ const lookupArray = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 0, 4, 5],
+ ]),
+ rowCount: 1,
+ columnCount: 6,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const matchType = NumberValueObject.create(0);
+ const resultObject = testFunction.calculate(lookupValue, lookupArray, matchType);
+ expect(resultObject.getValue()).toBe(4);
+ });
});
});
diff --git a/packages/engine-formula/src/functions/lookup/match/index.ts b/packages/engine-formula/src/functions/lookup/match/index.ts
index 871e2ffd11..ca74a3f6d0 100644
--- a/packages/engine-formula/src/functions/lookup/match/index.ts
+++ b/packages/engine-formula/src/functions/lookup/match/index.ts
@@ -14,10 +14,10 @@
* limitations under the License.
*/
-import { ErrorType } from '../../../basics/error-type';
-import { ArrayOrderSearchType } from '../../../engine/utils/compare';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
+import { ErrorType } from '../../../basics/error-type';
+import { ArrayOrderSearchType } from '../../../engine/utils/compare';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
@@ -82,9 +82,12 @@ export class Match extends BaseFunction {
searchArray: ArrayValueObject,
matchTypeValue: number
) {
+ // The blank cell in Excel will be treated as 0 in MATCH function.
+ const _value = value.isNull() ? NumberValueObject.create(0) : value;
+
const searchType = this._getSearchModeValue(matchTypeValue);
- const result = searchArray.orderSearch(value, searchType);
+ const result = searchArray.orderSearch(_value, searchType);
if (result == null) {
return ErrorValueObject.create(ErrorType.NA);