diff options
author | Dominik Stadler <centic@apache.org> | 2021-11-02 13:17:53 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2021-11-02 13:17:53 +0000 |
commit | 7a5bc4227bbcb72a7e09adb6e7b6e829b52bdff8 (patch) | |
tree | b6cdd237fd0be1612db82d9d2a20bd09e842d4e4 | |
parent | d205318583e5dbae0d9ccd64cb55d6930fe285d8 (diff) | |
download | poi-7a5bc4227bbcb72a7e09adb6e7b6e829b52bdff8.tar.gz poi-7a5bc4227bbcb72a7e09adb6e7b6e829b52bdff8.zip |
Do not fail on different count of categories and points and fix cloning charts
We see many cases where documents have charts with a difference here, so
downgrade the exception to a warning for now
Also handle cloning charts in workbooks where the parts have unexpected names
by making sure that the chosen part-name is not yet taken.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894676 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java | 6 | ||||
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java | 15 | ||||
-rw-r--r-- | test-data/spreadsheet/clone_sheet.xlsx | bin | 0 -> 12465 bytes |
3 files changed, 19 insertions, 2 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java b/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java index 3074f4f486..91ce35e964 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java @@ -23,6 +23,8 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.poi.ss.util.CellReference; import org.apache.poi.util.Beta; import org.apache.poi.util.Internal; @@ -45,6 +47,8 @@ import org.openxmlformats.schemas.drawingml.x2006.chart.CTUnsignedInt; */ @Beta public abstract class XDDFChartData { + private static final Logger LOGGER = LogManager.getLogger(XDDFChartData.class); + protected XDDFChart parent; protected List<Series> series; private XDDFCategoryAxis categoryAxis; @@ -166,7 +170,7 @@ public abstract class XDDFChartData { if (categoryData != null && values != null) { int numOfPoints = category.getPointCount(); if (numOfPoints != values.getPointCount()) { - throw new IllegalStateException("Category and values must have the same point count, but had " + + LOGGER.warn("Category and values must have the same point count, but had " + numOfPoints + " categories and " + values.getPointCount() + " values."); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java index b19f8e4611..ee953ba50c 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java @@ -33,6 +33,7 @@ import org.apache.logging.log4j.Logger; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackageRelationship; @@ -243,9 +244,21 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS protected RelationPart createChartRelationPart() { XSSFWorkbook wb = getSheet().getWorkbook(); XSSFFactory factory = wb == null ? XSSFFactory.getInstance() : wb.getXssfFactory(); - int chartNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.CHART.getContentType()) + OPCPackage pkg = getPackagePart().getPackage(); + int chartNumber = pkg.getPartsByContentType(XSSFRelation.CHART.getContentType()) .size() + 1; + // some broken files have incorrectly named package parts, + // so we need to avoid duplicates here by checking and increasing + // the part-number + try { + while (pkg.getPart(PackagingURIHelper.createPartName(XSSFRelation.CHART.getFileName(chartNumber))) != null) { + chartNumber++; + } + } catch (InvalidFormatException e) { + throw new IllegalStateException("Failed for " + chartNumber, e); + } + return createRelationship(XSSFRelation.CHART, factory, chartNumber, false); } diff --git a/test-data/spreadsheet/clone_sheet.xlsx b/test-data/spreadsheet/clone_sheet.xlsx Binary files differnew file mode 100644 index 0000000000..80b5854eaa --- /dev/null +++ b/test-data/spreadsheet/clone_sheet.xlsx |