ASF Bugzilla – Attachment 33476 Details for
Bug 58909
[PATCH] Performance problem on cloneSheet/setSheetName
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
proposed patch
adding_method_XSSFWorkbook_clone_sheet_int__String_.patch (text/plain), 5.19 KB, created by
Luca Martini
on 2016-01-22 10:38:42 UTC
(
hide
)
Description:
proposed patch
Filename:
MIME Type:
Creator:
Luca Martini
Created:
2016-01-22 10:38:42 UTC
Size:
5.19 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P ApachePOI >Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java >=================================================================== >--- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (revision 1726153) >+++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (working copy) >@@ -540,13 +540,30 @@ > */ > @Override > public XSSFSheet cloneSheet(int sheetNum) { >+ return cloneSheet(sheetNum, null); >+ } >+ /** >+ * Create an XSSFSheet from an existing sheet in the XSSFWorkbook. >+ * The cloned sheet is a deep copy of the original but with a new given >+ * name. >+ * >+ * @return XSSFSheet representing the cloned sheet. >+ * @throws IllegalArgumentException if the sheet index or the sheet >+ * name is invalid >+ * @throws POIXMLException if there were errors when cloning >+ */ >+ public XSSFSheet cloneSheet(int sheetNum, String newName) { > validateSheetIndex(sheetNum); >- > XSSFSheet srcSheet = sheets.get(sheetNum); >- String srcName = srcSheet.getSheetName(); >- String clonedName = getUniqueSheetName(srcName); > >- XSSFSheet clonedSheet = createSheet(clonedName); >+ if (newName == null) { >+ String srcName = srcSheet.getSheetName(); >+ newName = getUniqueSheetName(srcName); >+ } else { >+ validateSheetName(newName); >+ } >+ >+ XSSFSheet clonedSheet = createSheet(newName); > try { > ByteArrayOutputStream out = new ByteArrayOutputStream(); > srcSheet.write(out); >@@ -763,8 +780,7 @@ > throw new IllegalArgumentException("sheetName must not be null"); > } > >- if (containsSheet( sheetname, sheets.size() )) >- throw new IllegalArgumentException( "The workbook already contains a sheet of this name"); >+ validateSheetName(sheetname); > > // YK: Mimic Excel and silently truncate sheet names longer than 31 characters > if(sheetname.length() > 31) sheetname = sheetname.substring(0, 31); >@@ -804,6 +820,13 @@ > return wrapper; > } > >+ private void validateSheetName( >+ final String sheetname) throws IllegalArgumentException { >+ if (containsSheet( sheetname, sheets.size() )) { >+ throw new IllegalArgumentException("The workbook already contains a sheet of this name"); >+ } >+ } >+ > protected XSSFDialogsheet createDialogsheet(String sheetname, CTDialogsheet dialogsheet) { > XSSFSheet sheet = createSheet(sheetname); > return new XSSFDialogsheet(sheet); >Index: src/ooxml/testcases/org/apache/poi/xssf/AllXSSFTests.java >=================================================================== >--- src/ooxml/testcases/org/apache/poi/xssf/AllXSSFTests.java (revision 1726153) >+++ src/ooxml/testcases/org/apache/poi/xssf/AllXSSFTests.java (working copy) >@@ -47,7 +47,8 @@ > TestCellReference.class, > TestCTColComparator.class, > TestNumericRanges.class, >- TestCellFormatPart.class >+ TestCellFormatPart.class, >+ TestXSSFCloneSheet.class > }) > public final class AllXSSFTests { > } >Index: src/ooxml/testcases/org/apache/poi/xssf/TestXSSFCloneSheet.java >=================================================================== >--- src/ooxml/testcases/org/apache/poi/xssf/TestXSSFCloneSheet.java (revision 0) >+++ src/ooxml/testcases/org/apache/poi/xssf/TestXSSFCloneSheet.java (working copy) >@@ -0,0 +1,65 @@ >+package org.apache.poi.xssf; >+ >+import static org.junit.Assert.assertEquals; >+import static org.junit.Assert.fail; >+ >+import org.apache.poi.xssf.usermodel.XSSFSheet; >+import org.apache.poi.xssf.usermodel.XSSFWorkbook; >+import org.junit.Before; >+import org.junit.Test; >+ >+public class TestXSSFCloneSheet { >+ >+ private static final String OTHER_SHEET_NAME = "Another"; >+ private static final String VALID_SHEET_NAME = "Sheet01"; >+ private XSSFWorkbook wb; >+ >+ @Before >+ public void setUp() { >+ wb = new XSSFWorkbook(); >+ wb.createSheet(VALID_SHEET_NAME); >+ } >+ >+ >+ @Test >+ public void testCloneSheetIntValid() { >+ wb.cloneSheet(0); >+ assertEquals(2, wb.getNumberOfSheets()); >+ try { >+ wb.cloneSheet(2); >+ fail("ShouldFail"); >+ } catch (IllegalArgumentException e) { >+ >+ } >+ } >+ >+ @Test >+ public void testCloneSheetIntInvalid() { >+ try { >+ wb.cloneSheet(1); >+ fail("Should Fail"); >+ } catch (IllegalArgumentException e) { >+ >+ } >+ assertEquals(1, wb.getNumberOfSheets()); >+ } >+ >+ @Test >+ public void testCloneSheetIntStringValidName() { >+ XSSFSheet cloned = wb.cloneSheet(0, OTHER_SHEET_NAME); >+ assertEquals(OTHER_SHEET_NAME, cloned.getSheetName()); >+ assertEquals(2, wb.getNumberOfSheets()); >+ } >+ >+ @Test >+ public void testCloneSheetIntStringInvalidName() { >+ try { >+ wb.cloneSheet(0, VALID_SHEET_NAME); >+ fail("Should fail"); >+ } catch (IllegalArgumentException e) { >+ >+ } >+ assertEquals(1, wb.getNumberOfSheets()); >+ } >+ >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 58909
:
33476
|
33477