]> source.dussan.org Git - poi.git/commitdiff
Bug 59665: Using HSSFWorkbook#setSheetOrder to move sheets to the end corrupts bspos...
authorDominik Stadler <centic@apache.org>
Sun, 17 Jul 2016 09:27:07 +0000 (09:27 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 17 Jul 2016 09:27:07 +0000 (09:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753038 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/model/InternalWorkbook.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java

index 4c0fa6cae9f776f836edc7def4962aa164f4854d..84c6073c9d4259c14cec7611a190003814cdbe17 100644 (file)
@@ -655,17 +655,18 @@ public final class InternalWorkbook {
      * @param sheetname the name of the sheet to reorder
      * @param pos the position that we want to insert the sheet into (0 based)
      */
-
     public void setSheetOrder(String sheetname, int pos ) {
         int sheetNumber = getSheetIndex(sheetname);
         //remove the sheet that needs to be reordered and place it in the spot we want
         boundsheets.add(pos, boundsheets.remove(sheetNumber));
         
         // also adjust order of Records, calculate the position of the Boundsheets via getBspos()...
-        int pos0 = records.getBspos() - (boundsheets.size() - 1);
+        int initialBspos = records.getBspos();
+        int pos0 = initialBspos - (boundsheets.size() - 1);
         Record removed = records.get(pos0 + sheetNumber);
         records.remove(pos0 + sheetNumber);
                records.add(pos0 + pos, removed);
+        records.setBspos(initialBspos);
     }
 
     /**
index 42771aa8bf7b512090ce56fe34b1274babbabb32..8ddd002f726561cae06f25b4c3fedc6300146bcb 100644 (file)
@@ -1197,4 +1197,17 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
         wb = new HSSFWorkbook(new FileInputStream(file));
         assertCloseDoesNotModifyFile(filename, wb);
     }
+
+    @Test
+    public void setSheetOrderToEnd() throws Exception {
+        final HSSFWorkbook workbook = new HSSFWorkbook();
+        workbook.createSheet("A");
+        try {
+            for (int i = 0; i < 2 * workbook.getInternalWorkbook().getRecords().size(); i++) {
+                workbook.setSheetOrder("A", 0);
+            }
+        } catch (Exception e) {
+            throw new Exception("Moving a sheet to the end should not throw an exception, but threw ", e);
+        }
+    }
 }