Author: nick
Date: Mon Dec 13 05:39:39 2010
New Revision: 1045021
URL: http://svn.apache.org/viewvc?rev=1045021&view=rev
Log:
Fix bug #50440 - Support evaluating formulas with newlines in them, which XSSF
may have (but HSSF may not)
Added:
poi/trunk/test-data/spreadsheet/NewlineInFormulas.xlsx (with props)
Modified:
poi/trunk/src/documentation/content/xdocs/status.xml
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL:
http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1045021&r1=1045020&r2=1045021&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Mon Dec 13 05:39:39
2010
@@ -34,6 +34,7 @@
<changes>
<release version="3.8-beta1" date="2010-??-??">
+ <action dev="POI-DEVELOPERS" type="fix">50440 - Support evaluating
formulas with newlines in them, which XSSF may have (but HSSF may not)</action>
<action dev="POI-DEVELOPERS" type="add">Added inline string support
to XSSF EventModel</action>
<action dev="POI-DEVELOPERS" type="fix">50246 - Properly position
GutsRecord when reading HSSF workbooks</action>
<action dev="POI-DEVELOPERS" type="add">48539 - Added
implementation for MROUND(), VAR() and VARP()</action>
Modified:
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java?rev=1045021&r1=1045020&r2=1045021&view=diff
==============================================================================
---
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java
(original)
+++
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java
Mon Dec 13 05:39:39 2010
@@ -147,12 +147,26 @@ public final class XSSFEvaluationWorkboo
public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
XSSFCell cell = ((XSSFEvaluationCell)evalCell).getXSSFCell();
XSSFEvaluationWorkbook frBook =
XSSFEvaluationWorkbook.create(_uBook);
- return FormulaParser.parse(cell.getCellFormula(), frBook,
FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
+ String formulaText =
cleanXSSFFormulaText(cell.getCellFormula());
+ return FormulaParser.parse(formulaText, frBook,
FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
}
public UDFFinder getUDFFinder(){
return _uBook.getUDFFinder();
}
+
+ /**
+ * XSSF allows certain extra textual characters in the formula that
+ * HSSF does not. As these can't be composed down to HSSF-compatible
+ * Ptgs, this method strips them out for us.
+ */
+ private String cleanXSSFFormulaText(String text) {
+ // Newlines are allowed in XSSF
+ text = text.replaceAll("\\n", "").replaceAll("\\r", "");
+
+ // All done with cleaning
+ return text;
+ }
private static final class Name implements EvaluationName {
Modified:
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1045021&r1=1045020&r2=1045021&view=diff
==============================================================================
---
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
(original)
+++
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
Mon Dec 13 05:39:39 2010
@@ -602,4 +602,22 @@ public final class TestXSSFBugs extends
}
}
}
+
+ /**
+ * Newlines are valid characters in a formula
+ */
+ public void test50440() throws Exception {
+ Workbook wb =
XSSFTestDataSamples.openSampleWorkbook("NewlineInFormulas.xlsx");
+ Sheet s = wb.getSheetAt(0);
+ Cell c = s.getRow(0).getCell(0);
+
+ assertEquals("SUM(\n1,2\n)", c.getCellFormula());
+ assertEquals(3.0, c.getNumericCellValue());
+
+ FormulaEvaluator formulaEvaluator =
wb.getCreationHelper().createFormulaEvaluator();
+ formulaEvaluator.evaluateFormulaCell(c);
+
+ assertEquals("SUM(\n1,2\n)", c.getCellFormula());
+ assertEquals(3.0, c.getNumericCellValue());
+ }
}
Added: poi/trunk/test-data/spreadsheet/NewlineInFormulas.xlsx
URL:
http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/NewlineInFormulas.xlsx?rev=1045021&view=auto
==============================================================================
Binary file - no diff available.
Propchange: poi/trunk/test-data/spreadsheet/NewlineInFormulas.xlsx
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]