From 454dcfb6c9c877ae759a78a5e17dd550657a1281 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Wed, 5 Nov 2008 20:46:00 +0000 Subject: [PATCH] Fixed bug in conversion to/from text cells git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@711694 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFCell.java | 70 ++++++----- .../poi/hssf/usermodel/TestHSSFCell.java | 117 ++++++++++++------ 2 files changed, 117 insertions(+), 70 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 0b3c475615..9649fe9556 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 30f63ab4a2..abb9f1b410 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()); + } } -- 2.39.5