]> source.dussan.org Git - poi.git/commitdiff
#58558 SXSSFCell.setCellValue((RichTextString)null) fixed to work like XSSF and HSSF...
authorNick Burch <nick@apache.org>
Wed, 28 Oct 2015 17:42:56 +0000 (17:42 +0000)
committerNick Burch <nick@apache.org>
Wed, 28 Oct 2015 17:42:56 +0000 (17:42 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1711082 13f79535-47bb-0310-9956-ffa450edef68

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

index 3e6dbc5432605875e4b417ee520cdb51b4f0fd9b..2e5e01f21708432af8f49c907c981f64219b7f1f 100644 (file)
@@ -222,16 +222,21 @@ public class SXSSFCell implements Cell {
      */
     public void setCellValue(RichTextString value)
     {
-        ensureRichTextStringType();
-
-        if(value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()){
-            throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
-        }
-
-        ((RichTextValue)_value).setValue(value);
+        XSSFRichTextString xvalue = (XSSFRichTextString)value;
         
-        if (((XSSFRichTextString)value).hasFormatting())
-            logger.log(POILogger.WARN, "SXSSF doesn't support Shared Strings, rich text formatting information has be lost");
+        if (xvalue != null) {
+            ensureRichTextStringType();
+            
+            if (xvalue.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
+                throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
+            }
+            if (xvalue.hasFormatting())
+                logger.log(POILogger.WARN, "SXSSF doesn't support Shared Strings, rich text formatting information has be lost");
+            
+            ((RichTextValue)_value).setValue(xvalue);
+        } else {
+            setCellType(CELL_TYPE_BLANK);
+        }
     }
 
     /**
index 20451d6f3571cffec96b94ac0e21c647d607d3a7..9cdaf3d18de08b29d0fda6d9f2582954b7f9a90a 100644 (file)
@@ -767,7 +767,39 @@ public abstract class BaseTestCell {
                wb1.close();
        }
 
-       private void checkUnicodeValues(Workbook wb) {
+    /**
+     * Setting a cell value of a null RichTextString should set
+     *  the cell to Blank, test case for 58558
+     */
+    @Test
+    public void testSetCellValueNullRichTextString() throws IOException {
+        Workbook wb = _testDataProvider.createWorkbook();
+        Sheet sheet = wb.createSheet();
+        Cell cell = sheet.createRow(0).createCell(0);
+
+        RichTextString nullStr = null;
+        cell.setCellValue(nullStr);
+        assertEquals("", cell.getStringCellValue());
+        assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
+
+        cell = sheet.createRow(0).createCell(1);
+        cell.setCellValue(1.2d);
+        assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
+        cell.setCellValue(nullStr);
+        assertEquals("", cell.getStringCellValue());
+        assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
+
+        cell = sheet.createRow(0).createCell(1);
+        cell.setCellValue(wb.getCreationHelper().createRichTextString("Test"));
+        assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
+        cell.setCellValue(nullStr);
+        assertEquals("", cell.getStringCellValue());
+        assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
+
+        wb.close();
+    }
+
+    private void checkUnicodeValues(Workbook wb) {
                assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 0 _x0046_ without changes" : "row 0, cell 0 F without changes"), 
                                wb.getSheetAt(0).getRow(0).getCell(0).toString());
                assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 1 _x005fx0046_ with changes" : "row 0, cell 1 _x005fx0046_ with changes"),