]> source.dussan.org Git - poi.git/commitdiff
Multiple clone of same sheet now generates unique sheet name BUG 37416 fixed.
authorJason Height <jheight@apache.org>
Thu, 5 Jan 2006 07:29:36 +0000 (07:29 +0000)
committerJason Height <jheight@apache.org>
Thu, 5 Jan 2006 07:29:36 +0000 (07:29 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@366110 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/RecordFactory.java
src/java/org/apache/poi/hssf/record/TextObjectRecord.java
src/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java

index 544a896584bed02cd2c5fac0cf5ae71470b5c538..f9c86a3cec25a9d4467bcf49a1e8a3f1a50f8669 100644 (file)
@@ -169,7 +169,7 @@ public class RecordFactory
                                 lastDGRecord.join((AbstractEscherHolderRecord) record);
                             }
                         else if (record.getSid() == ContinueRecord.sid &&
-                                 (lastRecord instanceof ObjRecord)) {
+                                 ((lastRecord instanceof ObjRecord) || (lastRecord instanceof TextObjectRecord))) {
                           // Drawing records have a very strange continue behaviour.
                           //There can actually be OBJ records mixed between the continues.
                           lastDrawingRecord.processContinueRecord( ((ContinueRecord)record).getData() );
@@ -181,7 +181,8 @@ public class RecordFactory
                             //Gracefully handle records that we dont know about,
                             //that happen to be continued
                             records.add(record);
-                          } else throw new RecordFormatException("Unhandled Continue Record");
+                          } else 
+                                 throw new RecordFormatException("Unhandled Continue Record");
                             }
                         else {
                             lastRecord = record;
index b11f6d5748dbbb92202b52e44136fd3d1980f1fd..df4c47ece4f114fe5265d7520672ad37d35ae582 100644 (file)
@@ -24,7 +24,7 @@ import java.io.UnsupportedEncodingException;
 public class TextObjectRecord
         extends TextObjectBaseRecord
 {
-    HSSFRichTextString str = new HSSFRichTextString( "" );
+    HSSFRichTextString str;
 
     public TextObjectRecord()
     {
@@ -33,6 +33,8 @@ public class TextObjectRecord
     public TextObjectRecord( RecordInputStream in )
     {
         super( in );
+        if (str == null)
+               str = new HSSFRichTextString("");
     }
 
     protected void fillFields(RecordInputStream in)
index a3c62679c43b223e458036919cd3dc5e7fdde424..49a1cd4522a78bab872ee2b097dd2d4b71811984 100644 (file)
@@ -190,6 +190,8 @@ public class ValueRecordsAggregate
 
     /** Returns true if the row has cells attached to it */
     public boolean rowHasCells(int row) {
+       if (row > records.length)
+               return false;
       CellValueRecordInterface[] rowCells=records[row];
       if(rowCells==null) return false;
       for(int col=0;col<rowCells.length;col++) {
index 91b4683f50e6b36f5878f0fc4fe78347debf1189..a953315f96ef26adfd239686acc0c9e20aaa1b70 100644 (file)
@@ -473,10 +473,20 @@ public class HSSFWorkbook
         windowTwo.setPaged(sheets.size() == 1);
 
         sheets.add(clonedSheet);
-        if (srcName.length()<28) {
-            workbook.setSheetName(sheets.size()-1, srcName+"(2)");
-        }else {
-            workbook.setSheetName(sheets.size()-1,srcName.substring(0,28)+"(2)");
+        int i=1;
+        while (true) {
+               //Try and find the next sheet name that is unique
+               String name = srcName;
+               String index = Integer.toString(i++);
+               if (name.length()+index.length()+2<31)
+                 name = name + "("+index+")";
+               else name = name.substring(0, 31-index.length()-2)+"("+index+")";
+               
+               //If the sheet name is unique, then set it otherwise move on to the next number.
+               if (workbook.getSheetIndex(name) == -1) {
+              workbook.setSheetName(sheets.size()-1, name);
+              break;
+               }
         }
         return clonedSheet;
       }
index a603c27532380420525a8f1829b6ec6bc9f4ec8d..e21bacdfc31a2206a43e62d48a890e15aaa405a8 100644 (file)
@@ -204,6 +204,24 @@ public class TestHSSFSheet
         cell.setCellValue("Difference Check");
         assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test");
     }
+
+    /** tests that the sheet name for multiple clones of the same sheet is unique
+     * BUG 37416
+     */    
+    public void testCloneSheetMultipleTimes() {
+        HSSFWorkbook workbook = new HSSFWorkbook();
+        HSSFSheet sheet = workbook.createSheet("Test Clone");
+        HSSFRow row = sheet.createRow((short) 0);
+        HSSFCell cell = row.createCell((short) 0);
+        cell.setCellValue("clone_test");
+        //Clone the sheet multiple times
+        workbook.cloneSheet(0);
+        workbook.cloneSheet(0);
+        
+        assertNotNull(workbook.getSheet("Test Clone"));
+        assertNotNull(workbook.getSheet("Test Clone(1)"));
+        assertNotNull(workbook.getSheet("Test Clone(2)"));  
+    }
     
        /**
         * Test that the ProtectRecord is included when creating or cloning a sheet
index 7bf6c47937788bc3cbc436f29f0819d9844792b9..7b0b7c3f30ee3b9c825d9a1edf8871f2f5bb6b37 100644 (file)
@@ -46,15 +46,15 @@ public class TestHSSFWorkbook extends TestCase
 
         try
         {
-            b.setSheetName( 3,  "name1"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
+            b.setSheetName( 3,  "name1" );
             fail();
         }
         catch ( IllegalArgumentException pass )
         {
         }
 
-        b.setSheetName( 3,  "name2"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
-        b.setSheetName( 3,  "name2"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
+        b.setSheetName( 3,  "name2" );
+        b.setSheetName( 3,  "name2" );
         b.setSheetName( 3,  "name2" );
         
         HSSFWorkbook c = new HSSFWorkbook( );