diff options
6 files changed, 43 insertions, 10 deletions
diff --git a/src/java/org/apache/poi/hssf/record/RecordFactory.java b/src/java/org/apache/poi/hssf/record/RecordFactory.java index 544a896584..f9c86a3cec 100644 --- a/src/java/org/apache/poi/hssf/record/RecordFactory.java +++ b/src/java/org/apache/poi/hssf/record/RecordFactory.java @@ -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; diff --git a/src/java/org/apache/poi/hssf/record/TextObjectRecord.java b/src/java/org/apache/poi/hssf/record/TextObjectRecord.java index b11f6d5748..df4c47ece4 100644 --- a/src/java/org/apache/poi/hssf/record/TextObjectRecord.java +++ b/src/java/org/apache/poi/hssf/record/TextObjectRecord.java @@ -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) diff --git a/src/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java index a3c62679c4..49a1cd4522 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java @@ -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++) { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 91b4683f50..a953315f96 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -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; } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index a603c27532..e21bacdfc3 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -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 diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 7bf6c47937..7b0b7c3f30 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -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( ); |