You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2017/12/30 16:34:58 UTC

svn commit: r1819623 - in /poi/trunk: src/ooxml/java/org/apache/poi/xssf/usermodel/ src/ooxml/testcases/org/apache/poi/xssf/usermodel/ test-data/spreadsheet/

Author: yegor
Date: Sat Dec 30 16:34:57 2017
New Revision: 1819623

URL: http://svn.apache.org/viewvc?rev=1819623&view=rev
Log:
Bug 58106: when a cell with a 'master' shared formula is removed,  the next cell in the range becomes the master

Added:
    poi/trunk/test-data/spreadsheet/58106.xlsx   (with props)
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.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=1819623&r1=1819622&r2=1819623&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 Sat Dec 30 16:34:57 2017
@@ -559,6 +559,7 @@ public final class XSSFCell implements C
         if (formula == null) {
             wb.onDeleteFormula(this);
             if (_cell.isSetF()) {
+                _row.getSheet().onDeleteFormula(this);
                 _cell.unsetF();
             }
             return;
@@ -963,6 +964,9 @@ public final class XSSFCell implements C
             notifyArrayFormulaChanging();
         }
         if(prevType == CellType.FORMULA && cellType != CellType.FORMULA) {
+            if (_cell.isSetF()) {
+                _row.getSheet().onDeleteFormula(this);
+            }
             getSheet().getWorkbook().onDeleteFormula(this);
         }
 

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=1819623&r1=1819622&r2=1819623&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Sat Dec 30 16:34:57 2017
@@ -4455,7 +4455,40 @@ public class XSSFSheet extends POIXMLDoc
             removeRelation(part.getDocumentPart(), true);
         }
     }
-    
+
+    /**
+     *  when a cell with a 'master' shared formula is removed,  the next cell in the range becomes the master
+     */
+    protected void onDeleteFormula(XSSFCell cell){
+
+        CTCellFormula f = cell.getCTCell().getF();
+        if (f != null && f.getT() == STCellFormulaType.SHARED && f.isSetRef() && f.getStringValue() != null) {
+
+            CellRangeAddress ref = CellRangeAddress.valueOf(f.getRef());
+            if(ref.getNumberOfCells() > 1){
+                DONE:
+                for(int i = cell.getRowIndex(); i <= ref.getLastRow(); i++){
+                    XSSFRow row = getRow(i);
+                    if(row != null) for(int j = cell.getColumnIndex(); j <= ref.getLastColumn(); j++){
+                        XSSFCell nextCell = row.getCell(j);
+                        if(nextCell != null && nextCell != cell){
+                            CTCellFormula nextF = nextCell.getCTCell().getF();
+                            nextF.setStringValue(nextCell.getCellFormula());
+                            CellRangeAddress nextRef = new CellRangeAddress(
+                                    nextCell.getRowIndex(), ref.getLastRow(),
+                                    nextCell.getColumnIndex(), ref.getLastColumn());
+                            nextF.setRef(nextRef.formatAsString());
+
+                            sharedFormulas.put((int)nextF.getSi(), nextF);
+                            break DONE;
+                        }
+                    }
+                }
+            }
+
+        }
+    }
+
     /**
      * Determine the OleObject which links shapes with embedded resources
      *

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=1819623&r1=1819622&r2=1819623&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 Sat Dec 30 16:34:57 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.CTCellFormula;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellFormulaType;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
 
@@ -700,5 +701,48 @@ public final class TestXSSFCell extends
 
         }
 
+    }
+
+    @Test
+    public void testBug58106RemoveSharedFormula() throws Exception {
+        try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("58106.xlsx")) {
+            XSSFSheet sheet = wb.getSheetAt(0);
+            XSSFRow row = sheet.getRow(12);
+            XSSFCell cell = row.getCell(1);
+            CTCellFormula f = cell.getCTCell().getF();
+            assertEquals("B13:G13", f.getRef());
+            assertEquals("SUM(B1:B3)", f.getStringValue());
+            assertEquals(0, f.getSi());
+            assertEquals(STCellFormulaType.SHARED, f.getT());
+            for(char i = 'C'; i <= 'G'; i++){
+                XSSFCell sc =row.getCell(i-'A');
+                CTCellFormula sf = sc.getCTCell().getF();
+                assertFalse(sf.isSetRef());
+                assertEquals("", sf.getStringValue());
+                assertEquals(0, sf.getSi());
+                assertEquals(STCellFormulaType.SHARED, sf.getT());
+            }
+            assertEquals("B13:G13", sheet.getSharedFormula(0).getRef());
+
+            cell.setCellType(CellType.NUMERIC);
+
+            assertFalse(cell.getCTCell().isSetF());
+
+            XSSFCell nextFormulaMaster = row.getCell(2);
+            assertEquals("C13:G13", nextFormulaMaster.getCTCell().getF().getRef());
+            assertEquals("SUM(C1:C3)", nextFormulaMaster.getCTCell().getF().getStringValue());
+            assertEquals(0, nextFormulaMaster.getCTCell().getF().getSi());
+            for(char i = 'D'; i <= 'G'; i++){
+                XSSFCell sc =row.getCell(i-'A');
+                CTCellFormula sf = sc.getCTCell().getF();
+                assertFalse(sf.isSetRef());
+                assertEquals("", sf.getStringValue());
+                assertEquals(0, sf.getSi());
+                assertEquals(STCellFormulaType.SHARED, sf.getT());
+            }
+            assertEquals("C13:G13", sheet.getSharedFormula(0).getRef());
+
+        }
+
     }
 }
\ No newline at end of file

Added: poi/trunk/test-data/spreadsheet/58106.xlsx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/58106.xlsx?rev=1819623&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/58106.xlsx
------------------------------------------------------------------------------
--- svn:mime-type (added)
+++ svn:mime-type Sat Dec 30 16:34:57 2017
@@ -0,0 +1 @@
+application/vnd.openxmlformats-officedocument.spreadsheetml.sheet



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org