From b7c061f07a60267c0d711bbe0aadfc26d16a842a Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 10 Jan 2013 15:08:22 +0000 Subject: [PATCH] Patch from Johan from bug #54402 - fix IfError with indirect references git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1431403 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/formula/atp/IfError.java | 8 +++++--- .../apache/poi/ss/formula/atp/TestIfError.java | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/poi/ss/formula/atp/IfError.java b/src/java/org/apache/poi/ss/formula/atp/IfError.java index 5b7e21b8d..d3a22661b 100644 --- a/src/java/org/apache/poi/ss/formula/atp/IfError.java +++ b/src/java/org/apache/poi/ss/formula/atp/IfError.java @@ -18,6 +18,7 @@ package org.apache.poi.ss.formula.atp; import org.apache.poi.ss.formula.OperationEvaluationContext; +import org.apache.poi.ss.formula.WorkbookEvaluator; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.EvaluationException; import org.apache.poi.ss.formula.eval.ValueEval; @@ -47,7 +48,7 @@ final class IfError implements FreeRefFunction { ValueEval val; try { - val = evaluateArgParity(args[0], args[1], ec.getRowIndex(), ec.getColumnIndex()); + val = evaluateInternal(args[0], args[1], ec.getRowIndex(), ec.getColumnIndex()); } catch (EvaluationException e) { return e.getErrorEval(); } @@ -55,11 +56,12 @@ final class IfError implements FreeRefFunction { return val; } - private static ValueEval evaluateArgParity(ValueEval arg, ValueEval iferror, int srcCellRow, int srcCellCol) throws EvaluationException { + private static ValueEval evaluateInternal(ValueEval arg, ValueEval iferror, int srcCellRow, int srcCellCol) throws EvaluationException { + arg = WorkbookEvaluator.dereferenceResult(arg, srcCellRow, srcCellCol); if(arg instanceof ErrorEval) { return iferror; } else { return arg; } } -} \ No newline at end of file +} diff --git a/src/testcases/org/apache/poi/ss/formula/atp/TestIfError.java b/src/testcases/org/apache/poi/ss/formula/atp/TestIfError.java index bcaafda50..7a70fee26 100644 --- a/src/testcases/org/apache/poi/ss/formula/atp/TestIfError.java +++ b/src/testcases/org/apache/poi/ss/formula/atp/TestIfError.java @@ -36,6 +36,7 @@ public class TestIfError extends TestCase { /** * =IFERROR(210/35,\"Error in calculation\")" Divides 210 by 35 and returns 6.0 * =IFERROR(55/0,\"Error in calculation\")" Divides 55 by 0 and returns the error text + * =IFERROR(C1,\"Error in calculation\")" References the result of dividing 55 by 0 and returns the error text */ public static void testEvaluate(){ Workbook wb = new HSSFWorkbook(); @@ -46,6 +47,7 @@ public class TestIfError extends TestCase { // Create cells row1.createCell(0, Cell.CELL_TYPE_NUMERIC); row1.createCell(1, Cell.CELL_TYPE_NUMERIC); + row1.createCell(2, Cell.CELL_TYPE_NUMERIC); row2.createCell(0, Cell.CELL_TYPE_NUMERIC); row2.createCell(1, Cell.CELL_TYPE_NUMERIC); @@ -54,18 +56,22 @@ public class TestIfError extends TestCase { CellReference a2 = new CellReference("A2"); CellReference b1 = new CellReference("B1"); CellReference b2 = new CellReference("B2"); + CellReference c1 = new CellReference("C1"); // Set values sh.getRow(a1.getRow()).getCell(a1.getCol()).setCellValue(210); sh.getRow(a2.getRow()).getCell(a2.getCol()).setCellValue(55); sh.getRow(b1.getRow()).getCell(b1.getCol()).setCellValue(35); sh.getRow(b2.getRow()).getCell(b2.getCol()).setCellValue(0); + sh.getRow(c1.getRow()).getCell(c1.getCol()).setCellFormula("A1/B2"); Cell cell1 = sh.createRow(3).createCell(0); cell1.setCellFormula("IFERROR(A1/B1,\"Error in calculation\")"); Cell cell2 = sh.createRow(3).createCell(0); cell2.setCellFormula("IFERROR(A2/B2,\"Error in calculation\")"); - + Cell cell3 = sh.createRow(3).createCell(0); + cell3.setCellFormula("IFERROR(C1,\"error\")"); + double accuracy = 1E-9; FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); @@ -80,5 +86,10 @@ public class TestIfError extends TestCase { Cell.CELL_TYPE_STRING, evaluator.evaluate(cell2).getCellType()); assertEquals("Rounds -10 to a nearest multiple of -3 (-9)", "Error in calculation", evaluator.evaluate(cell2).getStringValue()); + + assertEquals("Check that C1 returns string", + Cell.CELL_TYPE_STRING, evaluator.evaluate(cell3).getCellType()); + assertEquals("Check that C1 returns string \"error\"", + "error", evaluator.evaluate(cell3).getStringValue()); } -} \ No newline at end of file +}