diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 5e268167d7..edfcf676a2 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49564 - Fixed default behaviour of XSSFCellStyle.getLocked() 48314 - Fixed setting column and row breaks in XSSF 51424 - Ignore exceptions in ParagraphSprmUncompressor 51415 - Fixed Workbook.createSheet(sheetName) to truncate names longer than 31 characters diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index 86a8aac235..51ffe6e06e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -565,7 +565,10 @@ public class XSSFCellStyle implements CellStyle { * @return boolean - whether the cell using this style is hidden */ public boolean getHidden() { - return getCellProtection().getHidden(); + if (!_cellXf.isSetProtection() || !_cellXf.getProtection().isSetHidden()) { + return false; + } + return _cellXf.getProtection().getHidden(); } /** @@ -619,7 +622,10 @@ public class XSSFCellStyle implements CellStyle { * @return whether the cell using this style are locked */ public boolean getLocked() { - return getCellProtection().getLocked(); + if (!_cellXf.isSetProtection() || !_cellXf.getProtection().isSetLocked()) { + return true; + } + return _cellXf.getProtection().getLocked(); } /** @@ -1169,7 +1175,10 @@ public class XSSFCellStyle implements CellStyle { * @param hidden - whether the cell using this style should be hidden */ public void setHidden(boolean hidden) { - getCellProtection().setHidden(hidden); + if (!_cellXf.isSetProtection()) { + _cellXf.addNewProtection(); + } + _cellXf.getProtection().setHidden(hidden); } /** @@ -1218,7 +1227,10 @@ public class XSSFCellStyle implements CellStyle { * @param locked - whether the cell using this style should be locked */ public void setLocked(boolean locked) { - getCellProtection().setLocked(locked); + if (!_cellXf.isSetProtection()) { + _cellXf.addNewProtection(); + } + _cellXf.getProtection().setLocked(locked); } /** @@ -1388,17 +1400,6 @@ public class XSSFCellStyle implements CellStyle { return (int) _cellStyleXf.getFontId(); } - /** - * get a cellProtection from the supplied XML definition - * @return CTCellProtection - */ - private CTCellProtection getCellProtection() { - if (_cellXf.getProtection() == null) { - _cellXf.addNewProtection(); - } - return _cellXf.getProtection(); - } - /** * get the cellAlignment object to use for manage alignment * @return XSSFCellAlignment - cell alignment diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java index 14259cf43c..a9c968c7e3 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java @@ -526,15 +526,15 @@ public class TestXSSFCellStyle extends TestCase { public void testGetSetHidden() { assertFalse(cellStyle.getHidden()); - cellXf.getProtection().setHidden(true); + cellStyle.setHidden(true); assertTrue(cellStyle.getHidden()); cellStyle.setHidden(false); assertFalse(cellStyle.getHidden()); } public void testGetSetLocked() { - assertFalse(cellStyle.getLocked()); - cellXf.getProtection().setLocked(true); + assertTrue(cellStyle.getLocked()); + cellStyle.setLocked(true); assertTrue(cellStyle.getLocked()); cellStyle.setLocked(false); assertFalse(cellStyle.getLocked()); diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index e521aa59c7..14f70295a6 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -544,4 +544,39 @@ public abstract class BaseTestCell extends TestCase { assertEquals(Cell.CELL_TYPE_ERROR, cell2.getCellType()); assertEquals(ErrorConstants.ERROR_DIV_0, cell2.getErrorCellValue()); } + + public void testDefaultStyleProperties() { + Workbook wb = _testDataProvider.createWorkbook(); + + Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0); + CellStyle style = cell.getCellStyle(); + + assertTrue(style.getLocked()); + assertFalse(style.getHidden()); + assertEquals(0, style.getIndention()); + assertEquals(0, style.getFontIndex()); + assertEquals(0, style.getAlignment()); + assertEquals(0, style.getDataFormat()); + assertEquals(false, style.getWrapText()); + + CellStyle style2 = wb.createCellStyle(); + assertTrue(style2.getLocked()); + assertFalse(style2.getHidden()); + style2.setLocked(false); + style2.setHidden(true); + assertFalse(style2.getLocked()); + assertTrue(style2.getHidden()); + + wb = _testDataProvider.writeOutAndReadBack(wb); + cell = wb.getSheetAt(0).getRow(0).getCell(0); + style = cell.getCellStyle(); + assertFalse(style2.getLocked()); + assertTrue(style2.getHidden()); + + style2.setLocked(true); + style2.setHidden(false); + assertTrue(style2.getLocked()); + assertFalse(style2.getHidden()); + } + }