Browse Source

[bug-65268] issue reading shared formulas

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894064 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_0
PJ Fanning 2 years ago
parent
commit
08a1f9af22

+ 9
- 13
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java View File

XSSFCell nextCell = row.getCell(j); XSSFCell nextCell = row.getCell(j);
if(nextCell != null && nextCell != cell && nextCell.getCellType() == CellType.FORMULA) { if(nextCell != null && nextCell != cell && nextCell.getCellType() == CellType.FORMULA) {
CTCellFormula nextF = nextCell.getCTCell().getF(); 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;
} }
} }
} }
} }
} }

} }
} }



+ 34
- 10
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java View File

import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Instant; 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.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile; import org.apache.commons.compress.archivers.zip.ZipFile;
XSSFSheet sheet = wb.getSheet("SheetWithSharedFormula"); XSSFSheet sheet = wb.getSheet("SheetWithSharedFormula");
XSSFCell v15 = sheet.getRow(14).getCell(21); XSSFCell v15 = sheet.getRow(14).getCell(21);
XSSFCell v16 = sheet.getRow(15).getCell(21); XSSFCell v16 = sheet.getRow(15).getCell(21);
XSSFCell v17 = sheet.getRow(16).getCell(21);
assertEquals("U15/R15", v15.getCellFormula()); assertEquals("U15/R15", v15.getCellFormula());
assertEquals(STCellFormulaType.SHARED, v15.getCTCell().getF().getT()); assertEquals(STCellFormulaType.SHARED, v15.getCTCell().getF().getT());
assertEquals("U16/R16", v16.getCellFormula()); assertEquals("U16/R16", v16.getCellFormula());
assertEquals(STCellFormulaType.NORMAL, v16.getCTCell().getF().getT()); //anomaly in original file 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(); int calcChainSize = wb.getCalculationChain().getCTCalcChain().sizeOfCArray();


v15.removeFormula(); v15.removeFormula();
assertEquals(CellType.NUMERIC, v15.getCellType(), "V15 is no longer a function"); assertEquals(CellType.NUMERIC, v15.getCellType(), "V15 is no longer a function");
assertNull(v15.getCTCell().getF(), "V15 xmlbeans function removed"); assertNull(v15.getCTCell().getF(), "V15 xmlbeans function removed");
assertEquals("U16/R16", v16.getCellFormula()); 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()); 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());
}
}
} }

BIN
test-data/spreadsheet/testSharedFormulasSetBlank.xlsx View File


Loading…
Cancel
Save