From: Nick Burch Date: Thu, 29 May 2014 11:39:50 +0000 (+0000) Subject: HSSFCell should follow XSSF, and allow setting a null-style to return to the default... X-Git-Tag: REL_3_11_BETA1~123 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=09a9ca7bef7ab2fffcb0a3ca5de71f9f64cd4011;p=poi.git HSSFCell should follow XSSF, and allow setting a null-style to return to the default style, fixes bug #56572 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1598258 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index a28d023aff..c66cdd3d13 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -899,7 +899,13 @@ public class HSSFCell implements Cell { setCellStyle( (HSSFCellStyle)style ); } public void setCellStyle(HSSFCellStyle style) { - // Verify it really does belong to our workbook + // A style of null means resetting back to the default style + if (style == null) { + _record.setXFIndex((short)0xf); + return; + } + + // Verify the style really does belong to our workbook style.verifyBelongsToWorkbook(_book); short styleIndex; diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java index e18eebe90b..078042970f 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java @@ -459,4 +459,40 @@ public final class TestHSSFCell extends BaseTestCell { cell.setCellValue((String)null); cell.setCellValue((RichTextString)null); } + + public void testSetRemoveStyle() throws Exception { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + HSSFRow row = sheet.createRow(0); + HSSFCell cell = row.createCell(0); + + HSSFCellStyle defaultStyle = wb.getCellStyleAt((short)15); + + // Starts out with the default style + assertEquals(defaultStyle, cell.getCellStyle()); + + // Create some styles, no change + HSSFCellStyle style1 = wb.createCellStyle(); + HSSFCellStyle style2 = wb.createCellStyle(); + style1.setDataFormat((short)2); + style2.setDataFormat((short)3); + + assertEquals(defaultStyle, cell.getCellStyle()); + + // Apply one, changes + cell.setCellStyle(style1); + assertEquals(style1, cell.getCellStyle()); + + // Apply the other, changes + cell.setCellStyle(style2); + assertEquals(style2, cell.getCellStyle()); + + // Remove, goes back to default + cell.setCellStyle(null); + assertEquals(defaultStyle, cell.getCellStyle()); + + // Add back, returns + cell.setCellStyle(style2); + assertEquals(style2, cell.getCellStyle()); + } }