Bug 55032: Fix handling missing option values in financial functions PV, FV, NPER and PMT

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1706254 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2015-10-01 14:05:28 +00:00
parent 2de9d527b4
commit 030c29cfed
2 changed files with 52 additions and 4 deletions

View File

@ -20,6 +20,7 @@ package org.apache.poi.ss.formula.functions;
import org.apache.poi.ss.formula.eval.BoolEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.MissingArgEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
@ -63,10 +64,24 @@ public abstract class FinanceFunction implements Function3Arg, Function4Arg {
switch (args.length) {
case 3:
return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], DEFAULT_ARG3, DEFAULT_ARG4);
case 4:
return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], args[3], DEFAULT_ARG4);
case 5:
return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], args[3], args[4]);
case 4: {
ValueEval arg3 = args[3];
if(arg3 == MissingArgEval.instance) {
arg3 = DEFAULT_ARG3;
}
return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], arg3, DEFAULT_ARG4);
}
case 5: {
ValueEval arg3 = args[3];
if(arg3 == MissingArgEval.instance) {
arg3 = DEFAULT_ARG3;
}
ValueEval arg4 = args[4];
if(arg4 == MissingArgEval.instance) {
arg4 = DEFAULT_ARG4;
}
return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], arg3, arg4);
}
}
return ErrorEval.VALUE_INVALID;
}

View File

@ -29,7 +29,12 @@ import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
/**
@ -176,4 +181,32 @@ public final class TestFormulaBugs {
double value) {
sheet.createRow(rowIx).createCell(colIx).setCellValue(value);
}
@Test
public void test55032() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("input");
Row row = sheet.createRow(0);
Cell cell = row.createCell(1);
checkFormulaValue(wb, cell, "PV(0.08/12, 20*12, 500, ,0)", -59777.14585);
checkFormulaValue(wb, cell, "PV(0.08/12, 20*12, 500, ,)", -59777.14585);
checkFormulaValue(wb, cell, "PV(0.08/12, 20*12, 500, 500,)", -59878.6315455);
checkFormulaValue(wb, cell, "FV(0.08/12, 20*12, 500, ,)", -294510.2078107270);
checkFormulaValue(wb, cell, "PMT(0.08/12, 20*12, 500, ,)", -4.1822003450);
checkFormulaValue(wb, cell, "NPER(0.08/12, 20*12, 500, ,)", -2.0758873434);
wb.close();
}
private void checkFormulaValue(Workbook wb, Cell cell, String formula, double expectedValue) {
cell.setCellFormula(formula);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
CellValue value = evaluator.evaluate(cell);
assertEquals(expectedValue, value.getNumberValue(), 0.0001);
}
}