poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1048951 - in /poi/trunk/src: documentation/content/xdocs/status.xml java/org/apache/poi/hssf/usermodel/HSSFSheet.java testcases/org/apache/poi/hssf/usermodel/TestBugs.java
Date Tue, 14 Dec 2010 06:41:08 GMT
Author: nick
Date: Tue Dec 14 06:41:08 2010
New Revision: 1048951

URL: http://svn.apache.org/viewvc?rev=1048951&view=rev
Log:
Fix bug #50416 - Correct shifting of the first or last row in a sheet by multiple rows

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1048951&r1=1048950&r2=1048951&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Tue Dec 14 06:41:08 2010
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta1" date="2010-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">50416 - Correct shifting of the
first or last row in a sheet by multiple rows</action>
            <action dev="POI-DEVELOPERS" type="fix">50440 - Support evaluating formulas
with newlines in them, which XSSF may have (but HSSF may not)</action>
            <action dev="POI-DEVELOPERS" type="add">Added inline string support to XSSF
EventModel</action>
            <action dev="POI-DEVELOPERS" type="fix">50246 - Properly position GutsRecord
when reading HSSF workbooks</action>

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java?rev=1048951&r1=1048950&r2=1048951&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java Tue Dec 14 06:41:08 2010
@@ -1221,10 +1221,14 @@ public final class HSSFSheet implements 
         if (n < 0) {
             s = startRow;
             inc = 1;
-        } else {
+        } else if (n > 0) {
             s = endRow;
             inc = -1;
+        } else {
+           // Nothing to do
+           return;
         }
+        
         NoteRecord[] noteRecs;
         if (moveComments) {
             noteRecs = _sheet.getNoteRecords();
@@ -1302,8 +1306,39 @@ public final class HSSFSheet implements 
                 }
             }
         }
-        if ( endRow == _lastrow || endRow + n > _lastrow ) _lastrow = Math.min( endRow
+ n, SpreadsheetVersion.EXCEL97.getLastRowIndex() );
-        if ( startRow == _firstrow || startRow + n < _firstrow ) _firstrow = Math.max(
startRow + n, 0 );
+        
+        // Re-compute the first and last rows of the sheet as needed
+        if(n > 0) {
+           // Rows are moving down
+           if ( startRow == _firstrow ) {
+              // Need to walk forward to find the first non-blank row
+              _firstrow = Math.max( startRow + n, 0 );
+              for( int i=startRow+1; i < startRow+n; i++ ) {
+                 if (getRow(i) != null) {
+                    _firstrow = i;
+                    break;
+                 }
+              }
+           }
+           if ( endRow + n > _lastrow ) {
+              _lastrow = Math.min( endRow + n, SpreadsheetVersion.EXCEL97.getLastRowIndex()
);
+           }
+        } else {
+           // Rows are moving up
+           if ( startRow + n < _firstrow ) {
+              _firstrow = Math.max( startRow + n, 0 );
+           }
+           if ( endRow == _lastrow  ) {
+              // Need to walk backward to find the last non-blank row
+              _lastrow = Math.min( endRow + n, SpreadsheetVersion.EXCEL97.getLastRowIndex()
);
+              for (int i=endRow-1; i > endRow+n; i++) {
+                 if (getRow(i) != null) {
+                    _lastrow = i;
+                    break;
+                 }
+              }
+           }
+        }
 
         // Update any formulas on this sheet that point to
         //  rows which have been moved

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java?rev=1048951&r1=1048950&r2=1048951&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java Tue Dec 14 06:41:08
2010
@@ -1899,4 +1899,58 @@ if(1==2) {
        HSSFWorkbook wb = openSample("50426.xls");
        writeOutAndReadBack(wb);
     }
+    
+    /**
+     * Last row number when shifting rows
+     */
+    public void test50416LastRowNumber() {
+       // Create the workbook with 1 sheet which contains 3 rows
+       HSSFWorkbook workbook = new HSSFWorkbook();
+       Sheet sheet = workbook.createSheet("Bug50416");
+       Row row1 = sheet.createRow(0);
+       Cell cellA_1 = row1.createCell(0,Cell.CELL_TYPE_STRING);
+       cellA_1.setCellValue("Cell A,1");
+       Row row2 = sheet.createRow(1);
+       Cell cellA_2 = row2.createCell(0,Cell.CELL_TYPE_STRING);
+       cellA_2.setCellValue("Cell A,2");
+       Row row3 = sheet.createRow(2);
+       Cell cellA_3 = row3.createCell(0,Cell.CELL_TYPE_STRING);
+       cellA_3.setCellValue("Cell A,3");
+       
+       // Test the last Row number it currently correct
+       assertEquals(2, sheet.getLastRowNum());
+       
+       // Shift the first row to the end
+       sheet.shiftRows(0, 0, 3);
+       assertEquals(3, sheet.getLastRowNum());
+       assertEquals(-1,         sheet.getRow(0).getLastCellNum());
+       assertEquals("Cell A,2", sheet.getRow(1).getCell(0).getStringCellValue());
+       assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
+       assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
+       
+       // Shift the 2nd row up to the first one
+       sheet.shiftRows(1, 1, -1);
+       assertEquals(3, sheet.getLastRowNum());
+       assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
+       assertEquals(-1,         sheet.getRow(1).getLastCellNum());
+       assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
+       assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
+
+       // Shift the 4th row up into the gap in the 3rd row
+       sheet.shiftRows(3, 3, -2);
+       assertEquals(2, sheet.getLastRowNum());
+       assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
+       assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
+       assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
+       assertEquals(-1,         sheet.getRow(3).getLastCellNum());
+       
+       // Now zap the empty 4th row - won't do anything
+       sheet.removeRow(sheet.getRow(3));
+       
+       // Test again the last row number which should be 2
+       assertEquals(2, sheet.getLastRowNum());
+       assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
+       assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
+       assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
+    }
 }



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


Mime
View raw message