]> source.dussan.org Git - poi.git/commitdiff
Fix bug #51850 - support creating comments in XSSF on an earlier slide when later...
authorNick Burch <nick@apache.org>
Thu, 22 Sep 2011 10:37:54 +0000 (10:37 +0000)
committerNick Burch <nick@apache.org>
Thu, 22 Sep 2011 10:37:54 +0000 (10:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1174048 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

index ef7ad52d5b5fd818f36cd230416f42bae283a159..72f74129dafae9a3f921fac9d1edeb7af4d6050b 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta5" date="2011-??-??">
+           <action dev="poi-developers" type="fix">51850 - support creating comments in XSSF on an earlier slide when later ones already have them</action>
            <action dev="poi-developers" type="add">51804 - optionally include Master Slide text in XSLF text extraction, as HSLF already offers</action>
            <action dev="poi-developers" type="add">New PackagePart method getRelatedPart(PackageRelationship) to simplify navigation of relations between OPC Parts</action>
            <action dev="poi-developers" type="fix">51832 - handle XLS files where the WRITEPROTECT record preceeds the FILEPASS one, rather than following as normal</action>
index 880af27650678175c4c3f65f730a7067ae6a52f3..fe147b622817271f4ea5161276eb9be8d3a2e6f8 100644 (file)
@@ -23,6 +23,7 @@ import java.util.Map.Entry;
 
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
+import org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException;
 import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackagePartName;
@@ -360,8 +361,13 @@ public class POIXMLDocumentPart {
                 addRelation(rel.getId(),doc);
             }
             return doc;
+        } catch (PartAlreadyExistsException pae) {
+           // Return the specific exception so the user knows
+           //  that the name is already taken
+           throw pae;
         } catch (Exception e){
-            throw new POIXMLException(e);
+           // Give a general wrapped exception for the problem
+           throw new POIXMLException(e);
         }
     }
 
index a78162917bea1c21f5280741d9c3915783585f90..e87dd0daaac7fc76948bc9e3fe5fbc509658a80a 100644 (file)
@@ -35,6 +35,7 @@ import org.apache.poi.POIXMLException;
 import org.apache.poi.hssf.record.PasswordRecord;
 import org.apache.poi.hssf.util.PaneInformation;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
@@ -2613,7 +2614,18 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
      */
     protected CommentsTable getCommentsTable(boolean create) {
         if(sheetComments == null && create){
-            sheetComments = (CommentsTable)createRelationship(XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), (int)sheet.getSheetId());
+           // Try to create a comments table with the same number as
+           //  the sheet has (i.e. sheet 1 -> comments 1)
+           try {
+              sheetComments = (CommentsTable)createRelationship(
+                    XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), (int)sheet.getSheetId());
+           } catch(PartAlreadyExistsException e) {
+              // Technically a sheet doesn't need the same number as
+              //  it's comments, and clearly someone has already pinched
+              //  our number! Go for the next available one instead
+              sheetComments = (CommentsTable)createRelationship(
+                    XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), -1);
+           }
         }
         return sheetComments;
     }
index 045e043fc65169367c51228a75be66506a125d4d..dd62f7d66804c6de8da3cc24b551d76a0f13af2d 100644 (file)
@@ -1177,13 +1177,14 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
      * Add comments to Sheet 1, when Sheet 2 already has
      *  comments (so /xl/comments1.xml is taken)
      */
-    public void DISABLEDtest51850() {
+    public void test51850() {
        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51850.xlsx");
        XSSFSheet sh1 = wb.getSheetAt(0);
        XSSFSheet sh2 = wb.getSheetAt(1);
  
        // Sheet 2 has comments
        assertNotNull(sh2.getCommentsTable(false));
+       assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
        
        // Sheet 1 doesn't (yet)
        assertNull(sh1.getCommentsTable(false));
@@ -1198,12 +1199,35 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
        anchor.setRow1(0);
        anchor.setRow2(1);
 
-       Comment excelComment = drawing.createCellComment(anchor);
-       excelComment.setString(
+       Comment comment1 = drawing.createCellComment(anchor);
+       comment1.setString(
              factory.createRichTextString("I like this cell. It's my favourite."));
-       excelComment.setAuthor("Bob T. Fish");
+       comment1.setAuthor("Bob T. Fish");
+       
+       Comment comment2 = drawing.createCellComment(anchor);
+       comment2.setString(
+             factory.createRichTextString("This is much less fun..."));
+       comment2.setAuthor("Bob T. Fish");
 
-       Cell c = sh1.getRow(0).getCell(4);
-       c.setCellComment(excelComment);
+       Cell c1 = sh1.getRow(0).createCell(4);
+       c1.setCellValue(2.3);
+       c1.setCellComment(comment1);
+       
+       Cell c2 = sh1.getRow(0).createCell(5);
+       c2.setCellValue(2.1);
+       c2.setCellComment(comment2);
+       
+       
+       // Save and re-load
+       wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
+       sh1 = wb.getSheetAt(0);
+       sh2 = wb.getSheetAt(1);
+       
+       // Check the comments
+       assertNotNull(sh2.getCommentsTable(false));
+       assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
+       
+       assertNotNull(sh1.getCommentsTable(false));
+       assertEquals(2, sh1.getCommentsTable(false).getNumberOfComments());
     }
 }