Add patch from bug #49690 - Add WorkbookUtil, which provies a way of generating valid sheet names

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@982260 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2010-08-04 14:13:42 +00:00
parent 79c4de256f
commit ba5f6a5cf3
6 changed files with 169 additions and 2 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.7-beta2" date="2010-??-??">
<action dev="POI-DEVELOPERS" type="add">49690 - Add WorkbookUtil, which provies a way of generating valid sheet names</action>
<action dev="POI-DEVELOPERS" type="fix">49694 - Use DataFormatter when autosizing columns, to better match the real display width of formatted cells</action>
<action dev="POI-DEVELOPERS" type="add">49441 - Allow overriding and guessing of HSMF non-unicode string encodings</action>
<action dev="POI-DEVELOPERS" type="fix">49689 - Allow the setting of user style names on newly created HSSF cell styles</action>

View File

@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.util.WorkbookUtil;
import java.io.IOException;
import java.io.FileOutputStream;
@ -33,7 +34,8 @@ public class NewSheet {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet(); // create with default name
wb.setSheetName(1, "second sheet"); // setting sheet name later
final String name = "second sheet";
wb.setSheetName(1, WorkbookUtil.createSafeSheetName(name)); // setting sheet name later
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();

View File

@ -84,6 +84,8 @@ public final class BoundSheetRecord extends StandardRecord {
/**
* Set the sheetname for this sheet. (this appears in the tabs at the bottom)
* @param sheetName the name of the sheet
* @see {@link org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)}
* for a safe way to create valid names
* @throws IllegalArgumentException if sheet name will cause excel to crash.
*/
public void setSheetname(String sheetName) {

View File

@ -725,7 +725,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
*
* @param sheetname the name for the new sheet. Note - certain length limits
* apply. See {@link #setSheetName(int, String)}.
*
* @see {@link org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)}
* for a safe way to create valid names
* @return HSSFSheet representing the new sheet.
* @throws IllegalArgumentException
* if there is already a sheet present with a case-insensitive

View File

@ -0,0 +1,77 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.util;
/**
* Helper methods for when working with Usermodel Workbooks
*/
public class WorkbookUtil {
/**
* Creates a valid sheet name, which is conform to the rules.
* In any case, the result safely can be used for
* {@link org.apache.poi.hssf.usermodel.HSSFWorkbook#setSheetName(int, String)}.
* <br>
* Rules:
* <ul>
* <li>never null</li>
* <li>minimum length is 1</li>
* <li>maximum length is 31</li>
* <li>doesn't contain special chars: / \ ? * ] [ </li>
* <li>Sheet names must not begin or end with ' (apostrophe)</li>
* </ul>
* Invalid characters are replaced by one space character ' '.
*
* @param nameProposal can be any string, will be truncated if necessary,
* allowed to be null
* @return a valid string, "empty" if to short, "null" if null
*/
public final static String createSafeSheetName(final String nameProposal) {
if (nameProposal == null) {
return "null";
}
if (nameProposal.length() < 1) {
return "empty";
}
final int length = Math.min(31, nameProposal.length());
final String shortenname = nameProposal.substring(0, length);
final StringBuilder result = new StringBuilder(shortenname);
for (int i=0; i<length; i++) {
char ch = result.charAt(i);
switch (ch) {
case '/':
case '\\':
case '?':
case '*':
case ']':
case '[':
result.setCharAt(i, ' ');
break;
case '\'':
if (i==0 || i==length-1) {
result.setCharAt(i, ' ');
}
break;
default:
// all other chars OK
}
}
return result.toString();
}
}

View File

@ -0,0 +1,84 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.util;
import junit.framework.TestCase;
/**
* Tests WorkbookUtil.
*
* @see org.apache.poi.ss.util.WorkbookUtil
*/
public final class TestWorkbookUtil extends TestCase {
/**
* borrowed test cases from
* {@link org.apache.poi.hssf.record.TestBoundSheetRecord#testValidNames()}
*/
public void testCreateSafeNames() {
String p = "Sheet1";
String actual = WorkbookUtil.createSafeSheetName(p);
assertEquals(p, actual);
p = "O'Brien's sales";
actual = WorkbookUtil.createSafeSheetName(p);
assertEquals(p, actual);
p = " data # ";
actual = WorkbookUtil.createSafeSheetName(p);
assertEquals(p, actual);
p = "data $1.00";
actual = WorkbookUtil.createSafeSheetName(p);
assertEquals(p, actual);
// now the replaced versions ...
actual = WorkbookUtil.createSafeSheetName("data?");
assertEquals("data ", actual);
actual = WorkbookUtil.createSafeSheetName("abc/def");
assertEquals("abc def", actual);
actual = WorkbookUtil.createSafeSheetName("data[0]");
assertEquals("data 0 ", actual);
actual = WorkbookUtil.createSafeSheetName("data*");
assertEquals("data ", actual);
actual = WorkbookUtil.createSafeSheetName("abc\\def");
assertEquals("abc def", actual);
actual = WorkbookUtil.createSafeSheetName("'data");
assertEquals(" data", actual);
actual = WorkbookUtil.createSafeSheetName("data'");
assertEquals("data ", actual);
actual = WorkbookUtil.createSafeSheetName("d'at'a");
assertEquals("d'at'a", actual);
actual = WorkbookUtil.createSafeSheetName(null);
assertEquals("null", actual);
actual = WorkbookUtil.createSafeSheetName("");
assertEquals("empty", actual);
actual = WorkbookUtil.createSafeSheetName("1234567890123456789012345678901TOOLONG");
assertEquals("1234567890123456789012345678901", actual);
}
}