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
This commit is contained in:
Yegor Kozlov 2009-04-17 18:02:01 +00:00
parent 1e40b052a3
commit ecb76070a9
7 changed files with 49 additions and 9 deletions

View File

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

View File

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

View File

@ -627,6 +627,7 @@ public final class XSSFCell implements Cell {
private void setBlank(){ private void setBlank(){
CTCell blank = CTCell.Factory.newInstance(); CTCell blank = CTCell.Factory.newInstance();
blank.setR(cell.getR()); blank.setR(cell.getR());
blank.setS(cell.getS());
cell.set(blank); cell.set(blank);
} }

View File

@ -179,6 +179,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
} }
} }
if(sharedStringSource == null) {
//Create SST if it is missing
sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
}
// Load individual sheets. The order of sheets is defined by the order of CTSheet elements in the workbook // Load individual sheets. The order of sheets is defined by the order of CTSheet elements in the workbook
sheets = new ArrayList<XSSFSheet>(shIdMap.size()); sheets = new ArrayList<XSSFSheet>(shIdMap.size());
for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) { for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
@ -192,11 +197,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
sheets.add(sh); sheets.add(sh);
} }
if(sharedStringSource == null) {
//Create SST if it is missing
sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
}
// Process the named ranges // Process the named ranges
namedRanges = new ArrayList<XSSFName>(); namedRanges = new ArrayList<XSSFName>();
if(workbook.isSetDefinedNames()) { if(workbook.isSetDefinedNames()) {

View File

@ -17,7 +17,7 @@
package org.apache.poi.xssf.usermodel; 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; import org.apache.poi.xssf.XSSFITestDataProvider;
/** /**
@ -28,4 +28,26 @@ public final class TestXSSFCell extends BaseTestCell {
public TestXSSFCell() { public TestXSSFCell() {
super(XSSFITestDataProvider.getInstance()); 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");
}
} }

Binary file not shown.

View File

@ -22,7 +22,6 @@ import java.util.Calendar;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.ITestDataProvider;
/** /**
@ -31,7 +30,7 @@ import org.apache.poi.ss.ITestDataProvider;
*/ */
public abstract class BaseTestCell extends TestCase { 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 * @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 cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
cell.setCellFormula("B1&C1"); cell.setCellFormula("B1&C1");
try { try {
cell.setCellValue(new HSSFRichTextString("hello")); cell.setCellValue(wb.getCreationHelper().createRichTextString("hello"));
} catch (ClassCastException e) { } catch (ClassCastException e) {
throw new AssertionFailedError("Identified bug 44606"); 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);
}
} }