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(engine-formula): average whole array results in AVERAGE (#7800)

FenjuFub172cc716587
diff --git a/packages/engine-formula/src/functions/statistical/average/__tests__/index.spec.ts b/packages/engine-formula/src/functions/statistical/average/__tests__/index.spec.ts
index 47d9a1f81b..887d597ca0 100644
--- a/packages/engine-formula/src/functions/statistical/average/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/statistical/average/__tests__/index.spec.ts
@@ -192,5 +192,39 @@ describe('Test average function', () => {
const result = testFunction.calculate(var1, var2);
expect(getObjectValue(result, true)).toBe(7.96692307692);
});
+
+ it('Positioned function result array is averaged as a whole', () => {
+ // e.g. =AVERAGE(FILTER(B1:B3,A1:A3="Phone")) entered in H1, where FILTER returns {3;7}
+ const averageFunction = new Average(FUNCTION_NAMES_STATISTICAL.AVERAGE);
+ averageFunction.setRefInfo('', '', 0, 7);
+ const var1 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([[3], [7]]),
+ rowCount: 2,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const result = averageFunction.calculate(var1);
+ expect(getObjectValue(result)).toBe(5);
+ });
+
+ it('Legacy implicit aggregate array still uses implicit intersection', () => {
+ const averageFunction = new Average(FUNCTION_NAMES_STATISTICAL.AVERAGE);
+ averageFunction.setRefInfo('', '', 1, 7);
+ const var1 = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([[3], [7]]),
+ rowCount: 2,
+ columnCount: 1,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ var1.setLegacyImplicitForAggregate(true);
+ const result = averageFunction.calculate(var1);
+ expect(getObjectValue(result)).toBe(7);
+ });
});
});
diff --git a/packages/engine-formula/src/functions/statistical/average/index.ts b/packages/engine-formula/src/functions/statistical/average/index.ts
index 38174ec9f1..075a6a14a6 100644
--- a/packages/engine-formula/src/functions/statistical/average/index.ts
+++ b/packages/engine-formula/src/functions/statistical/average/index.ts
@@ -62,7 +62,7 @@ export class Average extends BaseFunction {
}
const array = variant as ArrayValueObject;
- if (array.usesInvertedIndexCache()) {
+ if (array.usesInvertedIndexCache() || !array.usesLegacyImplicitForAggregate()) {
return variant;
}