diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 0b3c47561..9649fe955 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -43,7 +43,6 @@ import org.apache.poi.hssf.record.NumberRecord; 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 final class HSSFCell { } public int getColumnIndex() { - return record.getColumn() & 0xFFFF; + return record.getColumn() & 0xFFFF; } /** @@ -332,38 +331,23 @@ public final class HSSFCell { 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 @@ public final class HSSFCell { 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 @@ public final class HSSFCell { } 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. diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java index 30f63ab4a..abb9f1b41 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java @@ -28,8 +28,8 @@ import org.apache.poi.hssf.model.Sheet; 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 @@ public final class TestHSSFCell extends TestCase { } } - /** - * 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()); + } }