diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-10-09 12:26:21 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-10-09 12:26:21 +0000 |
commit | 08a1f9af227ad39a01844d4d9d0fc32947f4511d (patch) | |
tree | f6311855e9281af5660ca30b454d2173f6b58b3d | |
parent | 798f8c1db3a46bfbc9c8d1829a9720b0823a6f90 (diff) | |
download | poi-08a1f9af227ad39a01844d4d9d0fc32947f4511d.tar.gz poi-08a1f9af227ad39a01844d4d9d0fc32947f4511d.zip |
[bug-65268] issue reading shared formulas
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894064 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java | 22 | ||||
-rw-r--r-- | poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java | 44 | ||||
-rw-r--r-- | test-data/spreadsheet/testSharedFormulasSetBlank.xlsx | bin | 0 -> 8889 bytes |
3 files changed, 43 insertions, 23 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index b383413e70..e953378dee 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -4666,25 +4666,21 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { XSSFCell nextCell = row.getCell(j); if(nextCell != null && nextCell != cell && nextCell.getCellType() == CellType.FORMULA) { CTCellFormula nextF = nextCell.getCTCell().getF(); - nextF.setStringValue(nextCell.getCellFormula(evalWb)); - //https://bz.apache.org/bugzilla/show_bug.cgi?id=65464 - nextF.setT(STCellFormulaType.SHARED); - if (!nextF.isSetSi()) { - nextF.setSi(f.getSi()); + if (nextF.getT() == STCellFormulaType.SHARED && nextF.getSi() == f.getSi()) { + nextF.setStringValue(nextCell.getCellFormula(evalWb)); + CellRangeAddress nextRef = new CellRangeAddress( + nextCell.getRowIndex(), ref.getLastRow(), + nextCell.getColumnIndex(), ref.getLastColumn()); + nextF.setRef(nextRef.formatAsString()); + + sharedFormulas.put(Math.toIntExact(nextF.getSi()), nextF); + break DONE; } - CellRangeAddress nextRef = new CellRangeAddress( - nextCell.getRowIndex(), ref.getLastRow(), - nextCell.getColumnIndex(), ref.getLastColumn()); - nextF.setRef(nextRef.formatAsString()); - - sharedFormulas.put(Math.toIntExact(nextF.getSi()), nextF); - break DONE; } } } } } - } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 29bbde1239..156e89cf25 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -35,15 +35,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.time.Instant; -import java.util.Arrays; -import java.util.Calendar; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TimeZone; -import java.util.TreeMap; +import java.util.*; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipFile; @@ -3560,18 +3552,50 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { XSSFSheet sheet = wb.getSheet("SheetWithSharedFormula"); XSSFCell v15 = sheet.getRow(14).getCell(21); XSSFCell v16 = sheet.getRow(15).getCell(21); + XSSFCell v17 = sheet.getRow(16).getCell(21); assertEquals("U15/R15", v15.getCellFormula()); assertEquals(STCellFormulaType.SHARED, v15.getCTCell().getF().getT()); assertEquals("U16/R16", v16.getCellFormula()); assertEquals(STCellFormulaType.NORMAL, v16.getCTCell().getF().getT()); //anomaly in original file + assertEquals("U17/R17", v17.getCellFormula()); + assertEquals(STCellFormulaType.SHARED, v17.getCTCell().getF().getT()); int calcChainSize = wb.getCalculationChain().getCTCalcChain().sizeOfCArray(); v15.removeFormula(); assertEquals(CellType.NUMERIC, v15.getCellType(), "V15 is no longer a function"); assertNull(v15.getCTCell().getF(), "V15 xmlbeans function removed"); assertEquals("U16/R16", v16.getCellFormula()); - assertEquals(STCellFormulaType.SHARED, v16.getCTCell().getF().getT()); + assertEquals(STCellFormulaType.NORMAL, v16.getCTCell().getF().getT()); + assertEquals("U17/R17", v17.getCellFormula()); + assertEquals(STCellFormulaType.SHARED, v17.getCTCell().getF().getT()); assertEquals(calcChainSize - 1, wb.getCalculationChain().getCTCalcChain().sizeOfCArray()); } } + + @Test + void testSetBlankOnNestedSharedFormulas() throws IOException { + try (XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("testSharedFormulasSetBlank.xlsx")) { + XSSFSheet s1 = wb1.getSheetAt(0); + assertNotNull(s1); + Iterator<Row> rowIterator = s1.rowIterator(); + int count = 0; + StringBuilder sb = new StringBuilder(); + while (rowIterator.hasNext()) { + Row row = rowIterator.next(); + Iterator<Cell> cellIterator = row.cellIterator(); + while (cellIterator.hasNext()) { + Cell cell = cellIterator.next(); + + // the toString is needed to exhibit the broken state + sb.append(cell.toString()).append(","); + count++; + + // breaks the sheet state + cell.setBlank(); + } + } + assertEquals(10, count); + assertEquals("2-1,2-1,1+2,2-1,2-1,3+3,3+3,3+3,2-1,2-1,", sb.toString()); + } + } } diff --git a/test-data/spreadsheet/testSharedFormulasSetBlank.xlsx b/test-data/spreadsheet/testSharedFormulasSetBlank.xlsx Binary files differnew file mode 100644 index 0000000000..e5aa087287 --- /dev/null +++ b/test-data/spreadsheet/testSharedFormulasSetBlank.xlsx |