diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java index 190b740fb..88e0971e7 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java @@ -24,6 +24,8 @@ package org.apache.poi.ss.usermodel; public interface CommentsSource { public String getAuthor(long authorId); + public int getNumberOfComments(); + public int findAuthor(String author); public Comment findCellComment(int row, int column); diff --git a/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java b/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java index 4372e0e64..02602551e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java @@ -69,6 +69,10 @@ public class CommentsTable implements CommentsSource, XSSFModel { doc.save(out, options); } + public int getNumberOfComments() { + return comments.getCommentList().sizeOfCommentArray(); + } + public String getAuthor(long authorId) { return getCommentsAuthors().getAuthorArray((int)authorId); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index a9476d453..4444017d3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -911,4 +911,13 @@ public class XSSFSheet implements Sheet { } return sheetComments; } + + /** + * Does this sheet have any comments on it? We need to know, + * so we can decide about writing it to disk or not + */ + public boolean hasComments() { + if(sheetComments == null) { return false; } + return (sheetComments.getNumberOfComments() > 0); + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 05479d9b7..cbaa92b8c 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -29,10 +29,10 @@ import javax.xml.namespace.QName; import org.apache.poi.POIXMLDocument; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CommentsSource; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.Font; -import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.usermodel.Palette; import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.SharedStringSource; @@ -41,6 +41,7 @@ import org.apache.poi.ss.usermodel.StylesSource; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; +import org.apache.poi.xssf.model.CommentsTable; import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.XSSFModel; @@ -105,6 +106,18 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { "/xl/image#.xml", null ); + public static final XSSFRelation SHEET_COMMENTS = new XSSFRelation( + "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments", + "/xl/comments#.xml", + CommentsTable.class + ); + public static final XSSFRelation SHEET_HYPERLINKS = new XSSFRelation( + null, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", + null, + null + ); public static class XSSFRelation { private String TYPE; @@ -241,8 +254,20 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { log.log(POILogger.WARN, "Sheet with name " + ctSheet.getName() + " and r:id " + ctSheet.getId()+ " was defined, but didn't exist in package, skipping"); continue; } + + // Get the comments for the sheet, if there are any + CommentsSource comments = null; + PackageRelationshipCollection commentsRel = + part.getRelationshipsByType(SHEET_COMMENTS.REL); + if(commentsRel != null && commentsRel.size() > 0) { + PackagePart commentsPart = + getTargetPart(commentsRel.getRelationship(0)); + comments = new CommentsTable(commentsPart.getInputStream()); + } + + // Now create the sheet WorksheetDocument worksheetDoc = WorksheetDocument.Factory.parse(part.getInputStream()); - XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this); + XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this, comments); this.sheets.add(sheet); } } catch (XmlException e) { @@ -656,6 +681,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { // Update our internal reference for the package part workbook.getSheets().getSheetArray(i).setId(rel.getId()); workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber); + + // If our sheet has comments, then write out those + // TODO } // Write shared strings and styles diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java index c231ba69c..12ecce94a 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java @@ -17,7 +17,12 @@ package org.apache.poi.xssf.model; +import java.io.File; + +import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFComment; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; @@ -78,7 +83,26 @@ public class TestCommentsTable extends TestCase { sheetComments.setCellComment("A1", comment); assertEquals(1, commentList.sizeOfCommentArray()); assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId())); - } - + + public void testExisting() throws Exception { + File xml = new File( + System.getProperty("HSSF.testdata.path") + + File.separator + "WithVariousData.xlsx" + ); + assertTrue(xml.exists()); + + XSSFWorkbook workbook = new XSSFWorkbook(xml.toString()); + Sheet sheet1 = workbook.getSheetAt(0); + Sheet sheet2 = workbook.getSheetAt(1); + + assertTrue( ((XSSFSheet)sheet1).hasComments() ); + assertFalse( ((XSSFSheet)sheet2).hasComments() ); + + // TODO - check rest of comments + } + + public void testWriteRead() throws Exception { + // TODO + } }