You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2010/06/04 17:58:02 UTC
svn commit: r951466 - in /poi/trunk/src: documentation/content/xdocs/
java/org/apache/poi/hssf/usermodel/ java/org/apache/poi/ss/usermodel/
ooxml/java/org/apache/poi/xssf/usermodel/
ooxml/testcases/org/apache/poi/xssf/usermodel/
Author: nick
Date: Fri Jun 4 15:58:02 2010
New Revision: 951466
URL: http://svn.apache.org/viewvc?rev=951466&view=rev
Log:
Add tests to verify that XSSF and HSSF do the same thing with retrieving the wrong type of value from string/numberic/formula cells, and tweak documentation to match the long standing behaviour (bug #47815)
Modified:
poi/trunk/src/documentation/content/xdocs/status.xml
poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
poi/trunk/src/java/org/apache/poi/ss/usermodel/Cell.java
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.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=951466&r1=951465&r2=951466&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Fri Jun 4 15:58:02 2010
@@ -34,6 +34,7 @@
<changes>
<release version="3.7-SNAPSHOT" date="2010-??-??">
+ <action dev="POI-DEVELOPERS" type="fix">47815 - correct documentation on what happens when you request a String from a non-string Formula cell</action>
<action dev="POI-DEVELOPERS" type="fix">49386 - avoid NPE when extracting OOXML file properties which are dates</action>
<action dev="POI-DEVELOPERS" type="fix">49377 - only call DecimalFormat.setRoundingMode on Java 1.6 - it's needed to match excel's rendering of numbers</action>
<action dev="POI-DEVELOPERS" type="fix">49378 - correct 1.6ism</action>
Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=951466&r1=951465&r2=951466&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Fri Jun 4 15:58:02 2010
@@ -700,7 +700,7 @@ public class HSSFCell implements Cell {
/**
* get the value of the cell as a string - for numeric cells we throw an exception.
* For blank cells we return an empty string.
- * For formulaCells that are not string Formulas, we return empty String
+ * For formulaCells that are not string Formulas, we throw an exception
*/
public String getStringCellValue()
{
@@ -711,7 +711,7 @@ public class HSSFCell implements Cell {
/**
* get the value of the cell as a string - for numeric cells we throw an exception.
* For blank cells we return an empty string.
- * For formulaCells that are not string Formulas, we return empty String
+ * For formulaCells that are not string Formulas, we throw an exception
*/
public HSSFRichTextString getRichStringCellValue() {
Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/Cell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/Cell.java?rev=951466&r1=951465&r2=951466&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/Cell.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/Cell.java Fri Jun 4 15:58:02 2010
@@ -257,7 +257,7 @@ public interface Cell {
* Get the value of the cell as a XSSFRichTextString
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
- * For formula cells we return the pre-calculated value.
+ * For formula cells we return the pre-calculated value if a string, otherwise an exception.
* </p>
* @return the value of the cell as a XSSFRichTextString
*/
@@ -267,7 +267,7 @@ public interface Cell {
* Get the value of the cell as a string
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
- * For formulaCells that are not string Formulas, we return empty String.
+ * For formulaCells that are not string Formulas, we throw an exception.
* </p>
* @return the value of the cell as a string
*/
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java?rev=951466&r1=951465&r2=951466&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java Fri Jun 4 15:58:02 2010
@@ -195,7 +195,15 @@ public final class XSSFCell implements C
return 0.0;
case CELL_TYPE_FORMULA:
case CELL_TYPE_NUMERIC:
- return _cell.isSetV() ? Double.parseDouble(_cell.getV()) : 0.0;
+ if(_cell.isSetV()) {
+ try {
+ return Double.parseDouble(_cell.getV());
+ } catch(NumberFormatException e) {
+ throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_STRING, false);
+ }
+ } else {
+ return 0.0;
+ }
default:
throw typeMismatch(CELL_TYPE_NUMERIC, cellType, false);
}
@@ -223,7 +231,7 @@ public final class XSSFCell implements C
* Get the value of the cell as a string
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
- * For formulaCells that are not string Formulas, we return empty String.
+ * For formulaCells that are not string Formulas, we throw an exception
* </p>
* @return the value of the cell as a string
*/
@@ -236,7 +244,7 @@ public final class XSSFCell implements C
* Get the value of the cell as a XSSFRichTextString
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
- * For formula cells we return the pre-calculated value.
+ * For formula cells we return the pre-calculated value if a string, otherwise an exception
* </p>
* @return the value of the cell as a XSSFRichTextString
*/
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=951466&r1=951465&r2=951466&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 Fri Jun 4 15:58:02 2010
@@ -28,6 +28,7 @@ import org.apache.poi.ss.usermodel.BaseT
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;
@@ -330,4 +331,70 @@ public final class TestXSSFBugs extends
assertEquals("123", df.formatRawCellContents(123.0, -1, "@"));
assertEquals("123", df.formatRawCellContents(123.0, -1, "General"));
}
+
+ /**
+ * Ensures that XSSF and HSSF agree with each other,
+ * and with the docs on when fetching the wrong
+ * kind of value from a Formula cell
+ */
+ public void test47815() {
+ Workbook[] wbs = new Workbook[] {
+ new HSSFWorkbook(),
+ new XSSFWorkbook()
+ };
+ for(Workbook wb : wbs) {
+ Sheet s = wb.createSheet();
+ Row r = s.createRow(0);
+
+ // Setup
+ Cell cn = r.createCell(0, Cell.CELL_TYPE_NUMERIC);
+ cn.setCellValue(1.2);
+ Cell cs = r.createCell(1, Cell.CELL_TYPE_STRING);
+ cs.setCellValue("Testing");
+
+ Cell cfn = r.createCell(2, Cell.CELL_TYPE_FORMULA);
+ cfn.setCellFormula("A1");
+ Cell cfs = r.createCell(3, Cell.CELL_TYPE_FORMULA);
+ cfs.setCellFormula("B1");
+
+ FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
+ assertEquals(Cell.CELL_TYPE_NUMERIC, fe.evaluate(cfn).getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, fe.evaluate(cfs).getCellType());
+ fe.evaluateFormulaCell(cfn);
+ fe.evaluateFormulaCell(cfs);
+
+ // Now test
+ assertEquals(Cell.CELL_TYPE_NUMERIC, cn.getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, cs.getCellType());
+ assertEquals(Cell.CELL_TYPE_FORMULA, cfn.getCellType());
+ assertEquals(Cell.CELL_TYPE_NUMERIC, cfn.getCachedFormulaResultType());
+ assertEquals(Cell.CELL_TYPE_FORMULA, cfs.getCellType());
+ assertEquals(Cell.CELL_TYPE_STRING, cfs.getCachedFormulaResultType());
+
+ // Different ways of retrieving
+ assertEquals(1.2, cn.getNumericCellValue());
+ try {
+ cn.getRichStringCellValue();
+ fail();
+ } catch(IllegalStateException e) {}
+
+ assertEquals("Testing", cs.getStringCellValue());
+ try {
+ cs.getNumericCellValue();
+ fail();
+ } catch(IllegalStateException e) {}
+
+ assertEquals(1.2, cfn.getNumericCellValue());
+ try {
+ cfn.getRichStringCellValue();
+ fail();
+ } catch(IllegalStateException e) {}
+
+ assertEquals("Testing", cfs.getStringCellValue());
+ try {
+ cfs.getNumericCellValue();
+ fail();
+ } catch(IllegalStateException e) {}
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org