public Comment findCellComment(String cellRef);
- public void setCellComment (int row, int column, Comment comment);
-
- public void setCellComment (String cellRef, Comment comment);
-
public Comment addComment();
}
import java.io.InputStream;
import java.io.OutputStream;
-import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFComment;
}
return null;
}
-
- public void setCellComment (int row, int column, Comment comment) {
- XSSFComment current = findCellComment(row, column);
- if (current == null) {
- current = addComment();
- }
- current = (XSSFComment)comment;
- current.setRow(row);
- current.setColumn((short) column);
- }
-
- public void setCellComment (String cellRef, Comment comment) {
- CellReference cellReference = new CellReference(cellRef);
- setCellComment(cellReference.getRow(), cellReference.getCol(), comment);
- }
+ /**
+ * Generates a new XSSFComment, associated with the
+ * current comments list.
+ */
public XSSFComment addComment() {
return new XSSFComment(this, getCommentsList().addNewComment());
}
private CTComment comment;
private CommentsSource comments;
+ /**
+ * Creates a new XSSFComment, associated with a given
+ * low level comment object.
+ * If, as an end user, you want a new XSSFComment
+ * object, the please ask your sheet for one.
+ */
public XSSFComment(CommentsSource comments, CTComment comment) {
this.comment = comment;
this.comments = comments;
}
- public XSSFComment(CommentsSource sheetComments) {
- this(sheetComments, CTComment.Factory.newInstance());
- }
-
public String getAuthor() {
return comments.getAuthor(comment.getAuthorId());
}
public void setVisible(boolean visible) {
// TODO Auto-generated method stub
-
}
}
}
public void setCellComment(String cellRef, XSSFComment comment) {
- getComments().setCellComment(cellRef, comment);
+ CellReference cellReference = new CellReference(cellRef);
+
+ comment.setRow(cellReference.getRow());
+ comment.setColumn((short)cellReference.getCol());
}
public void setCellHyperlink(XSSFHyperlink hyperlink) {
if(sheetComments == null) { return false; }
return (sheetComments.getNumberOfComments() > 0);
}
+ protected int getNumberOfComments() {
+ if(sheetComments == null) { return 0; }
+ return sheetComments.getNumberOfComments();
+ }
private CTSelection getSheetTypeSelection() {
if (getSheetTypeSheetView().sizeOfSelectionArray() == 0) {
assertNull(sheetComments.findCellComment(2, 0));
}
- public void testSetCellComment() {
+ public void testAddCellComment() {
CTComments comments = CTComments.Factory.newInstance();
CommentsTable sheetComments = new CommentsTable(comments);
CTCommentList commentList = comments.addNewCommentList();
assertEquals(0, commentList.sizeOfCommentArray());
- XSSFComment comment = new XSSFComment(sheetComments);
+
+ XSSFComment comment = sheetComments.addComment();
comment.setAuthor("test A1 author");
+ comment.setRow(0);
+ comment.setColumn((short)0);
- sheetComments.setCellComment("A1", comment);
assertEquals(1, commentList.sizeOfCommentArray());
assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId()));
assertEquals("test A1 author", comment.getAuthor());
package org.apache.poi.xssf.usermodel;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.RichTextString;
+import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.model.CommentsTable;
+import org.openxml4j.opc.Package;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
public void testConstructors() {
CommentsTable sheetComments = new CommentsTable();
- XSSFComment comment = new XSSFComment(sheetComments);
+ XSSFComment comment = sheetComments.addComment();
assertNotNull(comment);
CTComment ctComment = CTComment.Factory.newInstance();
assertEquals(TEST_RICHTEXTSTRING, ctComment.getText().getT());
}
+ /**
+ * Tests that we can add comments to a new
+ * file, save, load, and still see them
+ * @throws Exception
+ */
+ public void testCreateSave() throws Exception {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ XSSFSheet s1 = (XSSFSheet)wb.createSheet();
+ Row r1 = s1.createRow(0);
+ Cell r1c1 = r1.createCell(0);
+ r1c1.setCellValue(2.2);
+
+ assertEquals(0, s1.getNumberOfComments());
+
+ Comment c1 = s1.createComment();
+ c1.setAuthor("Author 1");
+ c1.setString(new XSSFRichTextString("Comment 1"));
+ r1c1.setCellComment(c1);
+
+ assertEquals(1, s1.getNumberOfComments());
+
+ // Save and re-load
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ wb.write(baos);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+
+ wb = new XSSFWorkbook(Package.open(bais));
+ s1 = (XSSFSheet)wb.getSheetAt(0);
+
+ assertEquals(1, s1.getNumberOfComments());
+ assertNotNull(s1.getRow(0).getCell(0).getCellComment());
+ assertEquals("Author 1", s1.getRow(0).getCell(0).getCellComment().getAuthor());
+ assertEquals("Comment 1", s1.getRow(0).getCell(0).getCellComment().getString().getString());
+
+ // Now add an orphaned one
+ Comment c2 = s1.createComment();
+ c2.setAuthor("Author 2");
+ c2.setString(new XSSFRichTextString("Second Comment"));
+ c2.setRow(0);
+ c2.setColumn((short)1);
+ assertEquals(2, s1.getNumberOfComments());
+
+ // Save and re-load
+ baos = new ByteArrayOutputStream();
+ wb.write(baos);
+ bais = new ByteArrayInputStream(baos.toByteArray());
+
+ wb = new XSSFWorkbook(Package.open(bais));
+ s1 = (XSSFSheet)wb.getSheetAt(0);
+
+ assertEquals(2, s1.getNumberOfComments());
+ assertNotNull(s1.getCellComment(0, 0));
+ assertNotNull(s1.getCellComment(0, 1));
+
+ assertEquals("Author 1", s1.getCellComment(0, 0).getAuthor());
+ assertEquals("Author 2", s1.getCellComment(0, 1).getAuthor());
+ }
}