Multiple clone of same sheet now generates unique sheet name BUG 37416 fixed.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@366110 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason Height 2006-01-05 07:29:36 +00:00
parent 7ce353785a
commit 8e822e3baf
6 changed files with 43 additions and 10 deletions

View 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;

View 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)

View 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++) {

View 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;
}

View 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

View 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( );