Skip to content

Commit

Permalink
fix(formula): common util for xxxIFS function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Jun 19, 2024
1 parent 64ea86c commit 69a15e9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 51 deletions.
25 changes: 25 additions & 0 deletions packages/engine-formula/src/engine/utils/value-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,28 @@ export function objectValueToCellValue(objectValue: Nullable<BaseValueObject>):
};
}
}

/**
* The size of the extended range is determined by the maximum width and height of the criteria range.
* @param variants
* @returns
*/
export function calculateMaxDimensions(variants: BaseValueObject[]) {
let maxRowLength = 0;
let maxColumnLength = 0;

variants.forEach((variant, i) => {
if (i % 2 === 1) {
if (variant.isArray()) {
const arrayValue = variant as ArrayValueObject;
maxRowLength = Math.max(maxRowLength, arrayValue.getRowCount());
maxColumnLength = Math.max(maxColumnLength, arrayValue.getColumnCount());
} else {
maxRowLength = Math.max(maxRowLength, 1);
maxColumnLength = Math.max(maxColumnLength, 1);
}
}
});

return { maxRowLength, maxColumnLength };
}
19 changes: 3 additions & 16 deletions packages/engine-formula/src/functions/math/sumifs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { ErrorType } from '../../../basics/error-type';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { booleanObjectIntersection, valueObjectCompare } from '../../../engine/utils/object-compare';
import { calculateMaxDimensions } from '../../../engine/utils/value-object';
import { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject, IArrayValueObject } from '../../../engine/value-object/base-value-object';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
Expand Down Expand Up @@ -46,24 +47,10 @@ export class Sumifs extends BaseFunction {
return ErrorValueObject.create(ErrorType.VALUE);
}

const { maxRowLength, maxColumnLength } = calculateMaxDimensions(variants);

const sumRowLength = (sumRange as ArrayValueObject).getRowCount();
const sumColumnLength = (sumRange as ArrayValueObject).getColumnCount();
// The size of the extended range is determined by the maximum width and height of the criteria range.
let maxRowLength = 0;
let maxColumnLength = 0;

variants.forEach((variant, i) => {
if (i % 2 === 1) {
if (variant.isArray()) {
const arrayValue = variant as ArrayValueObject;
maxRowLength = Math.max(maxRowLength, arrayValue.getRowCount());
maxColumnLength = Math.max(maxColumnLength, arrayValue.getColumnCount());
} else {
maxRowLength = Math.max(maxRowLength, 1);
maxColumnLength = Math.max(maxColumnLength, 1);
}
}
});

const booleanResults: BaseValueObject[][] = [];

Expand Down
20 changes: 4 additions & 16 deletions packages/engine-formula/src/functions/statistical/maxifs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { ErrorType } from '../../../basics/error-type';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { booleanObjectIntersection, valueObjectCompare } from '../../../engine/utils/object-compare';
import { calculateMaxDimensions } from '../../../engine/utils/value-object';
import { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject, IArrayValueObject } from '../../../engine/value-object/base-value-object';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
Expand Down Expand Up @@ -46,24 +47,10 @@ export class Maxifs extends BaseFunction {
return ErrorValueObject.create(ErrorType.VALUE);
}

const { maxRowLength, maxColumnLength } = calculateMaxDimensions(variants);

const sumRowLength = (maxRange as ArrayValueObject).getRowCount();
const sumColumnLength = (maxRange as ArrayValueObject).getColumnCount();
// The size of the extended range is determined by the maximum width and height of the criteria range.
let maxRowLength = 0;
let maxColumnLength = 0;

variants.forEach((variant, i) => {
if (i % 2 === 1) {
if (variant.isArray()) {
const arrayValue = variant as ArrayValueObject;
maxRowLength = Math.max(maxRowLength, arrayValue.getRowCount());
maxColumnLength = Math.max(maxColumnLength, arrayValue.getColumnCount());
} else {
maxRowLength = Math.max(maxRowLength, 1);
maxColumnLength = Math.max(maxColumnLength, 1);
}
}
});

const booleanResults: BaseValueObject[][] = [];

Expand Down Expand Up @@ -128,3 +115,4 @@ export class Maxifs extends BaseFunction {
return ArrayValueObject.create(arrayValueObjectData);
}
}

25 changes: 6 additions & 19 deletions packages/engine-formula/src/functions/statistical/minifs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { ErrorType } from '../../../basics/error-type';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { booleanObjectIntersection, valueObjectCompare } from '../../../engine/utils/object-compare';
import { calculateMaxDimensions } from '../../../engine/utils/value-object';
import { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject, IArrayValueObject } from '../../../engine/value-object/base-value-object';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
Expand Down Expand Up @@ -46,24 +47,10 @@ export class Minifs extends BaseFunction {
return ErrorValueObject.create(ErrorType.VALUE);
}

const sumRowLength = (minRange as ArrayValueObject).getRowCount();
const sumColumnLength = (minRange as ArrayValueObject).getColumnCount();
// The size of the extended range is determined by the maximum width and height of the criteria range.
let maxRowLength = 0;
let maxColumnLength = 0;

variants.forEach((variant, i) => {
if (i % 2 === 1) {
if (variant.isArray()) {
const arrayValue = variant as ArrayValueObject;
maxRowLength = Math.max(maxRowLength, arrayValue.getRowCount());
maxColumnLength = Math.max(maxColumnLength, arrayValue.getColumnCount());
} else {
maxRowLength = Math.max(maxRowLength, 1);
maxColumnLength = Math.max(maxColumnLength, 1);
}
}
});
const { maxRowLength, maxColumnLength } = calculateMaxDimensions(variants);

const minRowLength = (minRange as ArrayValueObject).getRowCount();
const minColumnLength = (minRange as ArrayValueObject).getColumnCount();

const booleanResults: BaseValueObject[][] = [];

Expand All @@ -74,7 +61,7 @@ export class Minifs extends BaseFunction {

const rangeRowLength = range.getRowCount();
const rangeColumnLength = range.getColumnCount();
if (rangeRowLength !== sumRowLength || rangeColumnLength !== sumColumnLength) {
if (rangeRowLength !== minRowLength || rangeColumnLength !== minColumnLength) {
return expandArrayValueObject(maxRowLength, maxColumnLength, ErrorValueObject.create(ErrorType.NA));
}

Expand Down

0 comments on commit 69a15e9

Please sign in to comment.