Return-Path: Delivered-To: apmail-poi-commits-archive@minotaur.apache.org Received: (qmail 93221 invoked from network); 14 Dec 2010 06:41:35 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 14 Dec 2010 06:41:35 -0000 Received: (qmail 86452 invoked by uid 500); 14 Dec 2010 06:41:35 -0000 Delivered-To: apmail-poi-commits-archive@poi.apache.org Received: (qmail 86415 invoked by uid 500); 14 Dec 2010 06:41:34 -0000 Mailing-List: contact commits-help@poi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@poi.apache.org Delivered-To: mailing list commits@poi.apache.org Received: (qmail 86408 invoked by uid 99); 14 Dec 2010 06:41:33 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 14 Dec 2010 06:41:33 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 14 Dec 2010 06:41:30 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id DB94423888E7; Tue, 14 Dec 2010 06:41:08 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@poi.apache.org From: nick@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101214064108.DB94423888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ + 50416 - Correct shifting of the first or last row in a sheet by multiple rows 50440 - Support evaluating formulas with newlines in them, which XSSF may have (but HSSF may not) Added inline string support to XSSF EventModel 50246 - Properly position GutsRecord when reading HSSF workbooks 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