<changes>
<release version="3.7-beta3" date="2010-??-??">
+ <action dev="poi-developers" type="fix">49875 - fixed XSSFWorkbook.createSheet to throw exception if sheet name begins or ends with a single quote (')</action>
<action dev="poi-developers" type="fix">49873 - fixed XSSFFormulaEvaluator to support blank cells</action>
<action dev="poi-developers" type="fix">49850 - added a getter for _iStartAt in ListFormatOverrideLevel</action>
<action dev="poi-developers" type="fix">49761 - change cell type to error when setting Double.NaN or Infinities</action>
import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndianOutput;
import org.apache.poi.util.StringUtil;
+import org.apache.poi.ss.util.WorkbookUtil;
/**
* Title: Bound Sheet Record (aka BundleSheet) (0x0085)<P>
*/
public void setSheetname(String sheetName) {
- validateSheetName(sheetName);
+ WorkbookUtil.validateSheetName(sheetName);
field_5_sheetname = sheetName;
field_4_isMultibyteUnicode = StringUtil.hasMultibyte(sheetName) ? 1 : 0;
}
- private static void validateSheetName(String sheetName) {
- if (sheetName == null) {
- throw new IllegalArgumentException("sheetName must not be null");
- }
- int len = sheetName.length();
- if (len < 1) {
- throw new IllegalArgumentException("sheetName must not be empty string");
- }
- for (int i=0; i<len; i++) {
- char ch = sheetName.charAt(i);
- switch (ch) {
- case '/':
- case '\\':
- case '?':
- case '*':
- case ']':
- case '[':
- break;
- default:
- // all other chars OK
- continue;
- }
- throw new IllegalArgumentException("Invalid char (" + ch
- + ") found at index (" + i + ") in sheet name '" + sheetName + "'");
- }
- if (sheetName.charAt(0) == '\'' || sheetName.charAt(len-1) == '\'') {
- throw new IllegalArgumentException("Invalid sheet name '" + sheetName
- + "'. Sheet names must not begin or end with (').");
- }
- }
-
/**
* get the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file
*
}
return result.toString();
}
+
+ /**
+ * Validates sheet name.
+ *
+ * <p>
+ * The character count <tt>MUST</tt> be greater than or equal to 1 and less than or equal to 31.
+ * The string MUST NOT contain the any of the following characters:
+ * <ul>
+ * <li> 0x0000 </li>
+ * <li> 0x0003 </li>
+ * <li> colon (:) </li>
+ * <li> backslash (\) </li>
+ * <li> asterisk (*) </li>
+ * <li> question mark (?) </li>
+ * <li> forward slash (/) </li>
+ * <li> opening square bracket ([) </li>
+ * <li> closing square bracket (]) </li>
+ * </ul>
+ * The string MUST NOT begin or end with the single quote (') character.
+ * </p>
+ *
+ * @param sheetName the name to validate
+ */
+ public static void validateSheetName(String sheetName) {
+ if (sheetName == null) {
+ throw new IllegalArgumentException("sheetName must not be null");
+ }
+ int len = sheetName.length();
+ if (len < 1) {
+ throw new IllegalArgumentException("sheetName must not be empty string");
+ }
+ for (int i=0; i<len; i++) {
+ char ch = sheetName.charAt(i);
+ switch (ch) {
+ case '/':
+ case '\\':
+ case '?':
+ case '*':
+ case ']':
+ case '[':
+ case ':':
+ break;
+ default:
+ // all other chars OK
+ continue;
+ }
+ throw new IllegalArgumentException("Invalid char (" + ch
+ + ") found at index (" + i + ") in sheet name '" + sheetName + "'");
+ }
+ if (sheetName.charAt(0) == '\'' || sheetName.charAt(len-1) == '\'') {
+ throw new IllegalArgumentException("Invalid sheet name '" + sheetName
+ + "'. Sheet names must not begin or end with (').");
+ }
+ }
+
}
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.util.*;
import org.apache.poi.xssf.model.CalculationChain;
import org.apache.poi.xssf.model.MapInfo;
}
private CTSheet addSheet(String sheetname) {
- validateSheetName(sheetname);
+ WorkbookUtil.validateSheetName(sheetname);
CTSheet sheet = workbook.getSheets().addNewSheet();
sheet.setName(sheetname);
* or contains /\?*[]
*
* @param sheet number (0 based)
- * @see #validateSheetName(String)
*/
public void setSheetName(int sheet, String name) {
validateSheetIndex(sheet);
- validateSheetName(name);
+ WorkbookUtil.validateSheetName(name);
if (containsSheet(name, sheet ))
throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
workbook.getSheets().getSheetArray(sheet).setName(name);
return false;
}
- /**
- * Validates sheet name.
- *
- * <p>
- * The character count <tt>MUST</tt> be greater than or equal to 1 and less than or equal to 31.
- * The string MUST NOT contain the any of the following characters:
- * <ul>
- * <li> 0x0000 </li>
- * <li> 0x0003 </li>
- * <li> colon (:) </li>
- * <li> backslash (\) </li>
- * <li> asterisk (*) </li>
- * <li> question mark (?) </li>
- * <li> forward slash (/) </li>
- * <li> opening square bracket ([) </li>
- * <li> closing square bracket (]) </li>
- * </ul>
- * The string MUST NOT begin or end with the single quote (') character.
- * </p>
- *
- * @param sheetName the name to validate
- */
- private static void validateSheetName(String sheetName) {
- if (sheetName == null) {
- throw new IllegalArgumentException("sheetName must not be null");
- }
- int len = sheetName.length();
- if (len < 1 || len > 31) {
- throw new IllegalArgumentException("sheetName '" + sheetName
- + "' is invalid - must be 1-30 characters long");
- }
- for (int i=0; i<len; i++) {
- char ch = sheetName.charAt(i);
- switch (ch) {
- case '/':
- case '\\':
- case '?':
- case '*':
- case ']':
- case '[':
- break;
- default:
- // all other chars OK
- continue;
- }
- throw new IllegalArgumentException("Invalid char (" + ch
- + ") found at index (" + i + ") in sheet name '" + sheetName + "'");
- }
- }
-
/**
* Gets a boolean value that indicates whether the date systems used in the workbook starts in 1904.
* <p>
//names cannot be blank or contain any of /\*?[]
String[] invalidNames = {"", "Sheet/", "Sheet\\",
- "Sheet?", "Sheet*", "Sheet[", "Sheet]"};
+ "Sheet?", "Sheet*", "Sheet[", "Sheet]", "'Sheet'",
+ "My:Sheet"};
for (String sheetName : invalidNames) {
try {
wb.createSheet(sheetName);