Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(function): add MINIFS and MAXIFS #1047

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guide/built-in-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,11 @@ Total number of functions: **{{ $page.functionsCount }}**
| LOGINV | Returns value of inverse lognormal distribution. | LOGINV(P; Mean; Stddev) |
| MAX | Returns the maximum value in a list of arguments. | MAX(Number1; Number2; ...Number30) |
| MAXA | Returns the maximum value in a list of arguments. | MAXA(Value1; Value2; ... Value30) |
| MAXIFS | Returns the max of the values of cells in a range that meets multiple criteria in multiple ranges. | MAXIFS(Max_Range ; Criterion_range1 ; Criterion1 [ ; Criterion_range2 ; Criterion2 [;...]]) |
| MEDIAN | Returns the median of a set of numbers. | MEDIAN(Number1; Number2; ...Number30) |
| MIN | Returns the minimum value in a list of arguments. | MIN(Number1; Number2; ...Number30) |
| MINA | Returns the minimum value in a list of arguments. | MINA(Value1; Value2; ... Value30) |
| MINIFS | Returns the min of the values of cells in a range that meets multiple criteria in multiple ranges. | MINIFS(Min_Range ; Criterion_range1 ; Criterion1 [ ; Criterion_range2 ; Criterion2 [;...]]) |
| NEGBINOM.DIST | Returns density of negative binomial distribution. | NEGBINOM.DIST(Number1; Number2; Number3; Mode) |
| NEGBINOMDIST | Returns density of negative binomial distribution. | NEGBINOMDIST(Number1; Number2; Number3; Mode) |
| NORM.DIST | Returns density of normal distribution. | NORM.DIST(X; Mean; Stddev; Mode) |
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/csCZ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'POZVYHLEDAT',
MAX: 'MAX',
MAXA: 'MAXA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIAN',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'ČÁST',
MIN: 'MIN',
MINA: 'MINA',
MINIFS: 'MINIFS',
MINUTE: 'MINUTA',
MIRR: 'MOD.MÍRA.VÝNOSNOSTI',
MMULT: 'SOUČIN.MATIC',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/daDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'SAMMENLIGN',
MAX: 'MAKS',
MAXA: 'MAKSV',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIAN',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'MIDT',
MIN: 'MIN',
MINA: 'MINV',
MINIFS: 'MINIFS',
MINUTE: 'MINUT',
MIRR: 'MIA',
MMULT: 'MPRODUKT',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/deDE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'VERGLEICH',
MAX: 'MAX',
MAXA: 'MAXA',
MAXIFS: 'MAXWENNS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIAN',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'TEIL',
MIN: 'MIN',
MINA: 'MINA',
MINIFS: 'MINWENNS',
MINUTE: 'MINUTE',
MIRR: 'QIKV',
MMULT: 'MMULT',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/enGB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'MATCH',
MAX: 'MAX',
MAXA: 'MAXA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIAN',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'MID',
MIN: 'MIN',
MINA: 'MINA',
MINIFS: 'MINIFS',
MINUTE: 'MINUTE',
MIRR: 'MIRR',
MMULT: 'MMULT',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/esES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ export const dictionary: RawTranslationPackage = {
MATCH: 'COINCIDIR',
MAX: 'MAX',
MAXA: 'MAXA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIANA',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'EXTRAE',
MIN: 'MIN',
MINA: 'MINA',
MINIFS: 'MINIFS',
MINUTE: 'MINUTO',
MIRR: 'TIRM',
MMULT: 'MMULT',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/fiFI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'VASTINE',
MAX: 'MAKS',
MAXA: 'MAKSA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIAANI',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'POIMI.TEKSTI',
MIN: 'MIN',
MINA: 'MINA',
MINIFS: 'MINIFS',
MINUTE: 'MINUUTIT',
MIRR: 'MSISÄINEN',
MMULT: 'MKERRO',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/frFR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'EQUIV',
MAX: 'MAX',
MAXA: 'MAXA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIANE',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'STXT',
MIN: 'MIN',
MINA: 'MINA',
MINIFS: 'MINIFS',
MINUTE: 'MINUTE',
MIRR: 'TRIM',
MMULT: 'PRODUITMAT',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/huHU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'HOL.VAN',
MAX: 'MAX',
MAXA: 'MAXA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIÁN',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'KÖZÉP',
MIN: 'MIN',
MINA: 'MINA',
MINIFS: 'MINIFS',
MINUTE: 'PERCEK',
MIRR: 'MEGTÉRÜLÉS',
MMULT: 'MSZORZAT',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/itIT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'CONFRONTA',
MAX: 'MAX',
MAXA: 'MAX.VALORI',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIANA',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'STRINGA.ESTRAI',
MIN: 'MIN',
MINA: 'MIN.VALORI',
MINIFS: 'MINIFS',
MINUTE: 'MINUTO',
MIRR: 'TIR.VAR',
MMULT: 'MATR.PRODOTTO',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/nbNO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'SAMMENLIGNE',
MAX: 'STØRST',
MAXA: 'MAKSA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIAN',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'DELTEKST',
MIN: 'MIN',
MINA: 'MINA',
MINIFS: 'MINIFS',
MINUTE: 'MINUTT',
MIRR: 'MODIR',
MMULT: 'MMULT',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/nlNL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'VERGELIJKEN',
MAX: 'MAX',
MAXA: 'MAXA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIAAN',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'DEEL',
MIN: 'MIN',
MINA: 'MINA',
MINIFS: 'MINIFS',
MINUTE: 'MINUUT',
MIRR: 'GIR',
MMULT: 'PRODUCTMAT',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/plPL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'PODAJ.POZYCJĘ',
MAX: 'MAKS',
MAXA: 'MAX.A',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAKS.Z.PULI',
MEDIAN: 'MEDIANA',
MEDIANPOOL: 'MEDIANA.Z.PULI',
MID: 'FRAGMENT.TEKSTU',
MIN: 'MIN',
MINA: 'MIN.A',
MINIFS: 'MINIFS',
MINUTE: 'MINUTA',
MIRR: 'MIRR',
MMULT: 'MACIERZ.ILOCZYN',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/ptPT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'CORRESP',
MAX: 'MÁXIMO',
MAXA: 'MÁXIMOA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MED',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'EXT.TEXTO',
MIN: 'MÍNIMO',
MINA: 'MÍNIMOA',
MINIFS: 'MINIFS',
MINUTE: 'MINUTO',
MIRR: 'MTIR',
MMULT: 'MATRIZ.MULT',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/ruRU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'ПОИСКПОЗ',
MAX: 'МАКС',
MAXA: 'МАКСА',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'МЕДИАНА',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'ПСТР',
MIN: 'МИН',
MINA: 'МИНА',
MINIFS: 'MINIFS',
MINUTE: 'МИНУТЫ',
MIRR: 'МВСД',
MMULT: 'МУМНОЖ',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/svSE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'PASSA',
MAX: 'MAX',
MAXA: 'MAXA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'MEDIAN',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'EXTEXT',
MIN: 'MIN',
MINA: 'MINA',
MINIFS: 'MINIFS',
MINUTE: 'MINUT',
MIRR: 'MODIR',
MMULT: 'MMULT',
Expand Down
2 changes: 2 additions & 0 deletions src/i18n/languages/trTR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ const dictionary: RawTranslationPackage = {
MATCH: 'KAÇINCI',
MAX: 'MAK',
MAXA: 'MAKA',
MAXIFS: 'MAXIFS',
MAXPOOL: 'MAXPOOL',
MEDIAN: 'ORTANCA',
MEDIANPOOL: 'MEDIANPOOL',
MID: 'PARÇAAL',
MIN: 'MİN',
MINA: 'MİNA',
MINIFS: 'MINIFS',
MINUTE: 'DAKİKA',
MIRR: 'D_İÇ_VERİM_ORANI',
MMULT: 'DÇARP',
Expand Down
106 changes: 106 additions & 0 deletions src/interpreter/plugin/MinMaxIfsPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @license
* Copyright (c) 2021 Handsoncode. All rights reserved.
*/

import {CellError, ErrorType} from '../../Cell'
import {ErrorMessage} from '../../error-message'
import {ProcedureAst} from '../../parser'
import {Condition, CriterionFunctionCompute} from '../CriterionFunctionCompute'
import {InterpreterState} from '../InterpreterState'
import {getRawValue, InterpreterValue, RawScalarValue} from '../InterpreterValue'
import {SimpleRangeValue} from '../SimpleRangeValue'
import {ArgumentTypes, FunctionPlugin, FunctionPluginTypecheck} from './FunctionPlugin'

/** Computes key for criterion function cache */
function minifsCacheKey(conditions: Condition[]): string {
const conditionsStrings = conditions.map(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(c) => `${c.conditionRange.range!.sheet},${c.conditionRange.range!.start.col},${c.conditionRange.range!.start.row}`
)
return ['MINIFS', ...conditionsStrings].join(',')
}

/** Computes key for criterion function cache */
function maxifsCacheKey(conditions: Condition[]): string {
const conditionsStrings = conditions.map(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(c) => `${c.conditionRange.range!.sheet},${c.conditionRange.range!.start.col},${c.conditionRange.range!.start.row}`
)
return ['MAXIFS', ...conditionsStrings].join(',')
}

export class MinMaxIfsPlugin extends FunctionPlugin implements FunctionPluginTypecheck<MinMaxIfsPlugin> {
public static implementedFunctions = {
MINIFS: {
method: 'minifs',
parameters: [
{argumentType: ArgumentTypes.RANGE},
{argumentType: ArgumentTypes.RANGE},
{argumentType: ArgumentTypes.NOERROR},
],
repeatLastArgs: 2,
},
MAXIFS: {
method: 'maxifs',
parameters: [
{argumentType: ArgumentTypes.RANGE},
{argumentType: ArgumentTypes.RANGE},
{argumentType: ArgumentTypes.NOERROR},
],
repeatLastArgs: 2,
},
}

public minifs(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.compute(ast, state, minifsCacheKey, Number.POSITIVE_INFINITY, (left, right) => Math.min(left, right))
}

public maxifs(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.compute(ast, state, maxifsCacheKey, Number.NEGATIVE_INFINITY, (left, right) => Math.max(left, right))
}

private compute(
ast: ProcedureAst,
state: InterpreterState,
cacheKey: (conditions: Condition[]) => string,
initialValue: RawScalarValue,
fn: (left: number, right: number) => number
): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('MINIFS'), (values: SimpleRangeValue, ...args) => {
const conditions: Condition[] = []
for (let i = 0; i < args.length; i += 2) {
const conditionArg = args[i] as SimpleRangeValue
const criterionPackage = this.interpreter.criterionBuilder.fromCellValue(args[i + 1], this.arithmeticHelper)
if (criterionPackage === undefined) {
return new CellError(ErrorType.VALUE, ErrorMessage.BadCriterion)
}
conditions.push(new Condition(conditionArg, criterionPackage))
}

return new CriterionFunctionCompute<RawScalarValue>(
this.interpreter,
cacheKey,
initialValue,
(left, right) => {
if (left instanceof CellError) {
return left
} else if (right instanceof CellError) {
return right
} else if (typeof left === 'number') {
if (typeof right === 'number') {
return fn(left, right)
} else {
return left
}
} else if (typeof right === 'number') {
return right
} else {
return 0
}
},
(arg) => getRawValue(arg)
).compute(values, conditions)
})
}
}
1 change: 1 addition & 0 deletions src/interpreter/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ export {StatisticalPlugin} from './StatisticalPlugin'
export {MathPlugin} from './MathPlugin'
export {ComplexPlugin} from './ComplexPlugin'
export {StatisticalAggregationPlugin} from './StatisticalAggregationPlugin'
export {MinMaxIfsPlugin} from './MinMaxIfsPlugin'
Loading