Skip to content

Commit ecb7607

Browse files
author
Yegor Kozlov
committed
Fixed XSSFCell to preserve cell style when cell value is set to blank, also avoid NPE in XSSFCell.setCellType() when workbook does not have SST, see bugs 47026 and 47028
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@766103 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1e40b05 commit ecb7607

File tree

7 files changed

+49
-9
lines changed

7 files changed

+49
-9
lines changed

src/documentation/content/xdocs/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
<!-- Don't forget to update status.xml too! -->
3939
<release version="3.5-beta6" date="2009-??-??">
40+
<action dev="POI-DEVELOPERS" type="fix">47028 - Fixed XSSFCell to preserve cell style when cell value is set to blank</action>
41+
<action dev="POI-DEVELOPERS" type="fix">47026 - Avoid NPE in XSSFCell.setCellType() when workbook does not have SST</action>
4042
<action dev="POI-DEVELOPERS" type="fix">46987 - Allow RecordFactory to handle non-zero padding at the end of the workbook stream</action>
4143
<action dev="POI-DEVELOPERS" type="fix">47034 - Fix reading the name of a NameRecord when the name is very long</action>
4244
<action dev="POI-DEVELOPERS" type="fix">47001 - Fixed WriteAccessRecord and LinkTable to handle unusual format written by Google Docs</action>

src/documentation/content/xdocs/status.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
<!-- Don't forget to update changes.xml too! -->
3535
<changes>
3636
<release version="3.5-beta6" date="2009-??-??">
37+
<action dev="POI-DEVELOPERS" type="fix">47028 - Fixed XSSFCell to preserve cell style when cell value is set to blank</action>
38+
<action dev="POI-DEVELOPERS" type="fix">47026 - Avoid NPE in XSSFCell.setCellType() when workbook does not have SST</action>
3739
<action dev="POI-DEVELOPERS" type="fix">46987 - Allow RecordFactory to handle non-zero padding at the end of the workbook stream</action>
3840
<action dev="POI-DEVELOPERS" type="fix">47034 - Fix reading the name of a NameRecord when the name is very long</action>
3941
<action dev="POI-DEVELOPERS" type="fix">47001 - Fixed WriteAccessRecord and LinkTable to handle unusual format written by Google Docs</action>

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ public void setAsActiveCell() {
627627
private void setBlank(){
628628
CTCell blank = CTCell.Factory.newInstance();
629629
blank.setR(cell.getR());
630+
blank.setS(cell.getS());
630631
cell.set(blank);
631632
}
632633

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ else if (p instanceof XSSFSheet) {
179179
}
180180
}
181181

182+
if(sharedStringSource == null) {
183+
//Create SST if it is missing
184+
sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
185+
}
186+
182187
// Load individual sheets. The order of sheets is defined by the order of CTSheet elements in the workbook
183188
sheets = new ArrayList<XSSFSheet>(shIdMap.size());
184189
for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
@@ -192,11 +197,6 @@ else if (p instanceof XSSFSheet) {
192197
sheets.add(sh);
193198
}
194199

195-
if(sharedStringSource == null) {
196-
//Create SST if it is missing
197-
sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
198-
}
199-
200200
// Process the named ranges
201201
namedRanges = new ArrayList<XSSFName>();
202202
if(workbook.isSetDefinedNames()) {

src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1717

1818
package org.apache.poi.xssf.usermodel;
1919

20-
import org.apache.poi.ss.usermodel.BaseTestCell;
20+
import org.apache.poi.ss.usermodel.*;
2121
import org.apache.poi.xssf.XSSFITestDataProvider;
2222

2323
/**
@@ -28,4 +28,26 @@ public final class TestXSSFCell extends BaseTestCell {
2828
public TestXSSFCell() {
2929
super(XSSFITestDataProvider.getInstance());
3030
}
31+
32+
/**
33+
* Bug 47026: trouble changing cell type when workbook doesn't contain
34+
* Shared String Table
35+
*/
36+
public void test47026_1() throws Exception {
37+
Workbook source = _testDataProvider.openSampleWorkbook("47026.xlsm");
38+
Sheet sheet = source.getSheetAt(0);
39+
Row row = sheet.getRow(0);
40+
Cell cell = row.getCell(0);
41+
cell.setCellType(Cell.CELL_TYPE_STRING);
42+
cell.setCellValue("456");
43+
}
44+
45+
public void test47026_2() throws Exception {
46+
Workbook source = _testDataProvider.openSampleWorkbook("47026.xlsm");
47+
Sheet sheet = source.getSheetAt(0);
48+
Row row = sheet.getRow(0);
49+
Cell cell = row.getCell(0);
50+
cell.setCellFormula(null);
51+
cell.setCellValue("456");
52+
}
3153
}
Binary file not shown.

src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2222
import junit.framework.AssertionFailedError;
2323
import junit.framework.TestCase;
2424

25-
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
2625
import org.apache.poi.ss.ITestDataProvider;
2726

2827
/**
@@ -31,7 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3130
*/
3231
public abstract class BaseTestCell extends TestCase {
3332

34-
private final ITestDataProvider _testDataProvider;
33+
protected final ITestDataProvider _testDataProvider;
3534

3635
/**
3736
* @param testDataProvider an object that provides test data in HSSF / XSSF specific way
@@ -401,9 +400,23 @@ public final void testSetStringInFormulaCell_bug44606() {
401400
Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
402401
cell.setCellFormula("B1&C1");
403402
try {
404-
cell.setCellValue(new HSSFRichTextString("hello"));
403+
cell.setCellValue(wb.getCreationHelper().createRichTextString("hello"));
405404
} catch (ClassCastException e) {
406405
throw new AssertionFailedError("Identified bug 44606");
407406
}
408407
}
408+
409+
/**
410+
* Make sure that cell.setCellType(Cell.CELL_TYPE_BLANK) preserves the cell style
411+
*/
412+
public void testSetBlank_bug47028() {
413+
Workbook wb = _testDataProvider.createWorkbook();
414+
CellStyle style = wb.createCellStyle();
415+
Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
416+
cell.setCellStyle(style);
417+
int i1 = cell.getCellStyle().getIndex();
418+
cell.setCellType(Cell.CELL_TYPE_BLANK);
419+
int i2 = cell.getCellStyle().getIndex();
420+
assertEquals(i1, i2);
421+
}
409422
}

0 commit comments

Comments
 (0)