Search in sources :

Example 1 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.

the class LookupUtils method resolveRowOrColIndexArg.

/**
	 * Processes the third argument to VLOOKUP, or HLOOKUP (<b>col_index_num</b>
	 * or <b>row_index_num</b> respectively).<br>
	 * Sample behaviour:
	 *    <table border="0" cellpadding="1" cellspacing="2" summary="Sample behaviour">
	 *      <tr><th>Input&nbsp;&nbsp;&nbsp;Return</th><th>Value&nbsp;&nbsp;</th><th>Thrown Error</th></tr>
	 *      <tr><td>5</td><td>4</td><td>&nbsp;</td></tr>
	 *      <tr><td>2.9</td><td>2</td><td>&nbsp;</td></tr>
	 *      <tr><td>"5"</td><td>4</td><td>&nbsp;</td></tr>
	 *      <tr><td>"2.18e1"</td><td>21</td><td>&nbsp;</td></tr>
	 *      <tr><td>"-$2"</td><td>-3</td><td>*</td></tr>
	 *      <tr><td>FALSE</td><td>-1</td><td>*</td></tr>
	 *      <tr><td>TRUE</td><td>0</td><td>&nbsp;</td></tr>
	 *      <tr><td>"TRUE"</td><td>&nbsp;</td><td>#REF!</td></tr>
	 *      <tr><td>"abc"</td><td>&nbsp;</td><td>#REF!</td></tr>
	 *      <tr><td>""</td><td>&nbsp;</td><td>#REF!</td></tr>
	 *      <tr><td>&lt;blank&gt;</td><td>&nbsp;</td><td>#VALUE!</td></tr>
	 *    </table><br/>
	 *
	 * Note - out of range errors (result index too high) are handled by the caller.
	 * @return column or row index as a zero-based value, never negative.
	 * @throws EvaluationException when the specified arg cannot be coerced to a non-negative integer
	 */
