View | Details | Raw Unified | Return to bug 57171
Collapse All | Expand All

(-)a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (-4 / +25 lines)
Lines 480-485 public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss Link Here
480
480
481
        workbook.updateNamesAfterCellShift(shifter);
481
        workbook.updateNamesAfterCellShift(shifter);
482
482
483
        // adjust active sheet if necessary
484
        int active = getActiveSheetIndex();
485
        if(active == oldSheetIndex) {
486
            // moved sheet was the active one
487
            setActiveSheet(pos);
488
        } else if ((active < oldSheetIndex && active < pos) ||
489
                (active > oldSheetIndex && active > pos)) {
490
            // not affected
491
        } else if (pos > oldSheetIndex) {
492
            // moved sheet was below before and is above now => active is one less
493
            setActiveSheet(active-1);
494
        } else {
495
            // remaining case: moved sheet was higher than active before and is lower now => active is one more
496
            setActiveSheet(active+1);
497
        }
483
    }
498
    }
484
499
485
    private void validateSheetIndex(int index) {
500
    private void validateSheetIndex(int index) {
Lines 937-943 public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss Link Here
937
     */
952
     */
938
    public void removeSheetAt(int index) {
953
    public void removeSheetAt(int index) {
939
        validateSheetIndex(index);
954
        validateSheetIndex(index);
940
        boolean wasActive = getSheetAt(index).isActive();
941
        boolean wasSelected = getSheetAt(index).isSelected();
955
        boolean wasSelected = getSheetAt(index).isSelected();
942
956
943
        _sheets.remove(index);
957
        _sheets.remove(index);
Lines 954-962 public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss Link Here
954
        if (newSheetIndex >= nSheets) {
968
        if (newSheetIndex >= nSheets) {
955
            newSheetIndex = nSheets-1;
969
            newSheetIndex = nSheets-1;
956
        }
970
        }
957
        if (wasActive) {
958
            setActiveSheet(newSheetIndex);
959
        }
960
971
961
        if (wasSelected) {
972
        if (wasSelected) {
962
            boolean someOtherSheetIsStillSelected = false;
973
            boolean someOtherSheetIsStillSelected = false;
Lines 970-975 public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss Link Here
970
                setSelectedTab(newSheetIndex);
981
                setSelectedTab(newSheetIndex);
971
            }
982
            }
972
        }
983
        }
984
985
        // adjust active sheet
986
        int active = getActiveSheetIndex();
987
        if(active == index) {
988
            // removed sheet was the active one, reset active sheet if there is still one left now
989
            setActiveSheet(newSheetIndex);
990
        } else if (active > index) {
991
            // removed sheet was below the active one => active is one less now
992
            setActiveSheet(active-1);
993
        }
973
    }
994
    }
