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
*
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;
}
}
+
+ @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