]> source.dussan.org Git - poi.git/commitdiff
Bugzilla Bug 30951
authorGlen Stampoultzis <glens@apache.org>
Sun, 19 Sep 2004 02:06:54 +0000 (02:06 +0000)
committerGlen Stampoultzis <glens@apache.org>
Sun, 19 Sep 2004 02:06:54 +0000 (02:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353598 13f79535-47bb-0310-9956-ffa450edef68

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

index 27604cf7a1e10977375bb1c35134afc9759882b7..d435a052a8f3d71825e038e818f6c57ef53d949a 100644 (file)
@@ -451,6 +451,24 @@ public class Workbook implements Model
         setSheetName( sheetnum, sheetname, (byte)0 );
     }
 
+    /**
+     * Determines whether a workbook contains the privided sheet name.
+     *
+     * @param name the name to test
+     * @param excludeSheetIdx the sheet to exclude from the check or -1 to include all sheets in the check.
+     * @return true if the sheet contains the name, false otherwise.
+     */
+    public boolean doesContainsSheetName( String name, int excludeSheetIdx )
+    {
+        for ( int i = 0; i < boundsheets.size(); i++ )
+        {
+            BoundSheetRecord boundSheetRecord = (BoundSheetRecord) boundsheets.get( i );
+            if (excludeSheetIdx != i && name.equals(boundSheetRecord.getSheetname()))
+                return true;
+        }
+        return false;
+    }
+
     public void setSheetName(int sheetnum, String sheetname, short encoding ) {
         checkSheets(sheetnum);
         BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum );
@@ -459,7 +477,7 @@ public class Workbook implements Model
                sheet.setCompressedUnicodeFlag( (byte)encoding );
     }
     
-       /**
+    /**
         * sets the order of appearance for a given sheet.
         *
         * @param sheetname the name of the sheet to reorder
index 6afe17f6f8d1213d4517d0598b45699d919a6142..57226d667ba436c31bc42d8ea05bf180b4a831aa 100644 (file)
@@ -244,14 +244,19 @@ public class HSSFWorkbook
      * @param sheet number (0 based)
      * @param sheet name
      */
-
     public void setSheetName(int sheet, String name)
     {
+        if (workbook.doesContainsSheetName( name, sheet ))
+            throw new IllegalArgumentException( "The workbook already contains a sheet with this name" );
+
         workbook.setSheetName( sheet, name, ENCODING_COMPRESSED_UNICODE );
     }
 
     public void setSheetName( int sheet, String name, short encoding )
     {
+        if (workbook.doesContainsSheetName( name, sheet ))
+            throw new IllegalArgumentException( "The workbook already contains a sheet with this name" );
+
         if (sheet > (sheets.size() - 1))
         {
             throw new RuntimeException("Sheet out of bounds");
@@ -361,9 +366,9 @@ public class HSSFWorkbook
 
     public HSSFSheet createSheet(String sheetname)
     {
+        if (workbook.doesContainsSheetName( sheetname, -1 ))
+            throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
 
-//        if (getNumberOfSheets() == 3)
-//            throw new RuntimeException("You cannot have more than three sheets in HSSF 1.0");
         HSSFSheet sheet = new HSSFSheet(workbook);
 
         sheets.add(sheet);
index 232916f8d50989a75905ba99523e2b4470516fb6..92ff4fce7721623a7f977d462926ce6de2bc0dad 100644 (file)
@@ -18,4 +18,44 @@ public class TestHSSFWorkbook extends TestCase
         NameRecord nameRecord = b.getWorkbook().getNameRecord( 0 );
         assertEquals( 3, nameRecord.getIndexToSheet() );
     }
+
+    public void testDuplicateNames()
+            throws Exception
+    {
+        HSSFWorkbook b = new HSSFWorkbook( );
+        b.createSheet();
+        b.createSheet();
+        b.createSheet("name1");
+        try
+        {
+            b.createSheet("name1");
+            fail();
+        }
+        catch ( IllegalArgumentException pass )
+        {
+        }
+        b.createSheet();
+        try
+        {
+            b.setSheetName( 3,  "name1" );
+            fail();
+        }
+        catch ( IllegalArgumentException pass )
+        {
+        }
+
+        try
+        {
+            b.setSheetName( 3,  "name1", HSSFWorkbook.ENCODING_UTF_16 );
+            fail();
+        }
+        catch ( IllegalArgumentException pass )
+        {
+        }
+
+        b.setSheetName( 3,  "name2", HSSFWorkbook.ENCODING_UTF_16 );
+        b.setSheetName( 3,  "name2", HSSFWorkbook.ENCODING_UTF_16 );
+        b.setSheetName( 3,  "name2" );
+
+    }
 }
\ No newline at end of file