apache / apache/poi

XSLF - Inserting XSLFChart fails when existing XMLSlideShow has Two or more embedded Excel spreadsheets.

Open
#1,002 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.3k
Forks
844
Avg merge
5h 52m
Merged PRs (30d)
47

Description

When a PowerPoint file that has two or more existing embedded Excel spreadsheets is opened and a chart is inserted saving the .pptx file fails with an org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException.

This is a result of XSLFChart (XDDFChart) storing it's data as an XSSFWorkbook. Although the chart index is considered in the XMLSlideShow.findNextAvailableFileNameIndex method (via XSLFRelation.CHART) the workbook index is not.

Workbook index state is not managed as closely as the chart indexes. It appears that they need to be known to the XMLSlideShow, as multiple charts on a pptx with multiple embedded Excel files will also fail.

Our temporary solution was to add an int workbookIndex property to the XDDFChart class, as well as a currentWorkbookIndex int property in the XMLSlideShow class, and manage them in the XMLSlideShow class:

public XSLFChart createChart() {

int chartIdx = findNextAvailableFileNameIndex(XSLFRelation.CHART);
int workbookIdx = findNextAvailableFileNameIndex(XSLFRelation.WORKBOOK);
XSLFChart chart = createRelationship(XSLFRelation.CHART, XSLFFactory.getInstance(), chartIdx, true).getDocumentPart();
chart.setChartIndex(chartIdx);
if (workbookIdx > currentWorkbookIndex ) {
this.currentWorkbookIndex = workbookIdx;
} else {
this.currentWorkbookIndex++;
}
chart.setWorkbookIndex(currentWorkbookIndex);
_charts.add(chart);
return chart;
}

Hopefully you guys knowing the model better can find a more elegant solution.
Thanks.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in XMLSlideShow.createChart() and findNextAvailableFileNameIndex(), then trace how XSLFChart/XDDFChart assigns chart and workbook indexes. Reproduce the failure with a presentation containing two or more embedded Excel spreadsheets and an inserted chart. Done means saving such presentations succeeds, including when multiple charts and embedded workbooks are present.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.