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

(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (-6 / +29 lines)
Lines 540-552 Link Here
540
     */
540
     */
541
    @Override
541
    @Override
542
    public XSSFSheet cloneSheet(int sheetNum) {
542
    public XSSFSheet cloneSheet(int sheetNum) {
543
        return cloneSheet(sheetNum, null);
544
    }
545
    /**
546
     * Create an XSSFSheet from an existing sheet in the XSSFWorkbook.
547
     *  The cloned sheet is a deep copy of the original but with a new given
548
     *  name.
549
     *
550
     * @return XSSFSheet representing the cloned sheet.
551
     * @throws IllegalArgumentException if the sheet index or the sheet
552
     *         name is invalid
553
     * @throws POIXMLException if there were errors when cloning
554
     */
555
    public XSSFSheet cloneSheet(int sheetNum, String newName) {
543
        validateSheetIndex(sheetNum);
556
        validateSheetIndex(sheetNum);
544
545
        XSSFSheet srcSheet = sheets.get(sheetNum);
557
        XSSFSheet srcSheet = sheets.get(sheetNum);
546
        String srcName = srcSheet.getSheetName();
547
        String clonedName = getUniqueSheetName(srcName);
548
558
549
        XSSFSheet clonedSheet = createSheet(clonedName);
559
        if (newName == null) {
560
            String srcName = srcSheet.getSheetName();
561
            newName = getUniqueSheetName(srcName);
562
        } else {
563
            validateSheetName(newName);
564
        }
565
566
        XSSFSheet clonedSheet = createSheet(newName);
550
        try {
567
        try {
551
            ByteArrayOutputStream out = new ByteArrayOutputStream();
568
            ByteArrayOutputStream out = new ByteArrayOutputStream();
552
            srcSheet.write(out);
569
            srcSheet.write(out);
Lines 763-770 Link Here
763
            throw new IllegalArgumentException("sheetName must not be null");
780
            throw new IllegalArgumentException("sheetName must not be null");
764
        }
781
        }
765
782
766
        if (containsSheet( sheetname, sheets.size() ))
783
        validateSheetName(sheetname);
767
               throw new IllegalArgumentException( "The workbook already contains a sheet of this name");
768
784
769
        // YK: Mimic Excel and silently truncate sheet names longer than 31 characters
785
        // YK: Mimic Excel and silently truncate sheet names longer than 31 characters
770
        if(sheetname.length() > 31) sheetname = sheetname.substring(0, 31);
786
        if(sheetname.length() > 31) sheetname = sheetname.substring(0, 31);
Lines 804-809 Link Here
804
        return wrapper;
820
        return wrapper;
805
    }
821
    }
806
822
823
    private void validateSheetName(
824
            final String sheetname) throws IllegalArgumentException {
825
        if (containsSheet( sheetname, sheets.size() )) {
826
            throw new IllegalArgumentException("The workbook already contains a sheet of this name");
827
        }
828
    }
829
807
    protected XSSFDialogsheet createDialogsheet(String sheetname, CTDialogsheet dialogsheet) {
830
    protected XSSFDialogsheet createDialogsheet(String sheetname, CTDialogsheet dialogsheet) {
808
        XSSFSheet sheet = createSheet(sheetname);
831
        XSSFSheet sheet = createSheet(sheetname);
809
        return new XSSFDialogsheet(sheet);
832
        return new XSSFDialogsheet(sheet);
(-)src/ooxml/testcases/org/apache/poi/xssf/AllXSSFTests.java (-1 / +2 lines)
Lines 47-53 Link Here
47
    TestCellReference.class,
47
    TestCellReference.class,
48
    TestCTColComparator.class,
48
    TestCTColComparator.class,
49
    TestNumericRanges.class,       
49
    TestNumericRanges.class,       
50
    TestCellFormatPart.class
50
    TestCellFormatPart.class,
51
    TestXSSFCloneSheet.class
51
})
52
})
52
public final class AllXSSFTests {
53
public final class AllXSSFTests {
53
}
54
}
(-)src/ooxml/testcases/org/apache/poi/xssf/TestXSSFCloneSheet.java (+65 lines)
Line 0 Link Here
1
package org.apache.poi.xssf;
2
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.fail;
5
6
import org.apache.poi.xssf.usermodel.XSSFSheet;
7
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
8
import org.junit.Before;
9
import org.junit.Test;
10
11
public class TestXSSFCloneSheet {
12
13
    private static final String OTHER_SHEET_NAME = "Another";
14
    private static final String VALID_SHEET_NAME = "Sheet01";
15
    private XSSFWorkbook wb;
16
17
    @Before
18
    public void setUp() {
19
        wb = new XSSFWorkbook();
20
        wb.createSheet(VALID_SHEET_NAME);  
21
    }
22
    
23
    
24
    @Test
25
    public void testCloneSheetIntValid() {
26
        wb.cloneSheet(0);
27
        assertEquals(2, wb.getNumberOfSheets());
28
        try {
29
            wb.cloneSheet(2);
30
            fail("ShouldFail");
31
        } catch (IllegalArgumentException e) {
32
            
33
        }
34
    }
35
36
    @Test
37
    public void testCloneSheetIntInvalid() {
38
        try {
39
            wb.cloneSheet(1);
40
            fail("Should Fail");
41
        } catch (IllegalArgumentException e) {
42
            
43
        }
44
        assertEquals(1, wb.getNumberOfSheets());
45
    }
46
47
    @Test
48
    public void testCloneSheetIntStringValidName() {
49
        XSSFSheet cloned = wb.cloneSheet(0, OTHER_SHEET_NAME);
50
        assertEquals(OTHER_SHEET_NAME, cloned.getSheetName());
51
        assertEquals(2, wb.getNumberOfSheets());
52
    }
53
    
54
    @Test
55
    public void testCloneSheetIntStringInvalidName() {
56
        try {
57
            wb.cloneSheet(0, VALID_SHEET_NAME);
58
            fail("Should fail");
59
        } catch (IllegalArgumentException e) {
60
            
61
        }
62
        assertEquals(1, wb.getNumberOfSheets());
63
    }
64
65
}

Return to bug 58909