Author: fanningpj
Date: Sat Aug 13 18:11:16 2022
New Revision: 1903398

URL: http://svn.apache.org/viewvc?rev=1903398&view=rev
Log:
[bug-66213] hack clone table code to avoid failing with edge cases

Modified:
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
    
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java
    poi/trunk/src/resources/ooxml-lite-report.clazz

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=1903398&r1=1903397&r2=1903398&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java 
(original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java 
Sat Aug 13 18:11:16 2022
@@ -4271,7 +4271,7 @@ public class XSSFSheet extends POIXMLDoc
 
         tables.put(tbl.getId(), table);
 
-        if(tableArea != null) {
+        if(tableArea != null && table.supportsAreaReference(tableArea)) {
             table.setArea(tableArea);
         }
 
@@ -4921,7 +4921,8 @@ public class XSSFSheet extends POIXMLDoc
             }
 
             // clone calculated column formulas
-            if 
(clonedTable.getCTTable().getTableColumns().getTableColumnList().size() > 0) {
+            if (clonedTable.getCTTable().getTableColumns() != null
+                    && 
clonedTable.getCTTable().getTableColumns().getTableColumnList().size() > 0) {
                 clonedTable.getCTTable().setTotalsRowCount(totalsRowCount);
                 for (int i = 0; i < 
clonedTable.getCTTable().getTableColumns().getTableColumnList().size(); i++) {
                     CTTableColumn tableCol = 
table.getCTTable().getTableColumns().getTableColumnList().get(i);

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java?rev=1903398&r1=1903397&r2=1903398&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java 
(original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java 
Sat Aug 13 18:11:16 2022
@@ -28,6 +28,8 @@ import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.ConcurrentSkipListMap;
 
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.apache.poi.ooxml.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.ss.SpreadsheetVersion;
@@ -56,6 +58,8 @@ import org.openxmlformats.schemas.spread
  */
 public class XSSFTable extends POIXMLDocumentPart implements Table {
 
+    private static final Logger LOG = LogManager.getLogger(XSSFTable.class);
+
     private CTTable ctTable;
     private transient List<XSSFXmlColumnPr> xmlColumnPrs;
     private transient List<XSSFTableColumn> tableColumns;
@@ -399,7 +403,11 @@ public class XSSFTable extends POIXMLDoc
     public void setStyleName(String newStyleName) {
         if (newStyleName == null) {
             if (ctTable.isSetTableStyleInfo()) {
-                ctTable.getTableStyleInfo().unsetName();
+                try {
+                    ctTable.getTableStyleInfo().unsetName();
+                } catch (Exception e) {
+                    LOG.atDebug().log("Failed to unset style name", e);
+                }
             }
             styleName = null;
             return;
@@ -498,6 +506,12 @@ public class XSSFTable extends POIXMLDoc
         updateHeaders();
     }
 
+    boolean supportsAreaReference(final AreaReference tableArea) {
+        int rowCount = (tableArea.getLastCell().getRow() - 
tableArea.getFirstCell().getRow()) + 1;
+        int minimumRowCount = 1 + getHeaderRowCount() + getTotalsRowCount();
+        return rowCount >= minimumRowCount;
+    }
+
     /**
      * Set the area reference for the cells which this table covers. The area
      * includes includes header rows and totals rows.
@@ -525,9 +539,8 @@ public class XSSFTable extends POIXMLDoc
                     "The AreaReference must not reference a different sheet");
         }
 
-        int rowCount = (tableArea.getLastCell().getRow() - 
tableArea.getFirstCell().getRow()) + 1;
-        int minimumRowCount = 1 + getHeaderRowCount() + getTotalsRowCount();
-        if (rowCount < minimumRowCount) {
+        if (!supportsAreaReference(tableArea)) {
+            int minimumRowCount = 1 + getHeaderRowCount() + 
getTotalsRowCount();
             throw new IllegalArgumentException("AreaReference needs at least " 
+ minimumRowCount
                     + " rows, to cover at least one data row and all header 
rows and totals rows");
         }

Modified: 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java?rev=1903398&r1=1903397&r2=1903398&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFTable.java
 Sat Aug 13 18:11:16 2022
@@ -752,4 +752,36 @@ public final class TestXSSFTable {
             }
         }
     }
+
+    @Test
+    void testCloneConditionalFormattingSamples() throws IOException {
+        try (XSSFWorkbook wb = 
XSSFTestDataSamples.openSampleWorkbook("ConditionalFormattingSamples.xlsx")) {
+            wb.cloneSheet(0, "Test");
+            try (UnsynchronizedByteArrayOutputStream bos = new 
UnsynchronizedByteArrayOutputStream()) {
+                wb.write(bos);
+                try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) 
{
+                    XSSFSheet sheet0 = wb2.getSheetAt(0);
+                    XSSFSheet sheet1 = wb2.getSheetAt(1);
+                    assertEquals(0, sheet0.getTables().size());
+                    assertEquals(0, sheet1.getTables().size());
+                }
+            }
+        }
+    }
+
+    @Test
+    void testCloneSingleCellTable() throws IOException {
+        try (XSSFWorkbook wb = 
XSSFTestDataSamples.openSampleWorkbook("SingleCellTable.xlsx")) {
+            wb.cloneSheet(0, "Test");
+            try (UnsynchronizedByteArrayOutputStream bos = new 
UnsynchronizedByteArrayOutputStream()) {
+                wb.write(bos);
+                try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) 
{
+                    XSSFSheet sheet0 = wb2.getSheetAt(0);
+                    XSSFSheet sheet1 = wb2.getSheetAt(1);
+                    assertEquals(1, sheet0.getTables().size());
+                    assertEquals(1, sheet1.getTables().size());
+                }
+            }
+        }
+    }
 }

Modified: poi/trunk/src/resources/ooxml-lite-report.clazz
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/resources/ooxml-lite-report.clazz?rev=1903398&r1=1903397&r2=1903398&view=diff
==============================================================================
--- poi/trunk/src/resources/ooxml-lite-report.clazz (original)
+++ poi/trunk/src/resources/ooxml-lite-report.clazz Sat Aug 13 18:11:16 2022
@@ -2513,3 +2513,7 @@ org/openxmlformats/schemas/drawingml/x20
 org/openxmlformats/schemas/drawingml/x2006/chart/STDLblPos$Enum
 org/openxmlformats/schemas/drawingml/x2006/chart/impl/STDLblPosImpl
 org/openxmlformats/schemas/drawingml/x2006/chart/STDLblPos
+org/openxmlformats/schemas/spreadsheetml/x2006/main/impl/STTotalsRowFunctionImpl
+org/openxmlformats/schemas/spreadsheetml/x2006/main/STTotalsRowFunction
+org/openxmlformats/schemas/spreadsheetml/x2006/main/impl/CTTableFormulaImpl
+org/openxmlformats/schemas/spreadsheetml/x2006/main/CTTableFormula



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to