]> source.dussan.org Git - poi.git/commitdiff
While working on bug 61148 I found that getCellType() and setCellErrorValue() in...
authorDominik Stadler <centic@apache.org>
Tue, 19 Sep 2017 12:47:13 +0000 (12:47 +0000)
committerDominik Stadler <centic@apache.org>
Tue, 19 Sep 2017 12:47:13 +0000 (12:47 +0000)
Reformating some small things as well.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808874 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java

index 503dd8257a147deebdc33fad595c7bd091d541ec..5ca6b3b2ea1f952a19cac232c6a4c9dc70717861 100644 (file)
@@ -124,14 +124,21 @@ public class SXSSFCell implements Cell {
         ensureType(cellType);
     }
 
+    private boolean isFormulaCell() {
+        return _value instanceof FormulaValue;
+    }
+
     /**
      * Return the cell type.
      *
      * @return the cell type
      */
     @Override
-    public CellType getCellType()
-    {
+    public CellType getCellType() {
+        if (isFormulaCell()) {
+            return CellType.FORMULA;
+        }
+
         return _value.getType();
     }
     
@@ -158,7 +165,7 @@ public class SXSSFCell implements Cell {
      */
     @Override
     public CellType getCachedFormulaResultType() {
-        if (_value.getType() != CellType.FORMULA) {
+        if (!isFormulaCell()) {
             throw new IllegalStateException("Only formula cells have cached results");
         }
 
@@ -497,13 +504,18 @@ public class SXSSFCell implements Cell {
      * @see org.apache.poi.ss.usermodel.FormulaError
      */
     @Override
-    public void setCellErrorValue(byte value)
-    {
-        ensureType(CellType.ERROR);
-        if(_value.getType()==CellType.FORMULA)
-            ((ErrorFormulaValue)_value).setPreEvaluatedValue(value);
-        else
-            ((ErrorValue)_value).setValue(value);
+    public void setCellErrorValue(byte value) {
+        // for formulas, we want to keep the type and only have an ERROR as formula value
+        if(_value.getType()==CellType.FORMULA) {
+            // ensure that the type is "ERROR"
+            setFormulaType(CellType.ERROR);
+
+            // populate the value
+            ((ErrorFormulaValue) _value).setPreEvaluatedValue(value);
+        } else {
+            ensureType(CellType.ERROR);
+            ((ErrorValue) _value).setValue(value);
+        }
     }
 
     /**
@@ -827,13 +839,13 @@ public class SXSSFCell implements Cell {
     {
         if(_value.getType()!=CellType.STRING
            ||((StringValue)_value).isRichText())
-            _value=new PlainStringValue();
+            _value = new PlainStringValue();
     }
     /*package*/ void ensureRichTextStringType()
     {
         if(_value.getType()!=CellType.STRING
            ||!((StringValue)_value).isRichText())
-            _value=new RichTextValue();
+            _value = new RichTextValue();
     }
     /*package*/ void ensureType(CellType type)
     {
@@ -879,7 +891,7 @@ public class SXSSFCell implements Cell {
         {
             case NUMERIC:
             {
-                _value=new NumericValue();
+                _value = new NumericValue();
                 break;
             }
             case STRING:
@@ -895,12 +907,12 @@ public class SXSSFCell implements Cell {
             }
             case FORMULA:
             {
-                _value=new NumericFormulaValue();
+                _value = new NumericFormulaValue();
                 break;
             }
             case BLANK:
             {
-                _value=new BlankValue();
+                _value = new BlankValue();
                 break;
             }
             case BOOLEAN:
@@ -916,7 +928,7 @@ public class SXSSFCell implements Cell {
             }
             case ERROR:
             {
-                _value=new ErrorValue();
+                _value = new ErrorValue();
                 break;
             }
             default:
@@ -932,22 +944,22 @@ public class SXSSFCell implements Cell {
         {
             case NUMERIC:
             {
-                _value=new NumericFormulaValue();
+                _value = new NumericFormulaValue();
                 break;
             }
             case STRING:
             {
-                _value=new StringFormulaValue();
+                _value = new StringFormulaValue();
                 break;
             }
             case BOOLEAN:
             {
-                _value=new BooleanFormulaValue();
+                _value = new BooleanFormulaValue();
                 break;
             }
             case ERROR:
             {
-                _value=new ErrorFormulaValue();
+                _value = new ErrorFormulaValue();
                 break;
             }
             default:
@@ -1041,12 +1053,12 @@ public class SXSSFCell implements Cell {
         Property _next;
         public Property(Object value)
         {
-            _value=value;
+            _value = value;
         }
         abstract int getType();
         void setValue(Object value)
         {
-            _value=value;
+            _value = value;
         }
         Object getValue()
         {
@@ -1090,7 +1102,7 @@ public class SXSSFCell implements Cell {
         }
         void setValue(double value)
         {
-            _value=value;
+            _value = value;
         }
         double getValue()
         {
@@ -1111,7 +1123,7 @@ public class SXSSFCell implements Cell {
         String _value;
         void setValue(String value)
         {
-            _value=value;
+            _value = value;
         }
         String getValue()
         {
@@ -1133,7 +1145,7 @@ public class SXSSFCell implements Cell {
         }
         void setValue(RichTextString value)
         {
-            _value=value;
+            _value = value;
         }
         RichTextString getValue()
         {
@@ -1154,7 +1166,7 @@ public class SXSSFCell implements Cell {
         }
         void setValue(String value)
         {
-            _value=value;
+            _value = value;
         }
         String getValue()
         {
@@ -1246,7 +1258,7 @@ public class SXSSFCell implements Cell {
         }
         void setValue(boolean value)
         {
-            _value=value;
+            _value = value;
         }
         boolean getValue()
         {
@@ -1262,7 +1274,7 @@ public class SXSSFCell implements Cell {
         }
         void setValue(byte value)
         {
-            _value=value;
+            _value = value;
         }
         byte getValue()
         {
index b84b06bb363161562b3ac1ac9e33706180cc9779..29ef231173a7eeeb2e84e7ee2dcef358ef666376 100644 (file)
@@ -20,6 +20,7 @@
 package org.apache.poi.xssf.streaming;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 
 import java.io.IOException;
 
@@ -71,6 +72,7 @@ public class TestSXSSFCell extends BaseTestXCell {
             XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
 
             CTRst is = xCell.getCTCell().getIs();
+            assertNotNull(is);
             XmlCursor c = is.newCursor();
             c.toNextToken();
             String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
index 03a6a8e0a43e0bbf13f58684eb23b2d63be7a74d..18dbf79f3c16c275558033eeb83f74714f8dcfeb 100644 (file)
@@ -1014,4 +1014,46 @@ public abstract class BaseTestCell {
 
         wb.close();
     }
+
+    @Test
+    public void testSetErrorValue() throws Exception {
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet sheet = wb.createSheet();
+            Row row = sheet.createRow(0);
+            Cell cell = row.createCell(0);
+
+            cell.setCellFormula("A2");
+            cell.setCellErrorValue(FormulaError.NAME.getCode());
+
+            assertEquals(CellType.FORMULA, cell.getCellType());
+            assertEquals(CellType.ERROR, cell.getCachedFormulaResultType());
+            assertEquals("A2", cell.getCellFormula());
+            try {
+                cell.getNumericCellValue();
+                fail("Should catch exception here");
+            } catch (IllegalStateException e) {
+                // expected here
+            }
+            try {
+                cell.getStringCellValue();
+                fail("Should catch exception here");
+            } catch (IllegalStateException e) {
+                // expected here
+            }
+            try {
+                cell.getRichStringCellValue();
+                fail("Should catch exception here");
+            } catch (IllegalStateException e) {
+                // expected here
+            }
+            try {
+                cell.getDateCellValue();
+                fail("Should catch exception here");
+            } catch (IllegalStateException e) {
+                // expected here
+            }
+            assertEquals(FormulaError.NAME.getCode(), cell.getErrorCellValue());
+            assertNull(cell.getHyperlink());
+        }
+    }
 }