poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ye...@apache.org
Subject svn commit: r819106 - in /poi/trunk: src/documentation/content/xdocs/status.xml src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java test-data/spreadsheet/47889.xlsx
Date Sat, 26 Sep 2009 10:41:01 GMT
Author: yegor
Date: Sat Sep 26 10:41:01 2009
New Revision: 819106

URL: http://svn.apache.org/viewvc?rev=819106&view=rev
Log:
fixed XSSFCell.getStringCellValue() to properly handle cached formula results

Added:
    poi/trunk/test-data/spreadsheet/47889.xlsx   (with props)
Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.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=819106&r1=819105&r2=819106&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Sat Sep 26 10:41:01 2009
@@ -33,6 +33,7 @@
 
     <changes>
         <release version="3.6-beta1" date="2009-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">47889 - fixed XSSFCell.getStringCellValue()
to properly handle cached formula results</action>
         </release>
         <release version="3.5-FINAL" date="2009-09-28">
            <action dev="POI-DEVELOPERS" type="fix">47747 - fixed logic for locating
shared formula records</action>

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=819106&r1=819105&r2=819106&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 Sat Sep 26 10:41:01
2009
@@ -249,6 +249,9 @@
                     } else {
                         rt = new XSSFRichTextString("");
                     }
+                } else if (_cell.getT() == STCellType.STR) {
+                    //cached formula value
+                    rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : "");
                 } else {
                     if (_cell.isSetV()) {
                         int idx = Integer.parseInt(_cell.getV());
@@ -411,12 +414,15 @@
     /**
      * Return the cell's style.
      *
-     * @return the cell's style. Always not-null. Default cell style has zero index and can
be obtained as
-     * <code>workbook.getCellStyleAt(0)</code>
+     * @return the cell's style.</code>
      */
     public XSSFCellStyle getCellStyle() {
-        long idx = _cell.isSetS() ? _cell.getS() : 0;
-        return _stylesSource.getStyleAt((int)idx);
+        XSSFCellStyle style = null;
+        if(_stylesSource.getNumCellStyles() > 0){
+            long idx = _cell.isSetS() ? _cell.getS() : 0;
+            style = _stylesSource.getStyleAt((int)idx);
+        }
+        return style;
     }
 
     /**
@@ -629,7 +635,7 @@
     private void setBlank(){
         CTCell blank = CTCell.Factory.newInstance();
         blank.setR(_cell.getR());
-        blank.setS(_cell.getS());
+        if(_cell.isSetS()) blank.setS(_cell.getS());
         _cell.set(blank);
     }
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java?rev=819106&r1=819105&r2=819106&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java Sat Sep
26 10:41:01 2009
@@ -21,6 +21,7 @@
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.model.SharedStringsTable;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
 
 /**
  * @author Yegor Kozlov
@@ -105,4 +106,64 @@
         assertEquals(0, sst.getCount());
         assertEquals(XSSFCell.CELL_TYPE_BLANK, cell_1.getCellType());
     }
+
+    public void testFormulaString() {
+        XSSFWorkbook wb = new XSSFWorkbook();
+        XSSFCell cell = wb.createSheet().createRow(0).createCell(0);
+        CTCell ctCell = cell.getCTCell(); //low-level bean holding cell's xml
+
+        cell.setCellFormula("A2");
+        assertEquals(XSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
+        //the value is not set and cell's type='N' which means blank
+        assertEquals(STCellType.N, ctCell.getT());
+
+        //set cached formula value
+        cell.setCellValue("t='str'");
+        //we are still of 'formula' type
+        assertEquals(XSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
+        //cached formula value is set and cell's type='STR'
+        assertEquals(STCellType.STR, ctCell.getT());
+        assertEquals("t='str'", cell.getStringCellValue());
+
+        //now remove the formula, the cached formula result remains
+        cell.setCellFormula(null);
+        assertEquals(XSSFCell.CELL_TYPE_STRING, cell.getCellType());
+        assertEquals(STCellType.STR, ctCell.getT());
+        //the line below failed prior to fix of Bug #47889
+        assertEquals("t='str'", cell.getStringCellValue());
+
+        //revert to a blank cell
+        cell.setCellValue((String)null);
+        assertEquals(XSSFCell.CELL_TYPE_BLANK, cell.getCellType());
+        assertEquals(STCellType.N, ctCell.getT());
+        assertEquals("", cell.getStringCellValue());
+    }
+
+    /**
+     * Bug 47889: problems when calling XSSFCell.getStringCellValue() on a workbook created
in Gnumeric
+     */
+    public void test47889() {
+        XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("47889.xlsx");
+        XSSFSheet sh = wb.getSheetAt(0);
+
+        XSSFCell cell;
+
+        //try a string cell
+        cell = sh.getRow(0).getCell(0);
+        assertEquals(XSSFCell.CELL_TYPE_STRING, cell.getCellType());
+        assertEquals("a", cell.getStringCellValue());
+        assertEquals("a", cell.toString());
+        //Gnumeric produces spreadsheets without styles
+        //make sure we return null for that instead of throwing OutOfBounds
+        assertEquals(null, cell.getCellStyle());
+
+        //try a numeric cell
+        cell = sh.getRow(1).getCell(0);
+        assertEquals(XSSFCell.CELL_TYPE_NUMERIC, cell.getCellType());
+        assertEquals(1.0, cell.getNumericCellValue());
+        assertEquals("1.0", cell.toString());
+        //Gnumeric produces spreadsheets without styles
+        //make sure we return null for that instead of throwing OutOfBounds
+        assertEquals(null, cell.getCellStyle());
+    }
 }

Added: poi/trunk/test-data/spreadsheet/47889.xlsx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/47889.xlsx?rev=819106&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/47889.xlsx
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



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


Mime
View raw message