You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2023/01/28 20:13:34 UTC

svn commit: r1907065 - in /poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf: streaming/SXSSFSheet.java usermodel/XSSFSheet.java

Author: fanningpj
Date: Sat Jan 28 20:13:33 2023
New Revision: 1907065

URL: http://svn.apache.org/viewvc?rev=1907065&view=rev
Log:
try to improve performance of XSSFSheet row/col grouping/ungrouping

Modified:
    poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java
    poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java

Modified: poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java?rev=1907065&r1=1907064&r2=1907065&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java (original)
+++ poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java Sat Jan 28 20:13:33 2023
@@ -1245,7 +1245,7 @@ public class SXSSFSheet implements Sheet
     }
 
     /**
-     * Ungroup a range of columns that were previously groupped
+     * Ungroup a range of columns that were previously grouped
      *
      * @param fromColumn   start column (0-based)
      * @param toColumn     end column (0-based)
@@ -1340,7 +1340,7 @@ public class SXSSFSheet implements Sheet
     }
 
     /**
-     * Ungroup a range of rows that were previously groupped
+     * Ungroup a range of rows that were previously grouped
      *
      * @param fromRow   start row (0-based)
      * @param toRow     end row (0-based)
@@ -1355,7 +1355,7 @@ public class SXSSFSheet implements Sheet
      *
      * <i>Not implemented for expanding (i.e. collapse == false)</i>
      *
-     * @param row   start row of a groupped range of rows (0-based)
+     * @param row   start row of a grouped range of rows (0-based)
      * @param collapse whether to expand/collapse the detail rows
      * @throws IllegalStateException if collapse is false as this is not implemented for SXSSF.
      */

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=1907065&r1=1907064&r2=1907065&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 Jan 28 20:13:33 2023
@@ -1695,15 +1695,20 @@ public class XSSFSheet extends POIXMLDoc
             this.columnHelper.setColumnAttributes(fixCol_before, fixCol_after);
         }
 
