From 368a2de04773eca5d37358a96d50c07738dc00c3 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 27 Jun 2012 14:50:22 +0000 Subject: [PATCH] Bug 53476 - Support Complex Name in formulas git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1354550 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../poi/ss/formula/WorkbookEvaluator.java | 8 ++-- .../poi/ss/formula/TestWorkbookEvaluator.java | 46 +++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index c05e507ff..6dbea32ac 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 53476 - Support Complex Name in formulas 53414 - properly update sheet dimensions when adding column Add File based constructor to OPCPackage, alongside existing String one (which constructed a File from the string internally) 53389 - Handle formatting General and @ formats even if a locale is prefixed to them diff --git a/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java b/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java index 4e45a76a3..79676d6a0 100644 --- a/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java +++ b/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java @@ -637,10 +637,10 @@ public final class WorkbookEvaluator { * YK: Used by OperationEvaluationContext to resolve indirect names. */ /*package*/ ValueEval evaluateNameFormula(Ptg[] ptgs, OperationEvaluationContext ec) { - if (ptgs.length > 1) { - throw new RuntimeException("Complex name formulas not supported yet"); - } - return getEvalForPtg(ptgs[0], ec); + if (ptgs.length == 1) { + return getEvalForPtg(ptgs[0], ec); + } + return evaluateFormula(ec, ptgs); } /** diff --git a/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java b/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java index 8303e0860..712335f85 100644 --- a/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java +++ b/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java @@ -42,6 +42,9 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.ErrorConstants; import org.apache.poi.ss.usermodel.FormulaEvaluator; +import org.apache.poi.ss.usermodel.Name; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** @@ -235,4 +238,47 @@ public class TestWorkbookEvaluator extends TestCase { assertEquals(Cell.CELL_TYPE_ERROR, cv.getCellType()); assertEquals(ErrorEval.CIRCULAR_REF_ERROR.getErrorCode(), cv.getErrorValue()); } + + + /** + * formulas with defined names. + */ + public void testNamesInFormulas() { + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("Sheet1"); + + Name name1 = wb.createName(); + name1.setNameName("aConstant"); + name1.setRefersToFormula("3.14"); + + Name name2 = wb.createName(); + name2.setNameName("aFormula"); + name2.setRefersToFormula("SUM(Sheet1!$A$1:$A$3)"); + + Name name3 = wb.createName(); + name3.setNameName("aSet"); + name3.setRefersToFormula("Sheet1!$A$2:$A$4"); + + + Row row0 = sheet.createRow(0); + Row row1 = sheet.createRow(1); + Row row2 = sheet.createRow(2); + Row row3 = sheet.createRow(3); + row0.createCell(0).setCellValue(2); + row1.createCell(0).setCellValue(5); + row2.createCell(0).setCellValue(3); + row3.createCell(0).setCellValue(7); + + row0.createCell(2).setCellFormula("aConstant"); + row1.createCell(2).setCellFormula("aFormula"); + row2.createCell(2).setCellFormula("SUM(aSet)"); + row3.createCell(2).setCellFormula("aConstant+aFormula+SUM(aSet)"); + + FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); + assertEquals(3.14, fe.evaluate(row0.getCell(2)).getNumberValue()); + assertEquals(10.0, fe.evaluate(row1.getCell(2)).getNumberValue()); + assertEquals(15.0, fe.evaluate(row2.getCell(2)).getNumberValue()); + assertEquals(28.14, fe.evaluate(row3.getCell(2)).getNumberValue()); + } + }