diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 4a1d8116c..f58747e7e 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,8 @@ + 47028 - Fixed XSSFCell to preserve cell style when cell value is set to blank + 47026 - Avoid NPE in XSSFCell.setCellType() when workbook does not have SST 46987 - Allow RecordFactory to handle non-zero padding at the end of the workbook stream 47034 - Fix reading the name of a NameRecord when the name is very long 47001 - Fixed WriteAccessRecord and LinkTable to handle unusual format written by Google Docs diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 7564d7d25..4ef245ad8 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,8 @@ + 47028 - Fixed XSSFCell to preserve cell style when cell value is set to blank + 47026 - Avoid NPE in XSSFCell.setCellType() when workbook does not have SST 46987 - Allow RecordFactory to handle non-zero padding at the end of the workbook stream 47034 - Fix reading the name of a NameRecord when the name is very long 47001 - Fixed WriteAccessRecord and LinkTable to handle unusual format written by Google Docs diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index f0ad410ad..2a0e1ff4b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -627,6 +627,7 @@ public final class XSSFCell implements Cell { private void setBlank(){ CTCell blank = CTCell.Factory.newInstance(); blank.setR(cell.getR()); + blank.setS(cell.getS()); cell.set(blank); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 9cb66c531..1648ac86f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -179,6 +179,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable(shIdMap.size()); for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) { @@ -192,11 +197,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable(); if(workbook.isSetDefinedNames()) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index b851d8827..f97a6d1b8 100755 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -17,7 +17,7 @@ package org.apache.poi.xssf.usermodel; -import org.apache.poi.ss.usermodel.BaseTestCell; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.XSSFITestDataProvider; /** @@ -28,4 +28,26 @@ public final class TestXSSFCell extends BaseTestCell { public TestXSSFCell() { super(XSSFITestDataProvider.getInstance()); } + + /** + * Bug 47026: trouble changing cell type when workbook doesn't contain + * Shared String Table + */ + public void test47026_1() throws Exception { + Workbook source = _testDataProvider.openSampleWorkbook("47026.xlsm"); + Sheet sheet = source.getSheetAt(0); + Row row = sheet.getRow(0); + Cell cell = row.getCell(0); + cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellValue("456"); + } + + public void test47026_2() throws Exception { + Workbook source = _testDataProvider.openSampleWorkbook("47026.xlsm"); + Sheet sheet = source.getSheetAt(0); + Row row = sheet.getRow(0); + Cell cell = row.getCell(0); + cell.setCellFormula(null); + cell.setCellValue("456"); + } } \ No newline at end of file diff --git a/src/testcases/org/apache/poi/hssf/data/47026.xlsm b/src/testcases/org/apache/poi/hssf/data/47026.xlsm new file mode 100755 index 000000000..77e4eb97f Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/47026.xlsm differ diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index 5649c950e..1a987c1ac 100755 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -22,7 +22,6 @@ import java.util.Calendar; import junit.framework.AssertionFailedError; import junit.framework.TestCase; -import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.ss.ITestDataProvider; /** @@ -31,7 +30,7 @@ import org.apache.poi.ss.ITestDataProvider; */ public abstract class BaseTestCell extends TestCase { - private final ITestDataProvider _testDataProvider; + protected final ITestDataProvider _testDataProvider; /** * @param testDataProvider an object that provides test data in HSSF / XSSF specific way @@ -401,9 +400,23 @@ public abstract class BaseTestCell extends TestCase { Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0); cell.setCellFormula("B1&C1"); try { - cell.setCellValue(new HSSFRichTextString("hello")); + cell.setCellValue(wb.getCreationHelper().createRichTextString("hello")); } catch (ClassCastException e) { throw new AssertionFailedError("Identified bug 44606"); } } + + /** + * Make sure that cell.setCellType(Cell.CELL_TYPE_BLANK) preserves the cell style + */ + public void testSetBlank_bug47028() { + Workbook wb = _testDataProvider.createWorkbook(); + CellStyle style = wb.createCellStyle(); + Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0); + cell.setCellStyle(style); + int i1 = cell.getCellStyle().getIndex(); + cell.setCellType(Cell.CELL_TYPE_BLANK); + int i2 = cell.getCellStyle().getIndex(); + assertEquals(i1, i2); + } }