diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index 27604cf7a..d435a052a 100644 --- a/src/java/org/apache/poi/hssf/model/Workbook.java +++ b/src/java/org/apache/poi/hssf/model/Workbook.java @@ -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 diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 6afe17f6f..57226d667 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -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); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 232916f8d..92ff4fce7 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -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" ); + + } } \ No newline at end of file