Fix bug #50416 - Correct shifting of the first or last row in a sheet by multiple rows
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1048951 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3d74ebf836
commit
63bc348d04
@ -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>
|
||||
|
@ -1221,10 +1221,14 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
|
||||
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 org.apache.poi.ss.usermodel.Sheet {
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user