974
995
975
    /**
996
    /**
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (-1 / +41 lines)
Lines 727-733 public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X Link Here
727
        CTSheet sheet = addSheet(sheetname);
727
        CTSheet sheet = addSheet(sheetname);
728
728
729
        int sheetNumber = 1;
729
        int sheetNumber = 1;
730
        for(XSSFSheet sh : sheets) sheetNumber = (int)Math.max(sh.sheet.getSheetId() + 1, sheetNumber);
730
        for(XSSFSheet sh : sheets) {
731
            sheetNumber = (int)Math.max(sh.sheet.getSheetId() + 1, sheetNumber);
732
        }
731
733
732
        XSSFSheet wrapper = (XSSFSheet)createRelationship(XSSFRelation.WORKSHEET, XSSFFactory.getInstance(), sheetNumber);
734
        XSSFSheet wrapper = (XSSFSheet)createRelationship(XSSFRelation.WORKSHEET, XSSFFactory.getInstance(), sheetNumber);
733
        wrapper.sheet = sheet;
735
        wrapper.sheet = sheet;
Lines 1072-1077 public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X Link Here
1072
        XSSFSheet sheet = getSheetAt(index);
1074
        XSSFSheet sheet = getSheetAt(index);
1073
        removeRelation(sheet);
1075
        removeRelation(sheet);
1074
        sheets.remove(index);
1076
        sheets.remove(index);
1077
1078
        // only set new sheet if there are still some left
1079
        if(sheets.size() == 0) {
1080
            return;
1081
        }
1082
1083
        // the index of the closest remaining sheet to the one just deleted
1084
        int newSheetIndex = index;
1085
        if (newSheetIndex >= sheets.size()) {
1086
            newSheetIndex = sheets.size()-1;
1087
        }
1088
1089
        // adjust active sheet
1090
        int active = getActiveSheetIndex();
1091
        if(active == index) {
1092
            // removed sheet was the active one, reset active sheet if there is still one left now
1093
            setActiveSheet(newSheetIndex);
1094
        } else if (active > index) {
1095
            // removed sheet was below the active one => active is one less now
1096
            setActiveSheet(active-1);
1097
        }
1075
    }
1098
    }
1076
1099
1077
    /**
1100
    /**
Lines 1374-1379 public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X Link Here
1374
    public void setSheetOrder(String sheetname, int pos) {
1397
    public void setSheetOrder(String sheetname, int pos) {
1375
        int idx = getSheetIndex(sheetname);
1398
        int idx = getSheetIndex(sheetname);
1376
        sheets.add(pos, sheets.remove(idx));
1399
        sheets.add(pos, sheets.remove(idx));
1400
1377
        // Reorder CTSheets
1401
        // Reorder CTSheets
1378
        CTSheets ct = workbook.getSheets();
1402
        CTSheets ct = workbook.getSheets();
1379
        XmlObject cts = ct.getSheetArray(idx).copy();
1403
        XmlObject cts = ct.getSheetArray(idx).copy();
Lines 1386-1391 public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X Link Here
1386
        for(int i=0; i < sheetArray.length; i++) {
1410
        for(int i=0; i < sheetArray.length; i++) {
1387
            sheets.get(i).sheet = sheetArray[i];
1411
            sheets.get(i).sheet = sheetArray[i];
1388
        }
1412
        }
1413
1414
        // adjust active sheet if necessary
1415
        int active = getActiveSheetIndex();
1416
        if(active == idx) {
1417
            // moved sheet was the active one
1418
            setActiveSheet(pos);
1419
        } else if ((active < idx && active < pos) ||
1420
                (active > idx && active > pos)) {
1421
            // not affected
1422
        } else if (pos > idx) {
1423
            // moved sheet was below before and is above now => active is one less
1424
            setActiveSheet(active-1);
1425
        } else {
1426
            // remaining case: moved sheet was higher than active before and is lower now => active is one more
1427
            setActiveSheet(active+1);
1428
        }
1389
    }
1429
    }
1390
1430
1391
    /**
1431
    /**
(-)a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java (-7 / +160 lines)
Lines 29-35 import org.apache.poi.ss.util.CellRangeAddress; Link Here
29
import org.apache.poi.ss.util.CellUtil;
29
import org.apache.poi.ss.util.CellUtil;
30
import org.apache.poi.xssf.XSSFITestDataProvider;
30
import org.apache.poi.xssf.XSSFITestDataProvider;
31
import org.apache.poi.xssf.XSSFTestDataSamples;
31
import org.apache.poi.xssf.XSSFTestDataSamples;
32
import org.junit.Test;
33
32
34
/**
33
/**
35
 * @author Yegor Kozlov
34
 * @author Yegor Kozlov
Lines 190-202 public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows { Link Here
190
        assertEquals("Amdocs:\ntest\n", comment.getString().getString());
189
        assertEquals("Amdocs:\ntest\n", comment.getString().getString());
191
	}
190
	}
192
191
193
	@Test
192
	public void testBug55280() throws IOException {
194
	public void testBug55280() {
195
        Workbook w = new XSSFWorkbook();
193
        Workbook w = new XSSFWorkbook();
196
        Sheet s = w.createSheet();
194
        try {
197
        for (int row = 0; row < 5000; ++row)
195
            Sheet s = w.createSheet();
198
            s.addMergedRegion(new CellRangeAddress(row, row, 0, 3));
196
            for (int row = 0; row < 5000; ++row)
197
                s.addMergedRegion(new CellRangeAddress(row, row, 0, 3));
199
198
200
        s.shiftRows(0, 4999, 1);        // takes a long time...
199
            s.shiftRows(0, 4999, 1);        // takes a long time...
200
        } finally {
201
            w.close();
202
        }
201
	}
203
	}
204
205
    public void test57171() throws Exception {
206
	    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
207
        assertEquals(5, wb.getActiveSheetIndex());
208
        removeAllSheetsBut(5, wb); // 5 is the active / selected sheet
209
        assertEquals(0, wb.getActiveSheetIndex());
210
211
        Workbook wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
212
        assertEquals(0, wbRead.getActiveSheetIndex());
213
214
        wbRead.removeSheetAt(0);
215
        assertEquals(0, wbRead.getActiveSheetIndex());
216
217
        //wb.write(new FileOutputStream("/tmp/57171.xls"));
218
    }
219
220
    public void test57163() throws IOException {
221
        Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
222
        assertEquals(5, wb.getActiveSheetIndex());
223
        wb.removeSheetAt(0);
224
        assertEquals(4, wb.getActiveSheetIndex());
225
226
        //wb.write(new FileOutputStream("/tmp/57163.xls"));
227
    }
228
229
    public void testSetSheetOrderAndAdjustActiveSheet() throws Exception {
230
        Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
231
        
232
        assertEquals(5, wb.getActiveSheetIndex());
233
234
        // move the sheets around in all possible combinations to check that the active sheet
235
        // is set correctly in all cases
236
        wb.setSheetOrder(wb.getSheetName(5), 4);
237
        assertEquals(4, wb.getActiveSheetIndex());
238
        
239
        wb.setSheetOrder(wb.getSheetName(5), 5);
240
        assertEquals(4, wb.getActiveSheetIndex());
241
242
        wb.setSheetOrder(wb.getSheetName(3), 5);
243
        assertEquals(3, wb.getActiveSheetIndex());
244
245
        wb.setSheetOrder(wb.getSheetName(4), 5);
246
        assertEquals(3, wb.getActiveSheetIndex());
247
248
        wb.setSheetOrder(wb.getSheetName(2), 2);
249
        assertEquals(3, wb.getActiveSheetIndex());
250
251
        wb.setSheetOrder(wb.getSheetName(2), 1);
252
        assertEquals(3, wb.getActiveSheetIndex());
253
254
        wb.setSheetOrder(wb.getSheetName(3), 5);
255
        assertEquals(5, wb.getActiveSheetIndex());
256
257
        wb.setSheetOrder(wb.getSheetName(0), 5);
258
        assertEquals(4, wb.getActiveSheetIndex());
259
260
        wb.setSheetOrder(wb.getSheetName(0), 5);
261
        assertEquals(3, wb.getActiveSheetIndex());
262
263
        wb.setSheetOrder(wb.getSheetName(0), 5);
264
        assertEquals(2, wb.getActiveSheetIndex());
265
266
        wb.setSheetOrder(wb.getSheetName(0), 5);
267
        assertEquals(1, wb.getActiveSheetIndex());
268
269
        wb.setSheetOrder(wb.getSheetName(0), 5);
270
        assertEquals(0, wb.getActiveSheetIndex());
271
272
        wb.setSheetOrder(wb.getSheetName(0), 5);
273
        assertEquals(5, wb.getActiveSheetIndex());
274
    }   
275
276
    public void testRemoveSheetAndAdjustActiveSheet() throws Exception {
277
        Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
278
        
279
        assertEquals(5, wb.getActiveSheetIndex());
280
        
281
        wb.removeSheetAt(0);
282
        assertEquals(4, wb.getActiveSheetIndex());
283
        
284
        wb.setActiveSheet(3);
285
        assertEquals(3, wb.getActiveSheetIndex());
286
        
287
        wb.removeSheetAt(4);
288
        assertEquals(3, wb.getActiveSheetIndex());
289
290
        wb.removeSheetAt(3);
291
        assertEquals(2, wb.getActiveSheetIndex());
292
293
        wb.removeSheetAt(0);
294
        assertEquals(1, wb.getActiveSheetIndex());
295
296
        wb.removeSheetAt(1);
297
        assertEquals(0, wb.getActiveSheetIndex());
298
299
        wb.removeSheetAt(0);
300
        assertEquals(0, wb.getActiveSheetIndex());
301
302
        try {
303
            wb.removeSheetAt(0);
304
            fail("Should catch exception as no more sheets are there");
305
        } catch (IllegalArgumentException e) {
306
            // expected
307
        }
308
        assertEquals(0, wb.getActiveSheetIndex());
309
        
310
        wb.createSheet();
311
        assertEquals(0, wb.getActiveSheetIndex());
312
        
313
        wb.removeSheetAt(0);
314
        assertEquals(0, wb.getActiveSheetIndex());
315
    }
316
317
    // TODO: enable when bug 57165 is fixed
318
    public void disabled_test57165() throws IOException {
319
        Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
320
        assertEquals(5, wb.getActiveSheetIndex());
321
        removeAllSheetsBut(3, wb);
322
        assertEquals(0, wb.getActiveSheetIndex());
323
        wb.createSheet("New Sheet1");
324
        assertEquals(0, wb.getActiveSheetIndex());
325
        wb.cloneSheet(0); // Throws exception here
326
        wb.setSheetName(1, "New Sheet");
327
        assertEquals(0, wb.getActiveSheetIndex());
328
329
        //wb.write(new FileOutputStream("/tmp/57165.xls"));
330
    }
331
332
//    public void test57165b() throws IOException {
333
//        Workbook wb = new XSSFWorkbook();
334
//        try {
335
//            wb.createSheet("New Sheet 1");
336
//            wb.createSheet("New Sheet 2");
337
//        } finally {
338
//            wb.close();
339
//        }
340
//    }
341
342
    private static void removeAllSheetsBut(int sheetIndex, Workbook wb)
343
    {
344
        int sheetNb = wb.getNumberOfSheets();
345
        // Move this sheet at the first position
346
        wb.setSheetOrder(wb.getSheetName(sheetIndex), 0);
347
        // Must make this sheet active (otherwise, for XLSX, Excel might protest that active sheet no longer exists)
348
        // I think POI should automatically handle this case when deleting sheets...
349
//      wb.setActiveSheet(0);
350
        for (int sn = sheetNb - 1; sn > 0; sn--)
351
        {
352
            wb.removeSheetAt(sn);
353
        }
354
    }
202
}
355
}
(-)a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java (-1 / +35 lines)
Lines 192-211 public abstract class BaseTestWorkbook { Link Here
192
        workbook.createSheet("sheet2");
192
        workbook.createSheet("sheet2");
193
        workbook.createSheet("sheet3");
193
        workbook.createSheet("sheet3");
194
        assertEquals(3, workbook.getNumberOfSheets());
194
        assertEquals(3, workbook.getNumberOfSheets());
195
196
        assertEquals(0, workbook.getActiveSheetIndex());
197
195
        workbook.removeSheetAt(1);
198
        workbook.removeSheetAt(1);
196
        assertEquals(2, workbook.getNumberOfSheets());
199
        assertEquals(2, workbook.getNumberOfSheets());
197
        assertEquals("sheet3", workbook.getSheetName(1));
200
        assertEquals("sheet3", workbook.getSheetName(1));
201
        assertEquals(0, workbook.getActiveSheetIndex());
202
198
        workbook.removeSheetAt(0);
203
        workbook.removeSheetAt(0);
199
        assertEquals(1, workbook.getNumberOfSheets());
204
        assertEquals(1, workbook.getNumberOfSheets());
200
        assertEquals("sheet3", workbook.getSheetName(0));
205
        assertEquals("sheet3", workbook.getSheetName(0));
206
        assertEquals(0, workbook.getActiveSheetIndex());
207
201
        workbook.removeSheetAt(0);
208
        workbook.removeSheetAt(0);
202
        assertEquals(0, workbook.getNumberOfSheets());
209
        assertEquals(0, workbook.getNumberOfSheets());
210
        assertEquals(0, workbook.getActiveSheetIndex());
203
211
204
        //re-create the sheets
212
        //re-create the sheets
205
        workbook.createSheet("sheet1");
213
        workbook.createSheet("sheet1");
206
        workbook.createSheet("sheet2");
214
        workbook.createSheet("sheet2");
207
        workbook.createSheet("sheet3");
215
        workbook.createSheet("sheet3");
208
        assertEquals(3, workbook.getNumberOfSheets());
216
        workbook.createSheet("sheet4");
217
        assertEquals(4, workbook.getNumberOfSheets());
218
219
        assertEquals(0, workbook.getActiveSheetIndex());
220
        workbook.setActiveSheet(2);
221
        assertEquals(2, workbook.getActiveSheetIndex());
222
223
        workbook.removeSheetAt(2);
224
        assertEquals(2, workbook.getActiveSheetIndex());
225
226
        workbook.removeSheetAt(1);
227
        assertEquals(1, workbook.getActiveSheetIndex());
228
229
        workbook.removeSheetAt(0);
230
        assertEquals(0, workbook.getActiveSheetIndex());
231
232
        workbook.removeSheetAt(0);
233
        assertEquals(0, workbook.getActiveSheetIndex());
209
    }
234
    }
210
235
211
    @Test
236
    @Test
Lines 287-296 public abstract class BaseTestWorkbook { Link Here
287
        assertEquals(8, wb.getSheetIndex("Sheet 8"));
312
        assertEquals(8, wb.getSheetIndex("Sheet 8"));
288
        assertEquals(9, wb.getSheetIndex("Sheet 9"));
313
        assertEquals(9, wb.getSheetIndex("Sheet 9"));
289
314
315
        // check active sheet
316
        assertEquals(0, wb.getActiveSheetIndex());
317
        
290
        // Change
318
        // Change
291
        wb.setSheetOrder("Sheet 6", 0);
319
        wb.setSheetOrder("Sheet 6", 0);
320
        assertEquals(1, wb.getActiveSheetIndex());
292
        wb.setSheetOrder("Sheet 3", 7);
321
        wb.setSheetOrder("Sheet 3", 7);
293
        wb.setSheetOrder("Sheet 1", 9);
322
        wb.setSheetOrder("Sheet 1", 9);
323
        
324
        // now the first sheet is at index 1
325
        assertEquals(1, wb.getActiveSheetIndex());
294
326
295
        // Check they're currently right
327
        // Check they're currently right
296
        assertEquals(0, wb.getSheetIndex("Sheet 6"));
328
        assertEquals(0, wb.getSheetIndex("Sheet 6"));
Lines 317-322 public abstract class BaseTestWorkbook { Link Here
317
        assertEquals(8, wbr.getSheetIndex("Sheet 9"));
349
        assertEquals(8, wbr.getSheetIndex("Sheet 9"));
318
        assertEquals(9, wbr.getSheetIndex("Sheet 1"));
350
        assertEquals(9, wbr.getSheetIndex("Sheet 1"));
319
351
352
        assertEquals(1, wb.getActiveSheetIndex());
353
        
320
        // Now get the index by the sheet, not the name
354
        // Now get the index by the sheet, not the name
321
        for(int i=0; i<10; i++) {
355
        for(int i=0; i<10; i++) {
322
        	Sheet s = wbr.getSheetAt(i);
356
        	Sheet s = wbr.getSheetAt(i);

Return to bug 57171