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

(-)cfl/workspaces/poi-trunk/ApachePOI/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (-2 / +4 lines)
Lines 1076-1081 Link Here
1076
     * @param index the 0-based index of the sheet to delete
1076
     * @param index the 0-based index of the sheet to delete
1077
     */
1077
     */
1078
    private void onSheetDelete(int index) {
1078
    private void onSheetDelete(int index) {
1079
        String sheetName = getSheetName(index);
1079
        //delete the CTSheet reference from workbook.xml
1080
        //delete the CTSheet reference from workbook.xml
1080
        workbook.getSheets().removeSheet(index);
1081
        workbook.getSheets().removeSheet(index);
1081
1082
Lines 1089-1096 Link Here
1089
        for (Iterator<XSSFName> it = namedRanges.iterator(); it.hasNext();) {
1090
        for (Iterator<XSSFName> it = namedRanges.iterator(); it.hasNext();) {
1090
            XSSFName nm = it.next();
1091
            XSSFName nm = it.next();
1091
            CTDefinedName ct = nm.getCTName();
1092
            CTDefinedName ct = nm.getCTName();
1092
            if(!ct.isSetLocalSheetId()) continue;
1093
            boolean nameIsScopedToRemovedSheet = ct.isSetLocalSheetId() && ct.getLocalSheetId() == index;
1093
            if (ct.getLocalSheetId() == index) {
1094
            boolean nameRefersToRemovedSheet = nm.getSheetName().equals(sheetName);
1095
            if (nameIsScopedToRemovedSheet || nameRefersToRemovedSheet) {
1094
                it.remove();
1096
                it.remove();
1095
            } else if (ct.getLocalSheetId() > index){
1097
            } else if (ct.getLocalSheetId() > index){
1096
                // Bump down by one, so still points at the same sheet
1098
                // Bump down by one, so still points at the same sheet
(-)cfl/workspaces/poi-trunk/ApachePOI/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorksheetRemoval.java (+25 lines)
Line 0 Link Here
1
package org.apache.poi.xssf.usermodel;
2
3
import junit.framework.TestCase;
4
5
import org.junit.Test;
6
7
public final class TestXSSFWorksheetRemoval extends TestCase {
8
    
9
    @Test
10
    public void testSheetRemovalRemovesNamedRanges() throws Exception {
11
        XSSFWorkbook wb = new XSSFWorkbook();
12
        wb.createSheet("Sheet1");
13
        
14
        XSSFName name = wb.createName();
15
        name.setNameName("test");
16
        name.setRefersToFormula("Sheet1!A1");
17
        
18
        assertEquals(1, wb.getNumberOfNames());
19
        
20
        wb.removeSheetAt(0);
21
        assertEquals("Names referencing deleted sheet should be removed", 0, wb.getNumberOfNames());
22
        wb.close();
23
    }
24
25
}

Return to bug 56887