* @param descriptor the part descriptor
* @param factory the factory that will create an instance of the requested relation
* @return the created child POIXMLDocumentPart
+ * @throws PartAlreadyExistsException
+ * If rule M1.12 is not verified : Packages shall not contain
+ * equivalent part names and package implementers shall neither
+ * create nor recognize packages with equivalent part names.
*/
public final POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory){
return createRelationship(descriptor, factory, -1, false);
}
+ /**
+ * Create a new child POIXMLDocumentPart
+ *
+ * @param descriptor the part descriptor
+ * @param factory the factory that will create an instance of the requested relation
+ * @param idx part number
+ * @return the created child POIXMLDocumentPart
+ * @throws PartAlreadyExistsException
+ * If rule M1.12 is not verified : Packages shall not contain
+ * equivalent part names and package implementers shall neither
+ * create nor recognize packages with equivalent part names.
+ */
public final POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx){
return createRelationship(descriptor, factory, idx, false);
}
* @param idx part number
* @param noRelation if true, then no relationship is added.
* @return the created child POIXMLDocumentPart
+ * @throws PartAlreadyExistsException
+ * If rule M1.12 is not verified : Packages shall not contain
+ * equivalent part names and package implementers shall neither
+ * create nor recognize packages with equivalent part names.
*/
protected final POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx, boolean noRelation){
try {
* @param contentType
* Part content type.
* @return The newly created part.
- * @throws InvalidFormatException
+ * @throws PartAlreadyExistsException
* If rule M1.12 is not verified : Packages shall not contain
* equivalent part names and package implementers shall neither
* create nor recognize packages with equivalent part names.
* Specify if the existing relationship part, if any, logically
* associated to the newly created part will be loaded.
* @return The newly created part.
- * @throws InvalidFormatException
+ * @throws PartAlreadyExistsException
* If rule M1.12 is not verified : Packages shall not contain
* equivalent part names and package implementers shall neither
* create nor recognize packages with equivalent part names.
* Relationship unique id.
* @return The newly created and added relationship
*
+ * @throws InvalidOperationException
+ * If a writing operation is done on a read only package or
+ * invalid nested relations are created.
* @throws InvalidFormatException
* If the URI point to a relationship part URI.
+ * @throws IllegalArgumentException if targetPartName, targetMode
+ * or relationshipType are passed as null
* @see org.apache.poi.openxml4j.opc.RelationshipSource#addRelationship(org.apache.poi.openxml4j.opc.PackagePartName,
* org.apache.poi.openxml4j.opc.TargetMode, java.lang.String, java.lang.String)
*/
CTSheet sheet = addSheet(sheetname);
int sheetNumber = 1;
- for(XSSFSheet sh : sheets) {
- sheetNumber = (int)Math.max(sh.sheet.getSheetId() + 1, sheetNumber);
+ outerloop:
+ while(true) {
+ for(XSSFSheet sh : sheets) {
+ sheetNumber = (int)Math.max(sh.sheet.getSheetId() + 1, sheetNumber);
+ }
+
+ // Bug 57165: We also need to check that the resulting file name is not already taken
+ // this can happen when moving/cloning sheets
+ String sheetName = XSSFRelation.WORKSHEET.getFileName(sheetNumber);
+ for(POIXMLDocumentPart relation : getRelations()) {
+ if(relation.getPackagePart() != null &&
+ sheetName.equals(relation.getPackagePart().getPartName().getName())) {
+ // name is taken => try next one
+ sheetNumber++;
+ continue outerloop;
+ }
+ }
+
+ // no duplicate found => use this one
+ break;
}
XSSFSheet wrapper = (XSSFSheet)createRelationship(XSSFRelation.WORKSHEET, XSSFFactory.getInstance(), sheetNumber);
\r
package org.apache.poi.xssf.usermodel;\r
\r
-import static org.junit.Assert.assertEquals;\r
-\r
import java.io.IOException;\r
import java.nio.charset.Charset;\r
import java.util.Calendar;\r
}\r
}\r
\r
- \r
- @Test\r
- public void test57165() throws IOException {\r
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");\r
- try {\r
- removeAllSheetsBut(3, wb);\r
- wb.cloneSheet(0); // Throws exception here\r
- wb.setSheetName(1, "New Sheet");\r
- //saveWorkbook(wb, fileName);\r
- \r
- XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);\r
- try {\r
- \r
- } finally {\r
- wbBack.close();\r
- }\r
- } finally {\r
- wb.close();\r
- }\r
- }\r
-\r
- private static void removeAllSheetsBut(int sheetIndex, Workbook wb)\r
- {\r
- int sheetNb = wb.getNumberOfSheets();\r
- // Move this sheet at the first position\r
- wb.setSheetOrder(wb.getSheetName(sheetIndex), 0);\r
- for (int sn = sheetNb - 1; sn > 0; sn--)\r
- {\r
- wb.removeSheetAt(sn);\r
- }\r
- }\r
-\r
// When this is fixed, the test case should go to BaseTestXCell with \r
// adjustments to use _testDataProvider to also verify this for XSSF\r
public void testBug57294() throws IOException {\r
assertEquals("~CIRCULAR~REF~", FormulaError.forInt(value.getErrorValue()).getString());
assertEquals("CIRCULAR_REF", FormulaError.forInt(value.getErrorValue()).toString());
}
+
+
+ @Test
+ public void test57165() throws IOException {
+ XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
+ try {
+ removeAllSheetsBut(3, wb);
+ wb.cloneSheet(0); // Throws exception here
+ wb.setSheetName(1, "New Sheet");
+ //saveWorkbook(wb, fileName);
+
+ XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ try {
+
+ } finally {
+ wbBack.close();
+ }
+ } finally {
+ wb.close();
+ }
+ }
+
+ private static void removeAllSheetsBut(int sheetIndex, Workbook wb)
+ {
+ int sheetNb = wb.getNumberOfSheets();
+ // Move this sheet at the first position
+ wb.setSheetOrder(wb.getSheetName(sheetIndex), 0);
+ for (int sn = sheetNb - 1; sn > 0; sn--)
+ {
+ wb.removeSheetAt(sn);
+ }
+ }
}