]> source.dussan.org Git - poi.git/commitdiff
Fix bug #49966 - Correctly remove calcChain entries for XSSF cells that stop holding...
authorNick Burch <nick@apache.org>
Tue, 21 Sep 2010 11:16:12 +0000 (11:16 +0000)
committerNick Burch <nick@apache.org>
Tue, 21 Sep 2010 11:16:12 +0000 (11:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@999314 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

index 736f3549b4694f079b5eb979adc0c8d11cab1101..81a4a3beaba67a346db73acbdf209db361b950c5 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta3" date="2010-??-??">
+           <action dev="poi-developers" type="fix">49966 - Correctly remove calcChain entries for XSSF cells that stop holding formulas</action>
            <action dev="poi-developers" type="add">47582 - XSSFCellStyle support for creating a style in one workbook based on a style from a different one</action>
            <action dev="poi-developers" type="fix">49931 - Avoid concurrency problems when re-ordering multiple HSSF header records for a PageSettingsBlock</action>
            <action dev="poi-developers" type="fix">49765 - Fix XWPFDocument.addPicture so that it correctly sets up relationships</action>
index 93efe4b37702ada8e1d21cfd0cecc23652ed5ad6..e0998bc590bf50c85c6c61d1873fc3a0f253ca8d 100644 (file)
@@ -721,11 +721,15 @@ public final class XSSFCell implements Cell {
      * @see #CELL_TYPE_ERROR
      */
     public void setCellType(int cellType) {
+        int prevType = getCellType();
+       
         if(isPartOfArrayFormulaGroup()){
             notifyArrayFormulaChanging();
         }
-
-        int prevType = getCellType();
+        if(prevType == CELL_TYPE_FORMULA && cellType != CELL_TYPE_FORMULA) {
+            getSheet().getWorkbook().onDeleteFormula(this);
+        }
+        
         switch (cellType) {
             case CELL_TYPE_BLANK:
                 setBlank();
index 53747f5e3f7b91953c553f85a035280c04cb3f8e..f61717f4753562b4a8b7eeb780d3a323372beb13 100644 (file)
@@ -365,9 +365,12 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
         }
 
         XSSFCell xcell = (XSSFCell)cell;
-        if(xcell.isPartOfArrayFormulaGroup()){
+        if(xcell.isPartOfArrayFormulaGroup()) {
             xcell.notifyArrayFormulaChanging();
         }
+        if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
+           _sheet.getWorkbook().onDeleteFormula(xcell);
+        }
         _cells.remove(cell.getColumnIndex());
     }
 
index 8ae7f9a8587b3c5b5b8e440073b95518aada4575..00d2ffee7e014ae7498618fad10e145f70570915 100644 (file)
@@ -833,7 +833,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
         //delete the CTSheet reference from workbook.xml
         workbook.getSheets().removeSheet(index);
 
-        //calculation chain is auxilary, remove it as it may contain orfan references to deleted cells
+        //calculation chain is auxiliary, remove it as it may contain orphan references to deleted cells
         if(calcChain != null) {
             removeRelation(calcChain);
             calcChain = null;
index 6e9583826f706e00b173d5022b8d2ecdaf76a539..db8cb68ecbee196b75f6f84f5300d9309a868f05 100644 (file)
@@ -38,6 +38,7 @@ import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.apache.poi.xssf.model.CalculationChain;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
 
@@ -544,4 +545,41 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
        }
     }
 
+    /**
+     * Various ways of removing a cell formula should all zap
+     *  the calcChain entry.
+     */
+    public void test49966() throws Exception {
+       XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("shared_formulas.xlsx");
+       XSSFSheet sheet = wb.getSheetAt(0);
+       
+       // CalcChain has lots of entries
+       CalculationChain cc = wb.getCalculationChain();
+       assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
+       assertEquals("A3", cc.getCTCalcChain().getCArray(1).getR());
+       assertEquals("A4", cc.getCTCalcChain().getCArray(2).getR());
+       assertEquals("A5", cc.getCTCalcChain().getCArray(3).getR());
+       assertEquals("A6", cc.getCTCalcChain().getCArray(4).getR());
+       assertEquals("A7", cc.getCTCalcChain().getCArray(5).getR());
+       
+       // Try various ways of changing the formulas
+       // If it stays a formula, chain entry should remain
+       // Otherwise should go
+       sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay
+       sheet.getRow(2).getCell(0).setCellFormula(null);  // go
+       sheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_FORMULA); // stay
+       sheet.getRow(4).getCell(0).setCellType(Cell.CELL_TYPE_STRING);  // go
+       sheet.getRow(5).removeCell(
+             sheet.getRow(5).getCell(0)  // go
+       );
+       
+       // Save and check
+       wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
+       sheet = wb.getSheetAt(0);
+       
+       cc = wb.getCalculationChain();
+       assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
+       assertEquals("A4", cc.getCTCalcChain().getCArray(1).getR());
+       assertEquals("A7", cc.getCTCalcChain().getCArray(2).getR());
+    }
 }