Search in sources :

Example 11 with StringEval

use of org.apache.poi.ss.formula.eval.StringEval 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 12 with StringEval

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

the class TestSumif method testBasic.

public void testBasic() {
    ValueEval[] arg0values = new ValueEval[] { _30, _30, _40, _40, _50, _50 };
    ValueEval[] arg2values = new ValueEval[] { _30, _40, _50, _60, _60, _60 };
    AreaEval arg0;
    AreaEval arg2;
    arg0 = EvalFactory.createAreaEval("A3:B5", arg0values);
    arg2 = EvalFactory.createAreaEval("D1:E3", arg2values);
    confirm(60.0, arg0, new NumberEval(30.0));
    confirm(70.0, arg0, new NumberEval(30.0), arg2);
    confirm(100.0, arg0, new StringEval(">45"));
    confirm(100.0, arg0, new StringEval(">=45"));
    confirm(100.0, arg0, new StringEval(">=50.0"));
    confirm(140.0, arg0, new StringEval("<45"));
    confirm(140.0, arg0, new StringEval("<=45"));
    confirm(140.0, arg0, new StringEval("<=40.0"));
    confirm(160.0, arg0, new StringEval("<>40.0"));
    confirm(80.0, arg0, new StringEval("=40.0"));
}
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) AreaEval(org.apache.poi.ss.formula.eval.AreaEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 13 with StringEval

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

the class TestText method testTextWithDeciamlFormatSecondArg.

@Test
public void testTextWithDeciamlFormatSecondArg() {
    ValueEval numArg = new NumberEval(321321.321);
    ValueEval formatArg = new StringEval("#,###.00000");
    ValueEval[] args = { numArg, formatArg };
    ValueEval result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
    DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale());
    char groupSeparator = dfs.getGroupingSeparator();
    char decimalSeparator = dfs.getDecimalSeparator();
    ValueEval testResult = new StringEval("321" + groupSeparator + "321" + decimalSeparator + "32100");
    assertEquals(testResult.toString(), result.toString());
    numArg = new NumberEval(321.321);
    formatArg = new StringEval("00000.00000");
    args[0] = numArg;
    args[1] = formatArg;
    result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
    testResult = new StringEval("00321" + decimalSeparator + "32100");
    assertEquals(testResult.toString(), result.toString());
    formatArg = new StringEval("$#.#");
    args[1] = formatArg;
    result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
    testResult = new StringEval("$321" + decimalSeparator + "3");
    assertEquals(testResult.toString(), result.toString());
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval) Test(org.junit.Test)

Example 14 with StringEval

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

the class TestText method testTextWithStringFirstArg.

@Test
public void testTextWithStringFirstArg() {
    ValueEval strArg = new StringEval("abc");
    ValueEval formatArg = new StringEval("abc");
    ValueEval[] args = { strArg, formatArg };
    ValueEval result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
    assertEquals(ErrorEval.VALUE_INVALID, result);
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) Test(org.junit.Test)

Example 15 with StringEval

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

the class TestTrim method testBasic.

public void testBasic() {
    confirmTrim(new StringEval(" hi "), "hi");
    confirmTrim(new StringEval("hi "), "hi");
    confirmTrim(new StringEval("  hi"), "hi");
    confirmTrim(new StringEval(" hi there  "), "hi there");
    confirmTrim(new StringEval(""), "");
    confirmTrim(new StringEval("   "), "");
}
Also used : StringEval(org.apache.poi.ss.formula.eval.StringEval)

Aggregations

StringEval (org.apache.poi.ss.formula.eval.StringEval)58 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)48 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)29 Test (org.junit.Test)18 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)13 Calendar (java.util.Calendar)10 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)10 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)9 Date (java.util.Date)8 I_MatchPredicate (org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate)7 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)5 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)3 RefEval (org.apache.poi.ss.formula.eval.RefEval)3 BigDecimal (java.math.BigDecimal)1 DateFormatSymbols (java.text.DateFormatSymbols)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 NumberFormat (java.text.NumberFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1