-        for(int index=fromColumn;index<=toColumn;index++){
-            CTCol col=columnHelper.getColumn1Based(index, false);
+        int maxLevelCol = -1;
+        for(int index = fromColumn; index <= toColumn; index++){
+            CTCol col = columnHelper.getColumn1Based(index, false);
             //col must exist
-            short outlineLevel=col.getOutlineLevel();
-            col.setOutlineLevel((short)(outlineLevel+1));
+            final short outlineLevel = col.getOutlineLevel();
+            final int newOutlineLevel = outlineLevel + 1;
+            col.setOutlineLevel((short) newOutlineLevel);
+            maxLevelCol = Math.max(maxLevelCol, newOutlineLevel);
             index = Math.toIntExact(col.getMax());
         }
-        worksheet.setColsArray(0,ctCols);
-        setSheetFormatPrOutlineLevelCol();
+        worksheet.setColsArray(0, ctCols);
+        if (maxLevelCol > getSheetFormatPrOutlineLevelCol()) {
+            increaseSheetFormatPrOutlineLevelColIfNecessary((short) maxLevelCol);
+        }
     }
 
     /**
@@ -1726,16 +1731,21 @@ public class XSSFSheet extends POIXMLDoc
      */
     @Override
     public void groupRow(int fromRow, int toRow) {
+        int maxOutlineLevel = -1;
         for (int i = fromRow; i <= toRow; i++) {
             XSSFRow xrow = getRow(i);
             if (xrow == null) {
                 xrow = createRow(i);
             }
             CTRow ctrow = xrow.getCTRow();
-            short outlineLevel = ctrow.getOutlineLevel();
-            ctrow.setOutlineLevel((short) (outlineLevel + 1));
+            final short outlineLevel = ctrow.getOutlineLevel();
+            final int newOutlineLevel = outlineLevel + 1;
+            maxOutlineLevel = Math.max(maxOutlineLevel, newOutlineLevel);
+            ctrow.setOutlineLevel((short) newOutlineLevel);
+        }
+        if (maxOutlineLevel >= 0) {
+            increaseSheetFormatPrOutlineLevelRowIfNecessary((short) maxOutlineLevel);
         }
-        setSheetFormatPrOutlineLevelRow();
     }
 
     private short getMaxOutlineLevelRows(){
@@ -3391,10 +3401,12 @@ public class XSSFSheet extends POIXMLDoc
     @Override
     public void ungroupColumn(int fromColumn, int toColumn) {
         CTCols cols = worksheet.getColsArray(0);
+        int maxLevelCol = -1;
         for (int index = fromColumn; index <= toColumn; index++) {
             CTCol col = columnHelper.getColumn(index, false);
             if (col != null) {
-                short outlineLevel = col.getOutlineLevel();
+                final short outlineLevel = col.getOutlineLevel();
+                maxLevelCol = Math.max(maxLevelCol, outlineLevel);
                 col.setOutlineLevel((short) (outlineLevel - 1));
                 index = Math.toIntExact(col.getMax());
 
@@ -3405,39 +3417,65 @@ public class XSSFSheet extends POIXMLDoc
             }
         }
         worksheet.setColsArray(0, cols);
-        setSheetFormatPrOutlineLevelCol();
+        if (maxLevelCol >= getSheetFormatPrOutlineLevelCol()) {
+            setSheetFormatPrOutlineLevelCol();
+        }
     }
 
     /**
-     * Ungroup a range of rows that were previously groupped
+     * Ungroup a range of rows that were previously grouped
      *
      * @param fromRow   start row (0-based)
      * @param toRow     end row (0-based)
      */
     @Override
     public void ungroupRow(int fromRow, int toRow) {
+        int maxOutlineLevel = -1;
         for (int i = fromRow; i <= toRow; i++) {
             XSSFRow xrow = getRow(i);
             if (xrow != null) {
                 CTRow ctRow = xrow.getCTRow();
-                int outlineLevel = ctRow.getOutlineLevel();
+                final short outlineLevel = ctRow.getOutlineLevel();
                 ctRow.setOutlineLevel((short) (outlineLevel - 1));
+                maxOutlineLevel = Math.max(maxOutlineLevel, outlineLevel);
                 //remove a row only if the row has no cell and if the outline level is 0
                 if (outlineLevel == 1 && xrow.getFirstCellNum() == -1) {
                     removeRow(xrow);
                 }
             }
         }
-        setSheetFormatPrOutlineLevelRow();
+        if (maxOutlineLevel >= getSheetFormatPrOutlineLevelRow()) {
+            setSheetFormatPrOutlineLevelRow();
+        }
+    }
+
+    private void increaseSheetFormatPrOutlineLevelRowIfNecessary(final short levelRow) {
+        if (levelRow > getSheetFormatPrOutlineLevelRow()) {
+            getSheetTypeSheetFormatPr().setOutlineLevelRow(levelRow);
+        }
     }
 
-    private void setSheetFormatPrOutlineLevelRow(){
-        short maxLevelRow=getMaxOutlineLevelRows();
+    private void increaseSheetFormatPrOutlineLevelColIfNecessary(final short levelCol) {
+        if (levelCol > getSheetFormatPrOutlineLevelCol()) {
+            getSheetTypeSheetFormatPr().setOutlineLevelCol(levelCol);
+        }
+    }
+
+    private void setSheetFormatPrOutlineLevelRow() {
+        final short maxLevelRow = getMaxOutlineLevelRows();
         getSheetTypeSheetFormatPr().setOutlineLevelRow(maxLevelRow);
     }
 
-    private void setSheetFormatPrOutlineLevelCol(){
-        short maxLevelCol=getMaxOutlineLevelCols();
+    private short getSheetFormatPrOutlineLevelRow() {
+        return getSheetTypeSheetFormatPr().getOutlineLevelRow();
+    }
+
+    private short getSheetFormatPrOutlineLevelCol() {
+        return getSheetTypeSheetFormatPr().getOutlineLevelCol();
+    }
+
+    private void setSheetFormatPrOutlineLevelCol() {
+        final short maxLevelCol = getMaxOutlineLevelCols();
         getSheetTypeSheetFormatPr().setOutlineLevelCol(maxLevelCol);
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org