Author: yegor Date: Wed Dec 20 16:30:13 2017 New Revision: 1818818 URL: http://svn.apache.org/viewvc?rev=1818818&view=rev Log: Bug 61869: updating a cell with shared formula produces an unreadable file
Added: poi/trunk/test-data/spreadsheet/61869.xlsx (with props) Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java?rev=1818818&r1=1818817&r2=1818818&view=diff ============================================================================== --- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java (original) +++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java Wed Dec 20 16:30:13 2017 @@ -558,21 +558,30 @@ public final class XSSFCell implements C XSSFWorkbook wb = _row.getSheet().getWorkbook(); if (formula == null) { wb.onDeleteFormula(this); - if(_cell.isSetF()) { + if (_cell.isSetF()) { _cell.unsetF(); } return; } - if(wb.getCellFormulaValidation()) { + if (wb.getCellFormulaValidation()) { XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb); //validate through the FormulaParser FormulaParser.parse(formula, fpb, formulaType, wb.getSheetIndex(getSheet()), getRowIndex()); } - CTCellFormula f = CTCellFormula.Factory.newInstance(); - f.setStringValue(formula); - _cell.setF(f); + CTCellFormula f; + if (_cell.isSetF()) { + f = _cell.getF(); + f.setStringValue(formula); + if(f.getT() == STCellFormulaType.SHARED){ + getRow().getSheet().onReadCell(this); + } + } else { + f = CTCellFormula.Factory.newInstance(); + f.setStringValue(formula); + _cell.setF(f); + } if(_cell.isSetV()) { _cell.unsetV(); } Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java?rev=1818818&r1=1818817&r2=1818818&view=diff ============================================================================== --- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java (original) +++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java Wed Dec 20 16:30:13 2017 @@ -48,6 +48,7 @@ import org.apache.poi.xssf.XSSFTestDataS import org.apache.poi.xssf.model.SharedStringsTable; import org.junit.Test; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellFormulaType; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; import static org.junit.Assert.*; @@ -670,4 +671,34 @@ public final class TestXSSFCell extends destCell.setCellValue(true); } -} + + /** + * Bug 61869: updating a shared formula produces an unreadable file + */ + @Test + public void test61869() throws Exception { + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61869.xlsx")) { + XSSFSheet sheet = wb.getSheetAt(0); + XSSFCell c2 = sheet.getRow(1).getCell(2); + assertEquals("SUM(A2,B2)", c2.getCellFormula()); + assertEquals(STCellFormulaType.SHARED, c2.getCTCell().getF().getT()); + assertEquals(0, c2.getCTCell().getF().getSi()); + XSSFCell c3 = sheet.getRow(2).getCell(2); + assertEquals(STCellFormulaType.SHARED, c3.getCTCell().getF().getT()); + assertEquals(0, c3.getCTCell().getF().getSi()); + assertEquals("SUM(A3,B3)", c3.getCellFormula()); + + assertEquals("SUM(A2,B2)", sheet.getSharedFormula(0).getStringValue()); + + c2.setCellFormula("SUM(A2:B2)"); + assertEquals(STCellFormulaType.SHARED, c2.getCTCell().getF().getT()); // c2 remains the master formula + + assertEquals("SUM(A2:B2)", sheet.getSharedFormula(0).getStringValue()); + assertEquals(STCellFormulaType.SHARED, c3.getCTCell().getF().getT()); + assertEquals(0, c3.getCTCell().getF().getSi()); + assertEquals("SUM(A3:B3)", c3.getCellFormula()); // formula in the follower cell is rebuilt + + } + + } +} \ No newline at end of file Added: poi/trunk/test-data/spreadsheet/61869.xlsx URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/61869.xlsx?rev=1818818&view=auto ============================================================================== Binary file - no diff available. Propchange: poi/trunk/test-data/spreadsheet/61869.xlsx ------------------------------------------------------------------------------ --- svn:mime-type (added) +++ svn:mime-type Wed Dec 20 16:30:13 2017 @@ -0,0 +1 @@ +application/vnd.openxmlformats-officedocument.spreadsheetml.sheet --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org For additional commands, e-mail: commits-h...@poi.apache.org