use of org.apache.poi.ss.formula.eval.NumberEval in project poi by apache.
the class Rate method evaluate.
public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
if (args.length < 3) {
//First 3 parameters are mandatory
return ErrorEval.VALUE_INVALID;
}
double periods, payment, present_val, future_val = 0, type = 0, estimate = 0.1, rate;
try {
ValueEval v1 = OperandResolver.getSingleValue(args[0], srcRowIndex, srcColumnIndex);
ValueEval v2 = OperandResolver.getSingleValue(args[1], srcRowIndex, srcColumnIndex);
ValueEval v3 = OperandResolver.getSingleValue(args[2], srcRowIndex, srcColumnIndex);
ValueEval v4 = null;
if (args.length >= 4)
v4 = OperandResolver.getSingleValue(args[3], srcRowIndex, srcColumnIndex);
ValueEval v5 = null;
if (args.length >= 5)
v5 = OperandResolver.getSingleValue(args[4], srcRowIndex, srcColumnIndex);
ValueEval v6 = null;
if (args.length >= 6)
v6 = OperandResolver.getSingleValue(args[5], srcRowIndex, srcColumnIndex);
periods = OperandResolver.coerceValueToDouble(v1);
payment = OperandResolver.coerceValueToDouble(v2);
present_val = OperandResolver.coerceValueToDouble(v3);
if (args.length >= 4)
future_val = OperandResolver.coerceValueToDouble(v4);
if (args.length >= 5)
type = OperandResolver.coerceValueToDouble(v5);
if (args.length >= 6)
estimate = OperandResolver.coerceValueToDouble(v6);
rate = calculateRate(periods, payment, present_val, future_val, type, estimate);
checkValue(rate);
} catch (EvaluationException e) {
LOG.log(POILogger.ERROR, "Can't evaluate rate function", e);
return e.getErrorEval();
}
return new NumberEval(rate);
}
use of org.apache.poi.ss.formula.eval.NumberEval in project poi by apache.
the class Sumif method eval.
private static ValueEval eval(int srcRowIndex, int srcColumnIndex, ValueEval arg1, AreaEval aeRange, AreaEval aeSum) {
// TODO - junit to prove last arg must be srcColumnIndex and not srcRowIndex
I_MatchPredicate mp = Countif.createCriteriaPredicate(arg1, srcRowIndex, srcColumnIndex);
// handle empty cells
if (mp == null) {
return NumberEval.ZERO;
}
double result = sumMatchingCells(aeRange, mp, aeSum);
return new NumberEval(result);
}
use of org.apache.poi.ss.formula.eval.NumberEval in project poi by apache.
the class Sumproduct method evaluateAreaSumProduct.
private static ValueEval evaluateAreaSumProduct(ValueEval[] evalArgs) throws EvaluationException {
int maxN = evalArgs.length;
TwoDEval[] args = new TwoDEval[maxN];
try {
System.arraycopy(evalArgs, 0, args, 0, maxN);
} catch (ArrayStoreException e) {
// one of the other args was not an AreaRef
return ErrorEval.VALUE_INVALID;
}
TwoDEval firstArg = args[0];
int height = firstArg.getHeight();
// TODO - junit
int width = firstArg.getWidth();
// first check dimensions
if (!areasAllSameSize(args, height, width)) {
// but errors in individual cells take precedence
for (int i = 1; i < args.length; i++) {
throwFirstError(args[i]);
}
return ErrorEval.VALUE_INVALID;
}
double acc = 0;
for (int rrIx = 0; rrIx < height; rrIx++) {
for (int rcIx = 0; rcIx < width; rcIx++) {
double term = 1D;
for (int n = 0; n < maxN; n++) {
double val = getProductTerm(args[n].getValue(rrIx, rcIx), false);
term *= val;
}
acc += term;
}
}
return new NumberEval(acc);
}
use of org.apache.poi.ss.formula.eval.NumberEval in project poi by apache.
the class TestSubtotal method confirmSubtotal.
private static void confirmSubtotal(int function, double expected) {
ValueEval[] values = new ValueEval[TEST_VALUES0.length];
for (int i = 0; i < TEST_VALUES0.length; i++) {
values[i] = new NumberEval(TEST_VALUES0[i]);
}
AreaEval arg1 = EvalFactory.createAreaEval("C1:D5", values);
ValueEval[] args = { new NumberEval(function), arg1 };
ValueEval result = new Subtotal().evaluate(args, 0, 0);
assertEquals(NumberEval.class, result.getClass());
assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0);
}
use of org.apache.poi.ss.formula.eval.NumberEval in project poi by apache.
the class TestSumif method testCriteriaArgRange.
/**
* test for bug observed near svn r882931
*/
public void testCriteriaArgRange() {
ValueEval[] arg0values = new ValueEval[] { _50, _60, _50, _50, _50, _30 };
ValueEval[] arg1values = new ValueEval[] { _30, _40, _50, _60 };
AreaEval arg0;
AreaEval arg1;
ValueEval ve;
arg0 = EvalFactory.createAreaEval("A3:B5", arg0values);
// single row range
arg1 = EvalFactory.createAreaEval("A2:D2", arg1values);
// invoking from cell C1
ve = invokeSumif(0, 2, arg0, arg1);
if (ve instanceof NumberEval) {
NumberEval ne = (NumberEval) ve;
if (ne.getNumberValue() == 30.0) {
throw new AssertionFailedError("identified error in SUMIF - criteria arg not evaluated properly");
}
}
confirmDouble(200, ve);
arg0 = EvalFactory.createAreaEval("C1:D3", arg0values);
// single column range
arg1 = EvalFactory.createAreaEval("B1:B4", arg1values);
// invoking from cell A4
ve = invokeSumif(3, 0, arg0, arg1);
confirmDouble(60, ve);
}
Aggregations