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 );
sheet.setCompressedUnicodeFlag( (byte)encoding );
}
- /**
+ /**
* sets the order of appearance for a given sheet.
*
* @param sheetname the name of the sheet to reorder
* @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");
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);
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" );
+
+ }
}
\ No newline at end of file