]> source.dussan.org Git - poi.git/commitdiff
bug 59789: move HSSFComment shifting due to rowShift outside of for-loop for performance
authorJaven O'Neal <onealj@apache.org>
Mon, 4 Jul 2016 03:06:11 +0000 (03:06 +0000)
committerJaven O'Neal <onealj@apache.org>
Mon, 4 Jul 2016 03:06:11 +0000 (03:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751198 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java

index 9fe9c49c6b9a703f22ad4f206bc9078bfb7e3970..037c6b754859e16ab1337ba7f27661a96d7675d1 100644 (file)
@@ -40,6 +40,7 @@ import org.apache.poi.hssf.record.EscherAggregate;
 import org.apache.poi.hssf.record.ExtendedFormatRecord;
 import org.apache.poi.hssf.record.HyperlinkRecord;
 import org.apache.poi.hssf.record.NameRecord;
+import org.apache.poi.hssf.record.NoteRecord;
 import org.apache.poi.hssf.record.Record;
 import org.apache.poi.hssf.record.RecordBase;
 import org.apache.poi.hssf.record.RowRecord;
@@ -1523,6 +1524,17 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
     public void shiftRows(int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) {
         shiftRows(startRow, endRow, n, copyRowHeight, resetOriginalRowHeight, true);
     }
+    
+    /**
+     * bound a row number between 0 and last row index (65535)
+     *
+     * @param row the row number
+     */
+    private static int clip(int row) {
+        return Math.min(
+                Math.max(0, row),
+                SpreadsheetVersion.EXCEL97.getLastRowIndex());
+    }
 
     /**
      * Shifts rows between startRow and endRow n number of rows.
@@ -1558,11 +1570,27 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
             return;
         }
         
-        RowShifter rowShifter = new HSSFRowShifter(this);
-
-        // Shift comments
+        final RowShifter rowShifter = new HSSFRowShifter(this);
+        
+        // Move comments from the source row to the
+        //  destination row. Note that comments can
+        //  exist for cells which are null
+        // If the row shift would shift the comments off the sheet
+        // (above the first row or below the last row), this code will shift the
+        // comments to the first or last row, rather than moving them out of
+        // bounds or deleting them
         if (moveComments) {
-            _sheet.getNoteRecords();
+            final HSSFPatriarch patriarch = createDrawingPatriarch();
+            for (final HSSFShape shape : patriarch.getChildren()) {
+                if (!(shape instanceof HSSFComment)) {
+                    continue;
+                }
+                final HSSFComment comment = (HSSFComment) shape;
+                final int r = comment.getRow();
+                if (startRow <= r && r <= endRow) {
+                    comment.setRow(clip(r + n));
+                }
+            }
         }
 
         // Shift Merged Regions
@@ -1576,10 +1604,10 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
         final int lastOverwrittenRow = endRow + n;
         for (HSSFHyperlink link : getHyperlinkList()) {
             // If hyperlink is fully contained in the rows that will be overwritten, delete the hyperlink
-            if (firstOverwrittenRow <= link.getFirstRow() &&
-                    link.getFirstRow() <= lastOverwrittenRow &&
-                    lastOverwrittenRow <= link.getLastRow() &&
-                    link.getLastRow() <= lastOverwrittenRow) {
+            final int firstRow = link.getFirstRow();
+            final int lastRow = link.getLastRow();
+            if (firstOverwrittenRow <= firstRow && firstRow <= lastOverwrittenRow &&
+                    lastOverwrittenRow <= lastRow && lastRow <= lastOverwrittenRow) {
                 removeHyperlink(link);
             }
         }
@@ -1633,26 +1661,6 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
             }
             // Now zap all the cells in the source row
             row.removeAllCells();
-
-            // Move comments from the source row to the
-            //  destination row. Note that comments can
-            //  exist for cells which are null
-            if (moveComments) {
-                // This code would get simpler if NoteRecords could be organised by HSSFRow.
-                HSSFPatriarch patriarch = createDrawingPatriarch();
-                final int lastChildIndex = patriarch.getChildren().size() - 1;
-                for (int i = lastChildIndex; i >= 0; i--) {
-                    HSSFShape shape = patriarch.getChildren().get(i);
-                    if (!(shape instanceof HSSFComment)) {
-                        continue;
-                    }
-                    HSSFComment comment = (HSSFComment) shape;
-                    if (comment.getRow() != rowNum) {
-                        continue;
-                    }
-                    comment.setRow(rowNum + n);
-                }
-            }
         }
 
         // Re-compute the first and last rows of the sheet as needed
index d06da8b45834715f5a05608f8165863999da8fea..b16645e8f6366b5f75334c15c9c0327abc2446ff 100644 (file)
@@ -602,7 +602,7 @@ public abstract class BaseTestSheetShiftRows {
         read.close();
     }
     
-    // bug 56454
+    // bug 56454: Incorrectly handles merged regions that do not contain column 0
     @Ignore
     @Test
     public void shiftRowsWithMergedRegionsThatDoNotContainColumnZero() throws IOException {