You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2008/11/05 21:46:00 UTC

svn commit: r711694 - in /poi/trunk/src: java/org/apache/poi/hssf/usermodel/HSSFCell.java testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java

Author: josh
Date: Wed Nov  5 12:46:00 2008
New Revision: 711694

URL: http://svn.apache.org/viewvc?rev=711694&view=rev
Log:
Fixed bug in conversion to/from text cells

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=711694&r1=711693&r2=711694&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Wed Nov  5 12:46:00 2008
@@ -43,7 +43,6 @@
 import org.apache.poi.hssf.record.ObjRecord;
 import org.apache.poi.hssf.record.Record;
 import org.apache.poi.hssf.record.RecordBase;
-import org.apache.poi.hssf.record.StringRecord;
 import org.apache.poi.hssf.record.SubRecord;
 import org.apache.poi.hssf.record.TextObjectRecord;
 import org.apache.poi.hssf.record.UnicodeString;
@@ -253,7 +252,7 @@
     }
     
     public int getColumnIndex() {
-    	return record.getColumn() & 0xFFFF;
+        return record.getColumn() & 0xFFFF;
     }
 
     /**
@@ -332,38 +331,23 @@
                 break;
 
             case CELL_TYPE_STRING :
-                LabelSSTRecord lrec = null;
+                LabelSSTRecord lrec;
 
-                if (cellType != this.cellType)
-                {
+                if (cellType == this.cellType) {
+                    lrec = (LabelSSTRecord) record;
+                } else {
                     lrec = new LabelSSTRecord();
+                    lrec.setColumn(col);
+                    lrec.setRow(row);
+                    lrec.setXFIndex(styleIndex);
                 }
-                else
-                {
-                    lrec = ( LabelSSTRecord ) record;
-                }
-                lrec.setColumn(col);
-                lrec.setRow(row);
-                lrec.setXFIndex(styleIndex);
-                if (setValue)
-                {
-                    if ((getStringCellValue() != null)
-                            && (!getStringCellValue().equals("")))
-                    {
-                        int sst = 0;
-
-                        UnicodeString str = getRichStringCellValue().getUnicodeString();
-//jmh                        if (encoding == ENCODING_COMPRESSED_UNICODE)
-//jmh                        {
-//                      jmh                            str.setCompressedUnicode();
-//                      jmh                        } else if (encoding == ENCODING_UTF_16)
-//                      jmh                        {
-//                      jmh                            str.setUncompressedUnicode();
-//                      jmh                        }
-                        sst = book.getWorkbook().addSSTString(str);
-                        lrec.setSSTIndex(sst);
-                        getRichStringCellValue().setUnicodeString(book.getWorkbook().getSSTString(sst));
-                    }
+                if (setValue) {
+                    String str = convertCellValueToString();
+                    int sstIndex = book.getWorkbook().addSSTString(new UnicodeString(str));
+                    lrec.setSSTIndex(sstIndex);
+                    UnicodeString us = book.getWorkbook().getSSTString(sstIndex);
+                    stringValue = new HSSFRichTextString();
+                    stringValue.setUnicodeString(us);
                 }
                 record = lrec;
                 break;
@@ -778,7 +762,9 @@
             case CELL_TYPE_BOOLEAN:
                 return (( BoolErrRecord ) record).getBooleanValue();
             case CELL_TYPE_STRING:
-                return Boolean.valueOf(((StringRecord)record).getString()).booleanValue();
+                int sstIndex = ((LabelSSTRecord)record).getSSTIndex();
+                String text = book.getWorkbook().getSSTString(sstIndex).getString();
+                return Boolean.valueOf(text).booleanValue();
             case CELL_TYPE_NUMERIC:
                 return ((NumberRecord)record).getValue() != 0;
 
@@ -792,6 +778,26 @@
         }
         throw new RuntimeException("Unexpected cell type (" + cellType + ")");
     }
+    private String convertCellValueToString() {
+
+        switch (cellType) {
+            case CELL_TYPE_BLANK:
+                return "";
+            case CELL_TYPE_BOOLEAN:
+                return ((BoolErrRecord) record).getBooleanValue() ? "TRUE" : "FALSE";
+            case CELL_TYPE_STRING:
+                int sstIndex = ((LabelSSTRecord)record).getSSTIndex();
+                return book.getWorkbook().getSSTString(sstIndex).getString();
+            case CELL_TYPE_NUMERIC:
+                return String.valueOf(((NumberRecord)record).getValue());
+            case CELL_TYPE_ERROR:
+                   return HSSFErrorConstants.getText(((BoolErrRecord) record).getErrorValue());
+            case CELL_TYPE_FORMULA:
+                // should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator
+                return "";
+        }
+        throw new RuntimeException("Unexpected cell type (" + cellType + ")");
+    }
 
     /**
      * get the value of the cell as a boolean.  For strings, numbers, and errors, we throw an exception.

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java?rev=711694&r1=711693&r2=711694&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java Wed Nov  5 12:46:00 2008
@@ -28,8 +28,8 @@
 import org.apache.poi.hssf.util.HSSFColor;
 
 /**
- * Tests various functionity having to do with HSSFCell.  For instance support for
- * paticular datatypes, etc.
+ * Tests various functionality having to do with {@link HSSFCell}.  For instance support for
+ * particular datatypes, etc.
  * @author Andrew C. Oliver (andy at superlinksoftware dot com)
  * @author  Dan Sherman (dsherman at isisph.com)
  * @author Alex Jacoby (ajacoby at gmail.com)
@@ -345,41 +345,82 @@
         }
     }
 
-    /**
-     * Test to ensure we can only assign cell styles that belong
-     *  to our workbook, and not those from other workbooks.
-     */
-    public void testCellStyleWorkbookMatch() throws Exception {
-    	HSSFWorkbook wbA = new HSSFWorkbook();
-    	HSSFWorkbook wbB = new HSSFWorkbook();
-    	
-    	HSSFCellStyle styA = wbA.createCellStyle();
-    	HSSFCellStyle styB = wbB.createCellStyle();
-    	
-    	styA.verifyBelongsToWorkbook(wbA);
-    	styB.verifyBelongsToWorkbook(wbB);
-    	try {
-    		styA.verifyBelongsToWorkbook(wbB);
-    		fail();
-    	} catch(IllegalArgumentException e) {}
-    	try {
-    		styB.verifyBelongsToWorkbook(wbA);
-    		fail();
-    	} catch(IllegalArgumentException e) {}
-    	
-    	HSSFCell cellA = wbA.createSheet().createRow(0).createCell(0);
-    	HSSFCell cellB = wbB.createSheet().createRow(0).createCell(0);
-    	
-    	cellA.setCellStyle(styA);
-    	cellB.setCellStyle(styB);
-    	try {
-        	cellA.setCellStyle(styB);
-    		fail();
-    	} catch(IllegalArgumentException e) {}
-    	try {
-        	cellB.setCellStyle(styA);
-    		fail();
-    	} catch(IllegalArgumentException e) {}
-    }
+	/**
+	 * Test to ensure we can only assign cell styles that belong
+	 *  to our workbook, and not those from other workbooks.
+	 */
+	public void testCellStyleWorkbookMatch() {
+		HSSFWorkbook wbA = new HSSFWorkbook();
+		HSSFWorkbook wbB = new HSSFWorkbook();
+
+		HSSFCellStyle styA = wbA.createCellStyle();
+		HSSFCellStyle styB = wbB.createCellStyle();
+
+		styA.verifyBelongsToWorkbook(wbA);
+		styB.verifyBelongsToWorkbook(wbB);
+		try {
+			styA.verifyBelongsToWorkbook(wbB);
+			fail();
+		} catch (IllegalArgumentException e) {}
+		try {
+			styB.verifyBelongsToWorkbook(wbA);
+			fail();
+		} catch (IllegalArgumentException e) {}
+
+		HSSFCell cellA = wbA.createSheet().createRow(0).createCell(0);
+		HSSFCell cellB = wbB.createSheet().createRow(0).createCell(0);
+
+		cellA.setCellStyle(styA);
+		cellB.setCellStyle(styB);
+		try {
+			cellA.setCellStyle(styB);
+			fail();
+		} catch (IllegalArgumentException e) {}
+		try {
+			cellB.setCellStyle(styA);
+			fail();
+		} catch (IllegalArgumentException e) {}
+	}
+
+	public void testChangeTypeStringToBool() {
+		HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0);
+
+		cell.setCellValue(new HSSFRichTextString("TRUE"));
+		assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCellType());
+		try {
+			cell.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
+		} catch (ClassCastException e) {
+			throw new AssertionFailedError(
+					"Identified bug in conversion of cell from text to boolean");
+		}
+
+		assertEquals(HSSFCell.CELL_TYPE_BOOLEAN, cell.getCellType());
+		assertEquals(true, cell.getBooleanCellValue());
+		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
+		assertEquals("TRUE", cell.getRichStringCellValue().getString());
+
+		// 'false' text to bool and back
+		cell.setCellValue(new HSSFRichTextString("FALSE"));
+		cell.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
+		assertEquals(HSSFCell.CELL_TYPE_BOOLEAN, cell.getCellType());
+		assertEquals(false, cell.getBooleanCellValue());
+		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
+		assertEquals("FALSE", cell.getRichStringCellValue().getString());
+	}
+
+	public void testChangeTypeBoolToString() {
+		HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0);
+		cell.setCellValue(true);
+		try {
+			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
+		} catch (IllegalStateException e) {
+			if (e.getMessage().equals("Cannot get a text value from a boolean cell")) {
+				throw new AssertionFailedError(
+						"Identified bug in conversion of cell from boolean to text");
+			}
+			throw e;
+		}
+		assertEquals("TRUE", cell.getRichStringCellValue().getString());
+	}
 }
 



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