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:
parent
7ce353785a
commit
8e822e3baf
@ -169,7 +169,7 @@ public class RecordFactory
|
|||||||
lastDGRecord.join((AbstractEscherHolderRecord) record);
|
lastDGRecord.join((AbstractEscherHolderRecord) record);
|
||||||
}
|
}
|
||||||
else if (record.getSid() == ContinueRecord.sid &&
|
else if (record.getSid() == ContinueRecord.sid &&
|
||||||
(lastRecord instanceof ObjRecord)) {
|
((lastRecord instanceof ObjRecord) || (lastRecord instanceof TextObjectRecord))) {
|
||||||
// Drawing records have a very strange continue behaviour.
|
// Drawing records have a very strange continue behaviour.
|
||||||
//There can actually be OBJ records mixed between the continues.
|
//There can actually be OBJ records mixed between the continues.
|
||||||
lastDrawingRecord.processContinueRecord( ((ContinueRecord)record).getData() );
|
lastDrawingRecord.processContinueRecord( ((ContinueRecord)record).getData() );
|
||||||
@ -181,7 +181,8 @@ public class RecordFactory
|
|||||||
//Gracefully handle records that we dont know about,
|
//Gracefully handle records that we dont know about,
|
||||||
//that happen to be continued
|
//that happen to be continued
|
||||||
records.add(record);
|
records.add(record);
|
||||||
} else throw new RecordFormatException("Unhandled Continue Record");
|
} else
|
||||||
|
throw new RecordFormatException("Unhandled Continue Record");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lastRecord = record;
|
lastRecord = record;
|
||||||
|
@ -24,7 +24,7 @@ import java.io.UnsupportedEncodingException;
|
|||||||
public class TextObjectRecord
|
public class TextObjectRecord
|
||||||
extends TextObjectBaseRecord
|
extends TextObjectBaseRecord
|
||||||
{
|
{
|
||||||
HSSFRichTextString str = new HSSFRichTextString( "" );
|
HSSFRichTextString str;
|
||||||
|
|
||||||
public TextObjectRecord()
|
public TextObjectRecord()
|
||||||
{
|
{
|
||||||
@ -33,6 +33,8 @@ public class TextObjectRecord
|
|||||||
public TextObjectRecord( RecordInputStream in )
|
public TextObjectRecord( RecordInputStream in )
|
||||||
{
|
{
|
||||||
super( in );
|
super( in );
|
||||||
|
if (str == null)
|
||||||
|
str = new HSSFRichTextString("");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void fillFields(RecordInputStream in)
|
protected void fillFields(RecordInputStream in)
|
||||||
|
@ -190,6 +190,8 @@ public class ValueRecordsAggregate
|
|||||||
|
|
||||||
/** Returns true if the row has cells attached to it */
|
/** Returns true if the row has cells attached to it */
|
||||||
public boolean rowHasCells(int row) {
|
public boolean rowHasCells(int row) {
|
||||||
|
if (row > records.length)
|
||||||
|
return false;
|
||||||
CellValueRecordInterface[] rowCells=records[row];
|
CellValueRecordInterface[] rowCells=records[row];
|
||||||
if(rowCells==null) return false;
|
if(rowCells==null) return false;
|
||||||
for(int col=0;col<rowCells.length;col++) {
|
for(int col=0;col<rowCells.length;col++) {
|
||||||
|
@ -473,10 +473,20 @@ public class HSSFWorkbook
|
|||||||
windowTwo.setPaged(sheets.size() == 1);
|
windowTwo.setPaged(sheets.size() == 1);
|
||||||
|
|
||||||
sheets.add(clonedSheet);
|
sheets.add(clonedSheet);
|
||||||
if (srcName.length()<28) {
|
int i=1;
|
||||||
workbook.setSheetName(sheets.size()-1, srcName+"(2)");
|
while (true) {
|
||||||
}else {
|
//Try and find the next sheet name that is unique
|
||||||
workbook.setSheetName(sheets.size()-1,srcName.substring(0,28)+"(2)");
|
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;
|
return clonedSheet;
|
||||||
}
|
}
|
||||||
|
@ -205,6 +205,24 @@ public class TestHSSFSheet
|
|||||||
assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test");
|
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
|
* Test that the ProtectRecord is included when creating or cloning a sheet
|
||||||
*/
|
*/
|
||||||
|
@ -46,15 +46,15 @@ public class TestHSSFWorkbook extends TestCase
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
b.setSheetName( 3, "name1"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
|
b.setSheetName( 3, "name1" );
|
||||||
fail();
|
fail();
|
||||||
}
|
}
|
||||||
catch ( IllegalArgumentException pass )
|
catch ( IllegalArgumentException pass )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
b.setSheetName( 3, "name2"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
|
b.setSheetName( 3, "name2" );
|
||||||
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( );
|
HSSFWorkbook c = new HSSFWorkbook( );
|
||||||
|
Loading…
Reference in New Issue
Block a user