Search in sources :

Example 1 with ValueVector

use of org.apache.poi.ss.formula.functions.LookupUtils.ValueVector in project poi by apache.

the class Match method evaluateLookupRange.

private static ValueVector evaluateLookupRange(ValueEval eval) throws EvaluationException {
    if (eval instanceof RefEval) {
        RefEval re = (RefEval) eval;
        if (re.getNumberOfSheets() == 1) {
            return new SingleValueVector(re.getInnerValueEval(re.getFirstSheetIndex()));
        } else {
            return LookupUtils.createVector(re);
        }
    }
    if (eval instanceof TwoDEval) {
        ValueVector result = LookupUtils.createVector((TwoDEval) eval);
        if (result == null) {
            throw new EvaluationException(ErrorEval.NA);
        }
        return result;
    }
    // Error handling for lookup_range arg is also unusual
    if (eval instanceof NumericValueEval) {
        throw new EvaluationException(ErrorEval.NA);
    }
    if (eval instanceof StringEval) {
        StringEval se = (StringEval) eval;
        Double d = OperandResolver.parseDouble(se.getStringValue());
        if (d == null) {
            // plain string
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        // else looks like a number
        throw new EvaluationException(ErrorEval.NA);
    }
    throw new RuntimeException("Unexpected eval type (" + eval + ")");
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) TwoDEval(org.apache.poi.ss.formula.TwoDEval) RefEval(org.apache.poi.ss.formula.eval.RefEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 2 with ValueVector

use of org.apache.poi.ss.formula.functions.LookupUtils.ValueVector in project poi by apache.

the class Lookup method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) {
    try {
        ValueEval lookupValue = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
        TwoDEval aeLookupVector = LookupUtils.resolveTableArrayArg(arg1);
        TwoDEval aeResultVector = LookupUtils.resolveTableArrayArg(arg2);
        ValueVector lookupVector = createVector(aeLookupVector);
        ValueVector resultVector = createVector(aeResultVector);
        if (lookupVector.getSize() > resultVector.getSize()) {
            // Excel seems to handle this by accessing past the end of the result vector.
            throw new RuntimeException("Lookup vector and result vector of differing sizes not supported yet");
        }
        int index = LookupUtils.lookupIndexOfValue(lookupValue, lookupVector, true);
        return resultVector.getItem(index);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) TwoDEval(org.apache.poi.ss.formula.TwoDEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 3 with ValueVector

use of org.apache.poi.ss.formula.functions.LookupUtils.ValueVector in project poi by apache.

the class Hlookup method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2, ValueEval arg3) {
    try {
        // Evaluation order:
        // arg0 lookup_value, arg1 table_array, arg3 range_lookup, find lookup value, arg2 row_index, fetch result
        ValueEval lookupValue = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
        TwoDEval tableArray = LookupUtils.resolveTableArrayArg(arg1);
        boolean isRangeLookup = LookupUtils.resolveRangeLookupArg(arg3, srcRowIndex, srcColumnIndex);
        int colIndex = LookupUtils.lookupIndexOfValue(lookupValue, LookupUtils.createRowVector(tableArray, 0), isRangeLookup);
        int rowIndex = LookupUtils.resolveRowOrColIndexArg(arg2, srcRowIndex, srcColumnIndex);
        ValueVector resultCol = createResultColumnVector(tableArray, rowIndex);
        return resultCol.getItem(colIndex);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) TwoDEval(org.apache.poi.ss.formula.TwoDEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 4 with ValueVector

use of org.apache.poi.ss.formula.functions.LookupUtils.ValueVector in project poi by apache.

the class LinearRegressionFunction method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
    double result;
    try {
        ValueVector vvY = createValueVector(arg0);
        ValueVector vvX = createValueVector(arg1);
        int size = vvX.getSize();
        if (size == 0 || vvY.getSize() != size) {
            return ErrorEval.NA;
        }
        result = evaluateInternal(vvX, vvY, size);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    if (Double.isNaN(result) || Double.isInfinite(result)) {
        return ErrorEval.NUM_ERROR;
    }
    return new NumberEval(result);
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 5 with ValueVector

use of org.apache.poi.ss.formula.functions.LookupUtils.ValueVector in project poi by apache.

the class Match method eval.

private static ValueEval eval(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, double match_type) {
    boolean matchExact = match_type == 0;
    // Note - Excel does not strictly require -1 and +1
    boolean findLargestLessThanOrEqual = match_type > 0;
    try {
        ValueEval lookupValue = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
        ValueVector lookupRange = evaluateLookupRange(arg1);
        int index = findIndexOfValue(lookupValue, lookupRange, matchExact, findLargestLessThanOrEqual);
        // +1 to convert to 1-based
        return new NumberEval(index + 1);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Aggregations

EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)7 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)7 TwoDEval (org.apache.poi.ss.formula.TwoDEval)4 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)4 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)3 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)2 RefEval (org.apache.poi.ss.formula.eval.RefEval)1 StringEval (org.apache.poi.ss.formula.eval.StringEval)1