Bugzilla 51790: fixed reading shared formulas in XSSF

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1294127 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2012-02-27 12:18:45 +00:00
parent 41ac3b51d3
commit a6f40e2952
4 changed files with 47 additions and 1 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.8-beta6" date="2012-??-??">
<action dev="poi-developers" type="add">51710 - fixed reading shared formulas in XSSF </action>
<action dev="poi-developers" type="add">52708 - misc improvements in CellFormat </action>
<action dev="poi-developers" type="add">52690 - added a getter for length of encrypted data in Ecma and Agile decryptors</action>
<action dev="poi-developers" type="fix">52255 - support adding TIFF,EPS and WPG pictures in OOXML documents </action>

View File

@ -2670,7 +2670,20 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
if (f != null && f.getT() == STCellFormulaType.SHARED && f.isSetRef() && f.getStringValue() != null) {
// save a detached copy to avoid XmlValueDisconnectedException,
// this may happen when the master cell of a shared formula is changed
sharedFormulas.put((int)f.getSi(), (CTCellFormula)f.copy());
CTCellFormula sf = (CTCellFormula)f.copy();
CellRangeAddress sfRef = CellRangeAddress.valueOf(sf.getRef());
CellReference cellRef = new CellReference(cell);
// If the shared formula range preceeds the master cell then the preseeing part is discarded, e.g.
// if the cell is E60 and the shared formula range is C60:M85 then the effective range is E60:M85
// see more details in https://issues.apache.org/bugzilla/show_bug.cgi?id=51710
if(cellRef.getCol() > sfRef.getFirstColumn() || cellRef.getRow() > sfRef.getFirstRow()){
String effectiveRef = new CellRangeAddress(
Math.max(cellRef.getRow(), sfRef.getFirstRow()), sfRef.getLastRow(),
Math.max(cellRef.getCol(), sfRef.getFirstColumn()), sfRef.getLastColumn()).formatAsString();
sf.setRef(effectiveRef);
}
sharedFormulas.put((int)f.getSi(), sf);
}
if (f != null && f.getT() == STCellFormulaType.ARRAY && f.getRef() != null) {
arrayFormulas.add(CellRangeAddress.valueOf(f.getRef()));

View File

@ -44,6 +44,7 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.CalculationChain;
@ -1285,4 +1286,35 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
assertEquals(20.0, c1.getNumericCellValue());
assertEquals(20.0, c2.getNumericCellValue());
}
/**
* Bugzilla 51710: problems reading shared formuals from .xlsx
*/
public void test51710() {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("51790.xlsx");
final String[] columns = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N"};
final int rowMax = 500; // bug triggers on row index 59
Sheet sheet = wb.getSheetAt(0);
// go through all formula cells
for (int rInd = 2; rInd <= rowMax; rInd++) {
Row row = sheet.getRow(rInd);
for (int cInd = 1; cInd <= 12; cInd++) {
Cell cell = row.getCell(cInd);
String formula = cell.getCellFormula();
CellReference ref = new CellReference(cell);
//simulate correct answer
String correct = "$A" + (rInd + 1) + "*" + columns[cInd] + "$2";
assertEquals("Incorrect formula in " + ref.formatAsString(), correct, formula);
}
}
}
}

Binary file not shown.