You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2016/07/27 18:15:55 UTC

svn commit: r1754328 - in /poi/trunk/src: ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java

Author: centic
Date: Wed Jul 27 18:15:54 2016
New Revision: 1754328

URL: http://svn.apache.org/viewvc?rev=1754328&view=rev
Log:
Bug 55384: Handle setting pre-evaluation string correctly in SXSSF as well

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java?rev=1754328&r1=1754327&r2=1754328&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java Wed Jul 27 18:15:54 2016
@@ -338,7 +338,11 @@ public class SXSSFCell implements Cell {
             }
     
             if(_value.getType()==CellType.FORMULA)
-                ((StringFormulaValue)_value).setPreEvaluatedValue(value);
+                if(_value instanceof NumericFormulaValue) {
+                    ((NumericFormulaValue) _value).setPreEvaluatedValue(Double.parseDouble(value));
+                } else {
+                    ((StringFormulaValue) _value).setPreEvaluatedValue(value);
+                }
             else
                 ((PlainStringValue)_value).setValue(value);
         } else {
@@ -956,6 +960,7 @@ public class SXSSFCell implements Cell {
     }
     /*package*/ void setFormulaType(CellType type)
     {
+        Value prevValue = _value;
         switch(type)
         {
             case NUMERIC:
@@ -983,6 +988,11 @@ public class SXSSFCell implements Cell {
                 throw new IllegalArgumentException("Illegal type " + type);
             }
         }
+
+        // if we had a Formula before, we should copy over the _value of the formula
+        if(prevValue instanceof FormulaValue) {
+            ((FormulaValue)_value)._value = ((FormulaValue)prevValue)._value;
+        }
     }
 //TODO: implement this correctly
     @NotImplemented

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java?rev=1754328&r1=1754327&r2=1754328&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java Wed Jul 27 18:15:54 2016
@@ -33,9 +33,12 @@ import java.awt.font.FontRenderContext;
 import java.awt.font.TextAttribute;
 import java.awt.font.TextLayout;
 import java.awt.geom.Rectangle2D;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.text.AttributedString;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import static org.junit.Assert.*;
@@ -1606,4 +1609,78 @@ public abstract class BaseTestBugzillaIs
         assertNull("Sheet0 after write", wb2.getPrintArea(0)); // CURRENTLY FAILS with "Sheet0!$A$1:$C$6"
         assertEquals("Sheet1 after write", "Sheet1!$A$1:$A$1", wb2.getPrintArea(1));
     }
-}
+
+
+    @Test
+    public void test55384() throws Exception {
+        Workbook wb = _testDataProvider.createWorkbook();
+        try {
+            Sheet sh = wb.createSheet();
+            for (int rownum = 0; rownum < 10; rownum++) {
+                org.apache.poi.ss.usermodel.Row row = sh.createRow(rownum);
+                for (int cellnum = 0; cellnum < 3; cellnum++) {
+                    Cell cell = row.createCell(cellnum);
+                    cell.setCellValue(rownum + cellnum);
+                }
+            }
+            Row row = sh.createRow(10);
+            // setting no precalculated value works just fine.
+            Cell cell1 = row.createCell(0);
+            cell1.setCellFormula("SUM(A1:A10)");
+
+            // but setting a precalculated STRING value fails totally in SXSSF
+            Cell cell2 = row.createCell(1);
+            cell2.setCellFormula("SUM(B1:B10)");
+            cell2.setCellValue("55");
+
+            // setting a precalculated int value works as expected
+            Cell cell3 = row.createCell(2);
+            cell3.setCellFormula("SUM(C1:C10)");
+            cell3.setCellValue(65);
+
+            assertEquals(CellType.FORMULA, cell1.getCellTypeEnum());
+            assertEquals(CellType.FORMULA, cell2.getCellTypeEnum());
+            assertEquals(CellType.FORMULA, cell3.getCellTypeEnum());
+
+            assertEquals("SUM(A1:A10)", cell1.getCellFormula());
+            assertEquals("SUM(B1:B10)", cell2.getCellFormula());
+            assertEquals("SUM(C1:C10)", cell3.getCellFormula());
+
+            /*String name = wb.getClass().getCanonicalName();
+            String ext = (wb instanceof HSSFWorkbook) ? ".xls" : ".xlsx";
+            OutputStream output = new FileOutputStream("/tmp" + name + ext);
+            try {
+                wb.write(output);
+            } finally {
+                output.close();
+            }*/
+
+            Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
+            checkFormulaPreevaluatedString(wbBack);
+            wbBack.close();
+        } finally {
+            wb.close();
+        }
+    }
+
+    private void checkFormulaPreevaluatedString(Workbook readFile) {
+        Sheet sheet = readFile.getSheetAt(0);
+        Row row = sheet.getRow(sheet.getLastRowNum());
+        assertEquals(10, row.getRowNum());
+
+        for (Cell cell : row) {
+            String cellValue = null;
+            switch (cell.getCellTypeEnum()) {
+                case STRING:
+                    cellValue = cell.getRichStringCellValue().getString();
+                    break;
+                case FORMULA:
+                    cellValue = cell.getCellFormula();
+                    break;
+            }
+            assertNotNull(cellValue);
+            cellValue = cellValue.isEmpty() ? null : cellValue;
+            assertNotNull(cellValue);
+        }
+    }
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org