]> source.dussan.org Git - poi.git/commitdiff
HSSFCell should follow XSSF, and allow setting a null-style to return to the default...
authorNick Burch <nick@apache.org>
Thu, 29 May 2014 11:39:50 +0000 (11:39 +0000)
committerNick Burch <nick@apache.org>
Thu, 29 May 2014 11:39:50 +0000 (11:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1598258 13f79535-47bb-0310-9956-ffa450edef68

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

index a28d023aff59352c6e7c558da7366d6951523524..c66cdd3d13a24670c79f84a968ba9ec77600d2af 100644 (file)
@@ -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;
index e18eebe90be2d9cd16394b9ecc4cfe2bd973c4dd..078042970f8361dcb4b53e7c212b1041df985faa 100644 (file)
@@ -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());
+    }
 }