diff options
author | Nick Burch <nick@apache.org> | 2010-08-04 14:13:42 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2010-08-04 14:13:42 +0000 |
commit | ba5f6a5cf3c995d0896b1d2c8a4937f6cd9128aa (patch) | |
tree | f8a42f7b3b41d9951eec94adbcaf384a4e4950b8 | |
parent | 79c4de256f76c7b372333552975c8701c7f6a798 (diff) | |
download | poi-ba5f6a5cf3c995d0896b1d2c8a4937f6cd9128aa.tar.gz poi-ba5f6a5cf3c995d0896b1d2c8a4937f6cd9128aa.zip |
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
6 files changed, 169 insertions, 2 deletions
diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index b554d8cea5..effa0239f0 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -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> diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java index 5fdf2dd30d..40754feec4 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java @@ -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(); diff --git a/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java b/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java index b8b6d72ca3..7f73a1dd18 100644 --- a/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java +++ b/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java @@ -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) { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index de1cb50550..4d5e752ad3 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -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 diff --git a/src/java/org/apache/poi/ss/util/WorkbookUtil.java b/src/java/org/apache/poi/ss/util/WorkbookUtil.java new file mode 100644 index 0000000000..11e01a04b1 --- /dev/null +++ b/src/java/org/apache/poi/ss/util/WorkbookUtil.java @@ -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(); + } +} diff --git a/src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java b/src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java new file mode 100644 index 0000000000..c1e49cab67 --- /dev/null +++ b/src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java @@ -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); + } +} |