Bugzilla Bug 30951

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353598 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Glen Stampoultzis 2004-09-19 02:06:54 +00:00
parent 4e92118a61
commit c6f3b914e5
3 changed files with 67 additions and 4 deletions

View 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

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

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