]> source.dussan.org Git - poi.git/commitdiff
change cell type to error when setting Double.NaN or Infinities, see Bugzilla 49761
authorYegor Kozlov <yegor@apache.org>
Sat, 4 Sep 2010 12:33:09 +0000 (12:33 +0000)
committerYegor Kozlov <yegor@apache.org>
Sat, 4 Sep 2010 12:33:09 +0000 (12:33 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@992591 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java

index ecd28631be66ff819cd5ed4770647430b690e03d..a4ffb04682c8b5625a35bc872e384613289fc344 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta3" date="2010-??-??">
+           <action dev="poi-developers" type="fix">49761 - change cell type to error when setting Double.NaN or Infinities</action>
            <action dev="poi-developers" type="fix">49833 - ensure that CTNumPr is included in poi-ooxml-schemas.jar</action>
            <action dev="POI-DEVELOPERS" type="fix">49841 - fixed LEFT and RIGHT to return #VALUE! when called with a negative operand </action>
            <action dev="POI-DEVELOPERS" type="fix">49783 - fixed evaluation of XSSF workbooks containing formulas with reference errors (#REF!)</action>
index 35d09c6b6dd6c5009689f54c3175bc64ea491b3d..2862a0d03279116f954c6ea6b316510341b8668e 100644 (file)
@@ -46,11 +46,7 @@ import org.apache.poi.hssf.record.common.UnicodeString;
 import org.apache.poi.hssf.record.formula.ExpPtg;
 import org.apache.poi.hssf.record.formula.Ptg;
 import org.apache.poi.hssf.record.formula.eval.ErrorEval;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.Comment;
-import org.apache.poi.ss.usermodel.Hyperlink;
-import org.apache.poi.ss.usermodel.RichTextString;
+import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.ss.util.NumberToTextConverter;
@@ -464,20 +460,31 @@ public class HSSFCell implements Cell {
      *        will change the cell to a numeric cell and set its value.
      */
     public void setCellValue(double value) {
-        int row=_record.getRow();
-        short col=_record.getColumn();
-        short styleIndex=_record.getXFIndex();
-
-        switch (_cellType) {
-            default:
-                setCellType(CELL_TYPE_NUMERIC, false, row, col, styleIndex);
-            case CELL_TYPE_NUMERIC:
-                (( NumberRecord ) _record).setValue(value);
-                break;
-            case CELL_TYPE_FORMULA:
-                ((FormulaRecordAggregate)_record).setCachedDoubleResult(value);
-                break;
+        if(Double.isInfinite(value)) {
+            // Excel does not support positive/negative infinities,
+            // rather, it gives a #DIV/0! error in these cases.
+            setCellErrorValue(FormulaError.DIV0.getCode());
+        } else if (Double.isNaN(value)){
+            // Excel does not support Not-a-Number (NaN),
+            // instead it immediately generates a #NUM! error.
+            setCellErrorValue(FormulaError.NUM.getCode());
+        } else {
+            int row=_record.getRow();
+            short col=_record.getColumn();
+            short styleIndex=_record.getXFIndex();
+
+            switch (_cellType) {
+                default:
+                    setCellType(CELL_TYPE_NUMERIC, false, row, col, styleIndex);
+                case CELL_TYPE_NUMERIC:
+                    (( NumberRecord ) _record).setValue(value);
+                    break;
+                case CELL_TYPE_FORMULA:
+                    ((FormulaRecordAggregate)_record).setCachedDoubleResult(value);
+                    break;
+            }
         }
+
     }
 
     /**
index 374c6b00d277bbcbc8630a04eb8b987f10566cd2..cee581daf865a213377b321042591f4e0a771cb3 100644 (file)
@@ -218,7 +218,14 @@ public final class XSSFCell implements Cell {
      *        will change the cell to a numeric cell and set its value.
      */
     public void setCellValue(double value) {
-        if(Double.isInfinite(value) || Double.isNaN(value)) {
+        if(Double.isInfinite(value)) {
+            // Excel does not support positive/negative infinities,
+            // rather, it gives a #DIV/0! error in these cases.
+            _cell.setT(STCellType.E);
+            _cell.setV(FormulaError.DIV0.getString());
+        } else if (Double.isNaN(value)){
+            // Excel does not support Not-a-Number (NaN),
+            // instead it immediately generates an #NUM! error.
             _cell.setT(STCellType.E);
             _cell.setV(FormulaError.NUM.getString());
         } else {
index 77e95c622a12a5d424c8cd542a9d22afebca9721..76f53c9c3785c50049822898ba163b47e04ea4de 100644 (file)
@@ -320,4 +320,6 @@ public final class TestHSSFCell extends BaseTestCell {
                        assertEquals("The maximum length of cell contents (text) is 32,767 characters", e.getMessage());
                }
        }
+
+
 }
index 39d21dd85fa67f2d1f05bc5bd6037f638fd26c84..dbbad380d4f227aa1ef42cbd38ab7b5a4d22e47e 100644 (file)
@@ -489,4 +489,59 @@ public abstract class BaseTestCell extends TestCase {
         int i2 = cell.getCellStyle().getIndex();
         assertEquals(i1, i2);
     }
+
+    /**
+     * Excel's implementation of floating number arithmetic does not fully adhere to IEEE 754:
+     *
+     * From http://support.microsoft.com/kb/78113:
+     *
+     * <ul>
+     * <li> Positive/Negative Infinities:
+     *   Infinities occur when you divide by 0. Excel does not support infinities, rather,
+     *   it gives a #DIV/0! error in these cases.
+     * </li>
+     * <li>
+     *   Not-a-Number (NaN):
+     *   NaN is used to represent invalid operations (such as infinity/infinity, 
+     *   infinity-infinity, or the square root of -1). NaNs allow a program to
+     *   continue past an invalid operation. Excel instead immediately generates
+     *   an error such as #NUM! or #DIV/0!.
+     * </li>
+     * </ul>
+     */
+    public void testNanAndInfinity() {
+        Workbook wb = _testDataProvider.createWorkbook();
+        Sheet workSheet = wb.createSheet("Sheet1");
+        Row row = workSheet.createRow(0);
+
+        Cell cell0 = row.createCell(0);
+        cell0.setCellValue(Double.NaN);
+        assertEquals("Double.NaN should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell0.getCellType());
+        assertEquals("Double.NaN should change cell value to #NUM!", ErrorConstants.ERROR_NUM, cell0.getErrorCellValue());
+
+        Cell cell1 = row.createCell(1);
+        cell1.setCellValue(Double.POSITIVE_INFINITY);
+        assertEquals("Double.POSITIVE_INFINITY should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell1.getCellType());
+        assertEquals("Double.POSITIVE_INFINITY should change cell value to #DIV/0!", ErrorConstants.ERROR_DIV_0, cell1.getErrorCellValue());
+
+        Cell cell2 = row.createCell(2);
+        cell2.setCellValue(Double.NEGATIVE_INFINITY);
+        assertEquals("Double.NEGATIVE_INFINITY should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell2.getCellType());
+        assertEquals("Double.NEGATIVE_INFINITY should change cell value to #DIV/0!", ErrorConstants.ERROR_DIV_0, cell2.getErrorCellValue());
+
+        wb = _testDataProvider.writeOutAndReadBack(wb);
+        row = wb.getSheetAt(0).getRow(0);
+
+        cell0 = row.getCell(0);
+        assertEquals(Cell.CELL_TYPE_ERROR, cell0.getCellType());
+        assertEquals(ErrorConstants.ERROR_NUM, cell0.getErrorCellValue());
+
+        cell1 = row.getCell(1);
+        assertEquals(Cell.CELL_TYPE_ERROR, cell1.getCellType());
+        assertEquals(ErrorConstants.ERROR_DIV_0, cell1.getErrorCellValue());
+
+        cell2 = row.getCell(2);
+        assertEquals(Cell.CELL_TYPE_ERROR, cell2.getCellType());
+        assertEquals(ErrorConstants.ERROR_DIV_0, cell2.getErrorCellValue());
+    }
 }