Browse Source

[bug-66216] fix issue where pivotTable.getPivotCacheDefinition() returns null

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903442 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_3
PJ Fanning 1 year ago
parent
commit
3184a18b40

+ 1
- 1
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java View File

private CTPivotCacheDefinition ctPivotCacheDefinition; private CTPivotCacheDefinition ctPivotCacheDefinition;


@Beta @Beta
public XSSFPivotCacheDefinition(){
public XSSFPivotCacheDefinition() {
super(); super();
ctPivotCacheDefinition = CTPivotCacheDefinition.Factory.newInstance(); ctPivotCacheDefinition = CTPivotCacheDefinition.Factory.newInstance();
createDefaultValues(); createDefaultValues();

+ 12
- 0
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java View File

//Removing root element //Removing root element
options.setLoadReplaceDocumentElement(null); options.setLoadReplaceDocumentElement(null);
pivotTableDefinition = CTPivotTableDefinition.Factory.parse(is, options); pivotTableDefinition = CTPivotTableDefinition.Factory.parse(is, options);
pivotCacheDefinition = null;
} catch (XmlException e) { } catch (XmlException e) {
throw new IOException(e.getLocalizedMessage()); throw new IOException(e.getLocalizedMessage());
} }
} }


private void lazyInitXSSFPivotCacheDefinition() {
for (POIXMLDocumentPart documentPart : getRelations()) {
if (documentPart instanceof XSSFPivotCacheDefinition) {
pivotCacheDefinition = (XSSFPivotCacheDefinition) documentPart;
}
}
}

@Beta @Beta
public void setPivotCache(XSSFPivotCache pivotCache) { public void setPivotCache(XSSFPivotCache pivotCache) {
this.pivotCache = pivotCache; this.pivotCache = pivotCache;


@Beta @Beta
public XSSFPivotCacheDefinition getPivotCacheDefinition() { public XSSFPivotCacheDefinition getPivotCacheDefinition() {
if (pivotCacheDefinition == null) {
lazyInitXSSFPivotCacheDefinition();
}
return pivotCacheDefinition; return pivotCacheDefinition;
} }



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

assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), cv1.getErrorValue()); assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), cv1.getErrorValue());
} }
} }

@Test
void testBug66216() throws IOException {
File file = XSSFTestDataSamples.getSampleFile("ExcelPivotTableSample.xlsx");
try (
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(fis)
) {
for (XSSFPivotTable pivotTable : workbook.getPivotTables()) {
assertNotNull(pivotTable.getCTPivotTableDefinition());
assertNotNull(pivotTable.getPivotCacheDefinition());
assertEquals(1, pivotTable.getRelations().size());
assertInstanceOf(XSSFPivotCacheDefinition.class, pivotTable.getRelations().get(0));
assertEquals("rId1", pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getId());
assertEquals(3,
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getRecordCount());
}
}
}
} }

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


Loading…
Cancel
Save