public static int resolveRowOrColIndexArg(ValueEval rowColIndexArg, int srcCellRow, int srcCellCol) throws EvaluationException {
    if (rowColIndexArg == null) {
        throw new IllegalArgumentException("argument must not be null");
    }
    ValueEval veRowColIndexArg;
    try {
        veRowColIndexArg = OperandResolver.getSingleValue(rowColIndexArg, srcCellRow, (short) srcCellCol);
    } catch (EvaluationException e) {
        // All errors get translated to #REF!
        throw EvaluationException.invalidRef();
    }
    int oneBasedIndex;
    if (veRowColIndexArg instanceof StringEval) {
        StringEval se = (StringEval) veRowColIndexArg;
        String strVal = se.getStringValue();
        Double dVal = OperandResolver.parseDouble(strVal);
        if (dVal == null) {
            // String does not resolve to a number. Raise #REF! error.
            throw EvaluationException.invalidRef();
        // This includes text booleans "TRUE" and "FALSE".  They are not valid.
        }
    // else - numeric value parses OK
    }
    // actual BoolEval values get interpreted as FALSE->0 and TRUE->1
    oneBasedIndex = OperandResolver.coerceValueToInt(veRowColIndexArg);
    if (oneBasedIndex < 1) {
        // note this is asymmetric with the errors when the index is too large (#REF!)
        throw EvaluationException.invalidValue();
    }
    // convert to zero based
    return oneBasedIndex - 1;
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 2 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.

the class Match method evaluateMatchTypeArg.

private static double evaluateMatchTypeArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
    ValueEval match_type = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
    if (match_type instanceof ErrorEval) {
        throw new EvaluationException((ErrorEval) match_type);
    }
    if (match_type instanceof NumericValueEval) {
        NumericValueEval ne = (NumericValueEval) match_type;
        return ne.getNumberValue();
    }
    if (match_type instanceof StringEval) {
        StringEval se = (StringEval) match_type;
        Double d = OperandResolver.parseDouble(se.getStringValue());
        if (d == null) {
            // plain string
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        // if the string parses as a number, it is OK
        return d.doubleValue();
    }
    throw new RuntimeException("Unexpected match_type type (" + match_type.getClass().getName() + ")");
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 3 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.

the class Mode method collectValues.

private static void collectValues(ValueEval arg, List<Double> temp) throws EvaluationException {
    if (arg instanceof TwoDEval) {
        TwoDEval ae = (TwoDEval) arg;
        int width = ae.getWidth();
        int height = ae.getHeight();
        for (int rrIx = 0; rrIx < height; rrIx++) {
            for (int rcIx = 0; rcIx < width; rcIx++) {
                ValueEval ve1 = ae.getValue(rrIx, rcIx);
                collectValue(ve1, temp, false);
            }
        }
        return;
    }
    if (arg instanceof RefEval) {
        RefEval re = (RefEval) arg;
        final int firstSheetIndex = re.getFirstSheetIndex();
        final int lastSheetIndex = re.getLastSheetIndex();
        for (int sIx = firstSheetIndex; sIx <= lastSheetIndex; sIx++) {
            collectValue(re.getInnerValueEval(sIx), temp, true);
        }
        return;
    }
    collectValue(arg, temp, true);
}
Also used : TwoDEval(org.apache.poi.ss.formula.TwoDEval) RefEval(org.apache.poi.ss.formula.eval.RefEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval)

Example 4 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.

the class MultiOperandNumericFunction method collectValues.

/**
	 * Collects values from a single argument
	 */
private void collectValues(ValueEval operand, DoubleList temp) throws EvaluationException {
    if (operand instanceof ThreeDEval) {
        ThreeDEval ae = (ThreeDEval) operand;
        for (int sIx = ae.getFirstSheetIndex(); sIx <= ae.getLastSheetIndex(); sIx++) {
            int width = ae.getWidth();
            int height = ae.getHeight();
            for (int rrIx = 0; rrIx < height; rrIx++) {
                for (int rcIx = 0; rcIx < width; rcIx++) {
                    ValueEval ve = ae.getValue(sIx, rrIx, rcIx);
                    if (!isSubtotalCounted() && ae.isSubTotal(rrIx, rcIx))
                        continue;
                    collectValue(ve, true, temp);
                }
            }
        }
        return;
    }
    if (operand instanceof TwoDEval) {
        TwoDEval ae = (TwoDEval) operand;
        int width = ae.getWidth();
        int height = ae.getHeight();
        for (int rrIx = 0; rrIx < height; rrIx++) {
            for (int rcIx = 0; rcIx < width; rcIx++) {
                ValueEval ve = ae.getValue(rrIx, rcIx);
                if (!isSubtotalCounted() && ae.isSubTotal(rrIx, rcIx))
                    continue;
                collectValue(ve, true, temp);
            }
        }
        return;
    }
    if (operand instanceof RefEval) {
        RefEval re = (RefEval) operand;
        for (int sIx = re.getFirstSheetIndex(); sIx <= re.getLastSheetIndex(); sIx++) {
            collectValue(re.getInnerValueEval(sIx), true, temp);
        }
        return;
    }
    collectValue(operand, false, temp);
}
Also used : TwoDEval(org.apache.poi.ss.formula.TwoDEval) ThreeDEval(org.apache.poi.ss.formula.ThreeDEval) RefEval(org.apache.poi.ss.formula.eval.RefEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringValueEval(org.apache.poi.ss.formula.eval.StringValueEval)

Example 5 with ValueEval

use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.

the class Sumproduct method throwFirstError.

private static void throwFirstError(TwoDEval areaEval) throws EvaluationException {
    int height = areaEval.getHeight();
    int width = areaEval.getWidth();
    for (int rrIx = 0; rrIx < height; rrIx++) {
        for (int rcIx = 0; rcIx < width; rcIx++) {
            ValueEval ve = areaEval.getValue(rrIx, rcIx);
            if (ve instanceof ErrorEval) {
                throw new EvaluationException((ErrorEval) ve);
            }
        }
    }
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Aggregations

ValueEval (org.apache.poi.ss.formula.eval.ValueEval)223 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)101 StringEval (org.apache.poi.ss.formula.eval.StringEval)48 Test (org.junit.Test)39 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)36 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)29 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)28 OperationEvaluationContext (org.apache.poi.ss.formula.OperationEvaluationContext)27 Calendar (java.util.Calendar)16 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)13 Date (java.util.Date)11 RefEval (org.apache.poi.ss.formula.eval.RefEval)9 TwoDEval (org.apache.poi.ss.formula.TwoDEval)7 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)4 NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)4 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)4 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)4 ArrayList (java.util.ArrayList)3 AssertionFailedError (junit.framework.AssertionFailedError)3 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)3