use of org.apache.poi.ss.formula.eval.EvaluationException 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();
}
}
use of org.apache.poi.ss.formula.eval.EvaluationException in project poi by apache.
the class TestMirr method testMirr.
public void testMirr() {
Mirr mirr = new Mirr();
double mirrValue;
double financeRate = 0.12;
double reinvestRate = 0.1;
double[] values = { -120000d, 39000d, 30000d, 21000d, 37000d, 46000d, reinvestRate, financeRate };
try {
mirrValue = mirr.evaluate(values);
} catch (EvaluationException e) {
throw new AssertionFailedError("MIRR should not failed with these parameters" + e);
}
assertEquals("mirr", 0.126094130366, mirrValue, 0.0000000001);
reinvestRate = 0.05;
financeRate = 0.08;
values = new double[] { -7500d, 3000d, 5000d, 1200d, 4000d, reinvestRate, financeRate };
try {
mirrValue = mirr.evaluate(values);
} catch (EvaluationException e) {
throw new AssertionFailedError("MIRR should not failed with these parameters" + e);
}
assertEquals("mirr", 0.18736225093, mirrValue, 0.0000000001);
reinvestRate = 0.065;
financeRate = 0.1;
values = new double[] { -10000, 3400d, 6500d, 1000d, reinvestRate, financeRate };
try {
mirrValue = mirr.evaluate(values);
} catch (EvaluationException e) {
throw new AssertionFailedError("MIRR should not failed with these parameters" + e);
}
assertEquals("mirr", 0.07039493966, mirrValue, 0.0000000001);
reinvestRate = 0.07;
financeRate = 0.01;
values = new double[] { -10000d, -3400d, -6500d, -1000d, reinvestRate, financeRate };
try {
mirrValue = mirr.evaluate(values);
} catch (EvaluationException e) {
throw new AssertionFailedError("MIRR should not failed with these parameters" + e);
}
assertEquals("mirr", -1, mirrValue, 0.0);
}
use of org.apache.poi.ss.formula.eval.EvaluationException in project poi by apache.
the class CalculateMortgage method evaluate.
@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
// verify that we have enough data
if (args.length != 3) {
return ErrorEval.VALUE_INVALID;
}
// declare doubles for values
double principal, rate, years, result;
try {
// extract values as ValueEval
ValueEval v1 = OperandResolver.getSingleValue(args[0], ec.getRowIndex(), ec.getColumnIndex());
ValueEval v2 = OperandResolver.getSingleValue(args[1], ec.getRowIndex(), ec.getColumnIndex());
ValueEval v3 = OperandResolver.getSingleValue(args[2], ec.getRowIndex(), ec.getColumnIndex());
// get data as doubles
principal = OperandResolver.coerceValueToDouble(v1);
rate = OperandResolver.coerceValueToDouble(v2);
years = OperandResolver.coerceValueToDouble(v3);
result = calculateMortgagePayment(principal, rate, years);
System.out.println("Result = " + result);
checkValue(result);
} catch (EvaluationException e) {
return e.getErrorEval();
}
return new NumberEval(result);
}
use of org.apache.poi.ss.formula.eval.EvaluationException in project poi by apache.
the class DateParser method parseDate.
/**
* Parses a date from a string.
*
* @param strVal a string with a date pattern.
* @return a date parsed from argument.
* @throws EvaluationException exception upon parsing.
*/
public static Calendar parseDate(String strVal) throws EvaluationException {
String[] parts = Pattern.compile("/").split(strVal);
if (parts.length != 3) {
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
String part2 = parts[2];
int spacePos = part2.indexOf(' ');
if (spacePos > 0) {
// drop time portion if present
part2 = part2.substring(0, spacePos);
}
int f0;
int f1;
int f2;
try {
f0 = Integer.parseInt(parts[0]);
f1 = Integer.parseInt(parts[1]);
f2 = Integer.parseInt(part2);
} catch (NumberFormatException e) {
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
if (f0 < 0 || f1 < 0 || f2 < 0 || (f0 > 12 && f1 > 12 && f2 > 12)) {
// easy to see this cannot be a valid date
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
if (f0 >= 1900 && f0 < 9999) {
// when 4 digit value appears first, the format is YYYY/MM/DD, regardless of OS settings
return makeDate(f0, f1, f2);
}
// TODO - find a way to choose the correct date format
throw new RuntimeException("Unable to determine date format for text '" + strVal + "'");
}
use of org.apache.poi.ss.formula.eval.EvaluationException in project poi by apache.
the class Choose method evaluate.
public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
if (args.length < 2) {
return ErrorEval.VALUE_INVALID;
}
try {
int ix = evaluateFirstArg(args[0], srcRowIndex, srcColumnIndex);
if (ix < 1 || ix >= args.length) {
return ErrorEval.VALUE_INVALID;
}
ValueEval result = OperandResolver.getSingleValue(args[ix], srcRowIndex, srcColumnIndex);
if (result == MissingArgEval.instance) {
return BlankEval.instance;
}
return result;
} catch (EvaluationException e) {
return e.getErrorEval();
}
}
Aggregations