]> source.dussan.org Git - poi.git/commitdiff
Do not fail on different count of categories and points and fix cloning charts
authorDominik Stadler <centic@apache.org>
Tue, 2 Nov 2021 13:17:53 +0000 (13:17 +0000)
committerDominik Stadler <centic@apache.org>
Tue, 2 Nov 2021 13:17:53 +0000 (13:17 +0000)
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

poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java
test-data/spreadsheet/clone_sheet.xlsx [new file with mode: 0644]

index 3074f4f48601b63934f98d4d31cf547cafe54eb6..91ce35e9649debb335c182a6c41f439aea616f6a 100644 (file)
@@ -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.");
                 }
             }
index b19f8e4611201a513d3219724d8687d319d61b4b..ee953ba50c21918feb19ea5bc85e71bd6ebbc76f 100644 (file)
@@ -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
new file mode 100644 (file)
index 0000000..80b5854
Binary files /dev/null and b/test-data/spreadsheet/clone_sheet.xlsx differ