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): update sum product (#6152)

Univerbf1d4d5ca8d4
diff --git a/packages/engine-formula/src/functions/math/sumproduct/__tests__/index.spec.ts b/packages/engine-formula/src/functions/math/sumproduct/__tests__/index.spec.ts
index e24c208626..a9aeab16a6 100644
--- a/packages/engine-formula/src/functions/math/sumproduct/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/math/sumproduct/__tests__/index.spec.ts
@@ -93,35 +93,35 @@ describe('Test sumproduct function', () => {
expect(getObjectValue(result)).toBe(8);
});
- // it('Array1 is array, other variants is not same dimension', () => {
- // const array1 = ArrayValueObject.create({
- // calculateValueList: transformToValueObject([
- // [1, '2', 3],
- // [null, 'test', 2],
- // [true, ' ', false],
- // ]),
- // rowCount: 3,
- // columnCount: 3,
- // unitId: '',
- // sheetId: '',
- // row: 0,
- // column: 0,
- // });
- // const array2 = ArrayValueObject.create({
- // calculateValueList: transformToValueObject([
- // [1, 1, 1],
- // [true, 1, false],
- // ]),
- // rowCount: 2,
- // columnCount: 3,
- // unitId: '',
- // sheetId: '',
- // row: 0,
- // column: 0,
- // });
- // const result = testFunction.calculate(array1, array2);
- // expect(getObjectValue(result)).toBe(ErrorType.VALUE);
- // });
+ it('Array1 is array, other variants is not same dimension', () => {
+ const array1 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, '2', 3],
+ [null, 'test', 2],
+ [true, ' ', false],
+ ]),
+ rowCount: 3,
+ columnCount: 3,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const array2 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [1, 1, 1],
+ [true, 1, false],
+ ]),
+ rowCount: 2,
+ columnCount: 3,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = testFunction.calculate(array1, array2);
+ expect(getObjectValue(result)).toBe(ErrorType.VALUE);
+ });
it('More test', () => {
const array1 = ArrayValueObject.create({
diff --git a/packages/engine-formula/src/functions/math/sumproduct/index.ts b/packages/engine-formula/src/functions/math/sumproduct/index.ts
index c5110bab32..33c45f1acb 100644
--- a/packages/engine-formula/src/functions/math/sumproduct/index.ts
+++ b/packages/engine-formula/src/functions/math/sumproduct/index.ts
@@ -51,14 +51,13 @@ export class Sumproduct extends BaseFunction {
// 3. Validate variants (errors + dimension)
const variantError = this._validateVariants(variants, rowCount, columnCount);
+
if (variantError) {
return variantError;
}
// 4. Core: sum-product over all cells
- const sum = this._sumProduct(baseArray, variants, rowCount, columnCount);
-
- return NumberValueObject.create(sum);
+ return this._sumProduct(baseArray, variants, rowCount, columnCount);
}
/**
@@ -105,7 +104,7 @@ export class Sumproduct extends BaseFunction {
variants: BaseValueObject[],
rowCount: number,
columnCount: number
- ): number {
+ ): BaseValueObject {
let sum = 0;
for (let r = 0; r < rowCount; r++) {
@@ -121,12 +120,12 @@ export class Sumproduct extends BaseFunction {
// Cannot throw directly here; need to wrap it in ErrorValueObject for the caller.
// But to keep the signature simple, we let _getVariantCell return null,
// and convert it to ErrorValueObject here uniformly.
- throw ErrorValueObject.create(ErrorType.VALUE);
+ return ErrorValueObject.create(ErrorType.VALUE);
}
if (variantValueObject.isError()) {
// Bubble up error via exception; caller will catch and return
- throw variantValueObject;
+ return variantValueObject as ErrorValueObject;
}
if (variantValueObject.isNumber()) {
@@ -141,7 +140,7 @@ export class Sumproduct extends BaseFunction {
}
}
- return sum;
+ return NumberValueObject.create(sum);
}
/**