|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2021 Handsoncode. All rights reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +import {CellError, ErrorType} from '../../Cell' |
| 7 | +import {ErrorMessage} from '../../error-message' |
| 8 | +import {ProcedureAst} from '../../parser' |
| 9 | +import {Condition, CriterionFunctionCompute} from '../CriterionFunctionCompute' |
| 10 | +import {InterpreterState} from '../InterpreterState' |
| 11 | +import {getRawValue, InterpreterValue, RawScalarValue} from '../InterpreterValue' |
| 12 | +import {SimpleRangeValue} from '../SimpleRangeValue' |
| 13 | +import {ArgumentTypes, FunctionPlugin, FunctionPluginTypecheck} from './FunctionPlugin' |
| 14 | + |
| 15 | +/** Computes key for criterion function cache */ |
| 16 | +function minifsCacheKey(conditions: Condition[]): string { |
| 17 | + const conditionsStrings = conditions.map( |
| 18 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 19 | + (c) => `${c.conditionRange.range!.sheet},${c.conditionRange.range!.start.col},${c.conditionRange.range!.start.row}` |
| 20 | + ) |
| 21 | + return ['MINIFS', ...conditionsStrings].join(',') |
| 22 | +} |
| 23 | + |
| 24 | +/** Computes key for criterion function cache */ |
| 25 | +function maxifsCacheKey(conditions: Condition[]): string { |
| 26 | + const conditionsStrings = conditions.map( |
| 27 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 28 | + (c) => `${c.conditionRange.range!.sheet},${c.conditionRange.range!.start.col},${c.conditionRange.range!.start.row}` |
| 29 | + ) |
| 30 | + return ['MAXIFS', ...conditionsStrings].join(',') |
| 31 | +} |
| 32 | + |
| 33 | +export class MinMaxIfsPlugin extends FunctionPlugin implements FunctionPluginTypecheck<MinMaxIfsPlugin> { |
| 34 | + public static implementedFunctions = { |
| 35 | + MINIFS: { |
| 36 | + method: 'minifs', |
| 37 | + parameters: [ |
| 38 | + {argumentType: ArgumentTypes.RANGE}, |
| 39 | + {argumentType: ArgumentTypes.RANGE}, |
| 40 | + {argumentType: ArgumentTypes.NOERROR}, |
| 41 | + ], |
| 42 | + repeatLastArgs: 2, |
| 43 | + }, |
| 44 | + MAXIFS: { |
| 45 | + method: 'maxifs', |
| 46 | + parameters: [ |
| 47 | + {argumentType: ArgumentTypes.RANGE}, |
| 48 | + {argumentType: ArgumentTypes.RANGE}, |
| 49 | + {argumentType: ArgumentTypes.NOERROR}, |
| 50 | + ], |
| 51 | + repeatLastArgs: 2, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + public minifs(ast: ProcedureAst, state: InterpreterState): InterpreterValue { |
| 56 | + return this.compute(ast, state, minifsCacheKey, Number.POSITIVE_INFINITY, (left, right) => Math.min(left, right)) |
| 57 | + } |
| 58 | + |
| 59 | + public maxifs(ast: ProcedureAst, state: InterpreterState): InterpreterValue { |
| 60 | + return this.compute(ast, state, maxifsCacheKey, Number.NEGATIVE_INFINITY, (left, right) => Math.max(left, right)) |
| 61 | + } |
| 62 | + |
| 63 | + private compute( |
| 64 | + ast: ProcedureAst, |
| 65 | + state: InterpreterState, |
| 66 | + cacheKey: (conditions: Condition[]) => string, |
| 67 | + initialValue: RawScalarValue, |
| 68 | + fn: (left: number, right: number) => number |
| 69 | + ): InterpreterValue { |
| 70 | + return this.runFunction(ast.args, state, this.metadata('MINIFS'), (values: SimpleRangeValue, ...args) => { |
| 71 | + const conditions: Condition[] = [] |
| 72 | + for (let i = 0; i < args.length; i += 2) { |
| 73 | + const conditionArg = args[i] as SimpleRangeValue |
| 74 | + const criterionPackage = this.interpreter.criterionBuilder.fromCellValue(args[i + 1], this.arithmeticHelper) |
| 75 | + if (criterionPackage === undefined) { |
| 76 | + return new CellError(ErrorType.VALUE, ErrorMessage.BadCriterion) |
| 77 | + } |
| 78 | + conditions.push(new Condition(conditionArg, criterionPackage)) |
| 79 | + } |
| 80 | + |
| 81 | + return new CriterionFunctionCompute<RawScalarValue>( |
| 82 | + this.interpreter, |
| 83 | + cacheKey, |
| 84 | + initialValue, |
| 85 | + (left, right) => { |
| 86 | + if (left instanceof CellError) { |
| 87 | + return left |
| 88 | + } else if (right instanceof CellError) { |
| 89 | + return right |
| 90 | + } else if (typeof left === 'number') { |
| 91 | + if (typeof right === 'number') { |
| 92 | + return fn(left, right) |
| 93 | + } else { |
| 94 | + return left |
| 95 | + } |
| 96 | + } else if (typeof right === 'number') { |
| 97 | + return right |
| 98 | + } else { |
| 99 | + return 0 |
| 100 | + } |
| 101 | + }, |
| 102 | + (arg) => getRawValue(arg) |
| 103 | + ).compute(values, conditions) |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
0 commit comments