From 5df572a3b6406d59e8556b3486d3cda562c9d465 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 25 Aug 2010 13:29:55 +0000 Subject: [PATCH] fixed evaluation of XSSF workbooks containing formulas with reference errors (#REF!), see Bugzilla 49783 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@989100 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/ss/formula/FormulaParser.java | 8 +++- .../apache/poi/ss/usermodel/FormulaError.java | 2 +- .../poi/xssf/usermodel/TestXSSFBugs.java | 35 +++++++++++++----- test-data/spreadsheet/49783.xlsx | Bin 0 -> 8947 bytes 5 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 test-data/spreadsheet/49783.xlsx diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 67f61e161..7668ebc6f 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49783 - fixed evaluation of XSSF workbooks containing formulas with reference errors (#REF!) 49751 - fixed fetching names of user defined styles in HSSFCellStyle.getUserStyleName() 48900 - support for protecting a XSSF workbook 49725 - fixed FormulaParser to correctly process defined names with underscore diff --git a/src/java/org/apache/poi/ss/formula/FormulaParser.java b/src/java/org/apache/poi/ss/formula/FormulaParser.java index 0f186e744..db46b53e8 100644 --- a/src/java/org/apache/poi/ss/formula/FormulaParser.java +++ b/src/java/org/apache/poi/ss/formula/FormulaParser.java @@ -417,8 +417,12 @@ public final class FormulaParser { SimpleRangePart part1 = parseSimpleRangePart(); if (part1 == null) { if (sheetIden != null) { - throw new FormulaParseException("Cell reference expected after sheet name at index " - + _pointer + "."); + if(look == '#'){ // error ref like MySheet!#REF! + return new ParseNode(ErrPtg.valueOf(parseErrorLiteral())); + } else { + throw new FormulaParseException("Cell reference expected after sheet name at index " + + _pointer + "."); + } } return parseNonRange(savePointer); } diff --git a/src/java/org/apache/poi/ss/usermodel/FormulaError.java b/src/java/org/apache/poi/ss/usermodel/FormulaError.java index c3c43301e..162f24aa4 100644 --- a/src/java/org/apache/poi/ss/usermodel/FormulaError.java +++ b/src/java/org/apache/poi/ss/usermodel/FormulaError.java @@ -57,7 +57,7 @@ public enum FormulaError { * OFFSET(A1,0,20000) will result in a #REF! error. *

*/ - REF(0x1D, "#REF!"), + REF(0x17, "#REF!"), /** * Intended to indicate when what looks like a name is used, but no such name has been defined. diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 5a6e250d9..e9daa37bf 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -24,15 +24,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagingURIHelper; -import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.DataFormatter; -import org.apache.poi.ss.usermodel.Font; -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; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; @@ -412,4 +404,29 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("Cycle", wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue()); } + + public void test49783() throws Exception { + Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49783.xlsx"); + Sheet sheet = wb.getSheetAt(0); + FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); + Cell cell; + + cell = sheet.getRow(0).getCell(0); + assertEquals("#REF!*#REF!", cell.getCellFormula()); + assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType()); + assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString()); + + Name nm1 = wb.getName("sale_1"); + assertNotNull("name sale_1 should be present", nm1); + assertEquals("Sheet1!#REF!", nm1.getRefersToFormula()); + Name nm2 = wb.getName("sale_2"); + assertNotNull("name sale_2 should be present", nm2); + assertEquals("Sheet1!#REF!", nm2.getRefersToFormula()); + + cell = sheet.getRow(1).getCell(0); + assertEquals("sale_1*sale_2", cell.getCellFormula()); + assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType()); + assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString()); + } + } diff --git a/test-data/spreadsheet/49783.xlsx b/test-data/spreadsheet/49783.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..09cda15b462d1c7688cd318745edb354acdf8eb0 GIT binary patch literal 8947 zcmeHMbyQW`)<1NYARUq-NJ@9gAr7T|G6>dpVl9=&^d***?fcglEhKz{BSMGyaEnU?35s1kZsRy3cR}2343h z_=j_Zh9GRI@Gs)L-F-YeVwbKces(&P{0c#fTIyb_ZO!h$Wy|$zl9y3dOWxOTtL`73S1h>-Z>Q*@N^5+yyB~p0k+)HX_2TA(n`7 z8VXiDH7m4FeP)smm9udk5KI87IHXABf_nZ>u9CNt+U{XpOljln5{mcbp62*i}9mS&k-$YaJ3=E(%@(nVuOzKH@w#fPMI1 zS@Psl83`PWKLZE=04V?!&eN9dUqtb6aJDgaaIpDtlKoB^IM{IpoBO{#S`r}EFwlhV z2ek%``Z~qin?(r-zr*yVm_s0z2?tRKYVOsyTq_`NqSH4A>tnVD!!}(ueb>Wc`XMss zJQ`y)IkCW+&5AZ06}SOxVMtI=T5ybwgbA!H+uP^aw=`480;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