Bug 63339: Use removeFormulaImpl() where possible and add a test which verifies that setting a string-value on a formula cell is performed

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1889837 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2021-05-13 12:11:26 +00:00
parent 5e656e05ef
commit 454d0452ef
2 changed files with 53 additions and 24 deletions

View File

@ -539,11 +539,7 @@ public final class XSSFCell extends CellBase {
private void setFormula(String formula, FormulaType formulaType) {
XSSFWorkbook wb = _row.getSheet().getWorkbook();
if (formulaType == FormulaType.ARRAY && formula == null) {
wb.onDeleteFormula(this);
if (_cell.isSetF()) {
_row.getSheet().onDeleteFormula(this, null);
_cell.unsetF();
}
removeFormulaImpl();
return;
}

View File

@ -3498,6 +3498,39 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
}
}
@Test
public void testBug63339() throws IOException {
try (Workbook wb = new XSSFWorkbook()) {
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0, CellType.FORMULA);
cell.setCellFormula("SUM(B1:E1)");
assertNotNull(((XSSFCell) cell).getCTCell().getF(),
"Element 'f' should contain the formula now");
// this will actually set the "cached" value of the Formula
// you will need to use setCellType() to change the cell to
// a different type of value
cell.setCellValue(34.5);
assertNotNull(((XSSFCell) cell).getCTCell().getF(),
"Element 'f' should not be set now");
assertEquals("34.5", ((XSSFCell) cell).getCTCell().getV(),
"Element 'v' should contain the string now");
try (Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb)) {
Cell cellBack = wbBack.getSheetAt(0).getRow(0).getCell(0);
assertNotNull(((XSSFCell) cellBack).getCTCell().getF(),
"Element 'f' should not be set now");
assertEquals("34.5", ((XSSFCell) cellBack).getCTCell().getV(),
"Element 'v' should contain the string now");
}
}
}
@Test
void testBug64508() throws IOException {
try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("64508.xlsx")) {