degenerate case of Named Range in that the 'group of cells' contains exactly one
cell. You can create as well as refer to cells in a workbook by their named range.
When working with Named Ranges, the classes: org.apache.poi.hssf.util.CellReference and
- & org.apache.poi.hssf.util.AreaReference are used.
+ & org.apache.poi.hssf.util.AreaReference are used (these
+ work for both XSSF and HSSF, despite the package name).
</p>
<p>
Creating Named Range / Named Cell
<source>
// setup code
String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet(sname);
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = wb.createSheet(sname);
sheet.createRow(0).createCell((short) 0).setCellValue(cvalue);
// 1. create named range for a single cell using areareference
- HSSFName namedCell = wb.createName();
+ Name namedCell = wb.createName();
namedCell.setNameName(cname);
String reference = sname+"!A1:A1"; // area reference
namedCell.setReference(reference);
// 2. create named range for a single cell using cellreference
- HSSFName namedCell = wb.createName();
+ Name namedCell = wb.createName();
namedCell.setNameName(cname);
String reference = sname+"!A1"; // cell reference
namedCell.setReference(reference);
// 3. create named range for an area using AreaReference
- HSSFName namedCell = wb.createName();
+ Name namedCell = wb.createName();
namedCell.setNameName(cname);
String reference = sname+"!A1:C5"; // area reference
namedCell.setReference(reference);
-
</source>
<p>
Reading from Named Range / Named Cell
<source>
// setup code
String cname = "TestName";
- HSSFWorkbook wb = getMyWorkbook(); // retrieve workbook
+ Workbook wb = getMyWorkbook(); // retrieve workbook
// retrieve the named range
int namedCellIdx = wb.getNameIndex(cellName);
- HSSFName aNamedCell = wb.getNameAt(namedCellIdx);
+ Name aNamedCell = wb.getNameAt(namedCellIdx);
// retrieve the cell at the named range and test its contents
AreaReference aref = new AreaReference(aNamedCell.getReference());
CellReference[] crefs = aref.getAllReferencedCells();
for (int i=0; i<crefs.length; i++) {
- HSSFSheet s = wb.getSheet(crefs[i].getSheetName());
- HSSFRow r = sheet.getRow(crefs[i].getRow());
- HSSFCell c = r.getCell(crefs[i].getCol());
+ Sheet s = wb.getSheet(crefs[i].getSheetName());
+ Row r = sheet.getRow(crefs[i].getRow());
+ Cell c = r.getCell(crefs[i].getCol());
// extract the cell contents based on cell type etc.
}
</source>
<source>
// Setup code
String cname = "TestName";
- HSSFWorkbook wb = getMyWorkbook(); // retrieve workbook
+ Workbook wb = getMyWorkbook(); // retrieve workbook
// Retrieve the named range
// Will be something like "$C$10,$D$12:$D$14";
int namedCellIdx = wb.getNameIndex(cellName);
- HSSFName aNamedCell = wb.getNameAt(namedCellIdx);
+ Name aNamedCell = wb.getNameAt(namedCellIdx);
// Retrieve the cell at the named range and test its contents
// Will get back one AreaReference for C10, and
CellReference[] crefs = arefs[i].getCells();
for (int j=0; j<crefs.length; j++) {
// Check it turns into real stuff
- HSSFSheet s = wb.getSheet(crefs[j].getSheetName());
- HSSFRow r = s.getRow(crefs[j].getRow());
- HSSFCell c = r.getCell(crefs[j].getCol());
+ Sheet s = wb.getSheet(crefs[j].getSheetName());
+ Row r = s.getRow(crefs[j].getRow());
+ Cell c = r.getCell(crefs[j].getCol());
// Do something with this corner cell
}
}
</source>
</section>
<anchor id="CellComments"/>
- <section><title>Cell Comments</title>
+ <section><title>Cell Comments - HSSF</title>
<p>
- In Excel a comment is a kind of a text shape,
+ In HSSF Excel, a comment is a kind of a text shape,
so inserting a comment is very similar to placing a text box in a worksheet:
</p>
<source>
comment = sheet.getCellComment(3, 1);
</source>
</section>
+
+ <section><title>Cell Comments - XSSF</title>
+ <p>
+ In XSSF Excel, a comment is still a kind of a text shape, but
+ things are generally much simpler.
+ </p>
+ <source>
+ Workbook wb = new XSSFWorkbook();
+ CreationHelper createHelper = wb.getCreationHelper();
+
+ Sheet sheet = wb.createSheet("Cell comments in POI XSSF");
+
+ //create a cell in row 3
+ Cell cell1 = sheet.createRow(3).createCell((short)1);
+ cell1.setCellValue(createHelper.createRichTextString("Hello, World"));
+
+ //anchor defines size and position of the comment in worksheet
+ Comment comment1 = createHelper.createComment();
+
+ // set text in the comment
+ comment1.setString(createHelper.createRichTextString(
+ "We can set comments in POI"));
+
+ //set comment author.
+ //you can see it in the status bar when moving mouse over the commented cell
+ comment1.setAuthor("Apache Software Foundation");
+
+ // The first way to assign comment to a cell is via HSSFCell.setCellComment method
+ cell1.setCellComment(comment1);
+
+
+ //create another cell in row 6
+ Cell cell2 = sheet.createRow(6).createCell((short)1);
+ cell2.setCellValue(36.6);
+
+
+ Comment comment2 = createHelper.createComment();
+ //modify background color of the comment
+ comment2.setFillColor(204, 236, 255);
+
+ RichTextString string = createHelper.createRichTextString(
+ "Normal body temperature");
+
+ //apply custom font to the text in the comment
+ Font font = wb.createFont();
+ font.setFontName("Arial");
+ font.setFontHeightInPoints((short)10);
+ font.setBoldweight(Font.BOLDWEIGHT_BOLD);
+ font.setColor(Color.RED.index);
+ string.applyFont(font);
+
+ comment2.setString(string);
+ //by default comments are hidden. This one is always visible.
+ comment2.setVisible(true);
+
+ comment2.setAuthor("Bill Gates");
+
+ /**
+ * The second way to assign comment to a cell is to implicitly specify its row and column.
+ * Note, it is possible to set row and column of a non-existing cell.
+ * It works, the commnet is visible.
+ */
+ comment2.setRow(6);
+ comment2.setColumn((short)1);
+
+ FileOutputStream out = new FileOutputStream("poi_comment.xls");
+ wb.write(out);
+ out.close();
+ </source>
+ <p>
+ Reading cell comments
+ </p>
+ <source>
+ Cell cell = sheet.get(3).getColumn((short)1);
+ Comment comment = cell.getCellComment();
+ if (comment != null) {
+ RichTextString str = comment.getString();
+ String author = comment.getAuthor();
+ }
+ // alternatively you can retrieve cell comments by (row, column)
+ comment = sheet.getCellComment(3, 1);
+ </source>
+ </section>
+
<anchor id="Autofit"/>
<section><title>Adjust column width to fit the contents</title>
<source>
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.usermodel;
+
+/**
+ * Allows the getting and saving of cell comments,
+ * associated with a given sheet.
+ */
+public interface CommentsSource {
+ public String getAuthor(long authorId);
+
+ public int findAuthor(String author);
+
+ public Comment findCellComment(int row, int column);
+
+ public Comment findCellComment(String cellRef);
+
+ public void setCellComment (int row, int column, Comment comment);
+
+ public void setCellComment (String cellRef, Comment comment);
+
+ public Comment addComment();
+}
package org.apache.poi.ss.usermodel;
-import java.io.IOException;
-
/**
* Allows the getting and saving of shared strings
*/
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xssf.model;
+
+import java.io.IOException;
+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.xssf.usermodel.XSSFComment;
+import org.apache.poi.xssf.util.CellReference;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlOptions;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CommentsDocument;
+
+public class CommentsTable implements CommentsSource, XSSFModel {
+ private CTComments comments;
+
+ public CommentsTable(InputStream is) throws IOException {
+ readFrom(is);
+ }
+ public CommentsTable() {
+ comments = CTComments.Factory.newInstance();
+ }
+ /**
+ * For unit testing only!
+ */
+ public CommentsTable(CTComments comments) {
+ this.comments = comments;
+ }
+
+ public void readFrom(InputStream is) throws IOException {
+ try {
+ CommentsDocument doc = CommentsDocument.Factory.parse(is);
+ comments = doc.getComments();
+ } catch (XmlException e) {
+ throw new IOException(e.getLocalizedMessage());
+ }
+ }
+ public void writeTo(OutputStream out) throws IOException {
+ XmlOptions options = new XmlOptions();
+ options.setSaveOuter();
+ options.setUseDefaultNamespace();
+
+ // Requests use of whitespace for easier reading
+ options.setSavePrettyPrint();
+
+ CommentsDocument doc = CommentsDocument.Factory.newInstance(options);
+ doc.setComments(comments);
+ doc.save(out, options);
+ }
+
+ public String getAuthor(long authorId) {
+ return getCommentsAuthors().getAuthorArray((int)authorId);
+ }
+
+ public int findAuthor(String author) {
+ for (int i = 0 ; i < getCommentsAuthors().sizeOfAuthorArray() ; i++) {
+ if (getCommentsAuthors().getAuthorArray(i).equals(author)) {
+ return i;
+ }
+ }
+ return addNewAuthor(author);
+ }
+
+ public XSSFComment findCellComment(int row, int column) {
+ return findCellComment(new CellReference().convertRowColToString((short)row, (short)column));
+ }
+
+ public XSSFComment findCellComment(String cellRef) {
+ for (CTComment comment : getCommentsList().getCommentArray()) {
+ if (cellRef.equals(comment.getRef())) {
+ return new XSSFComment(this, comment);
+ }
+ }
+ 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);
+ }
+
+ public XSSFComment addComment() {
+ return new XSSFComment(this, getCommentsList().addNewComment());
+ }
+
+ private CTCommentList getCommentsList() {
+ if (comments.getCommentList() == null) {
+ comments.addNewCommentList();
+ }
+ return comments.getCommentList();
+ }
+
+ private CTAuthors getCommentsAuthors() {
+ if (comments.getAuthors() == null) {
+ comments.addNewAuthors();
+ }
+ return comments.getAuthors();
+ }
+
+ private int addNewAuthor(String author) {
+ int index = getCommentsAuthors().sizeOfAuthorArray();
+ getCommentsAuthors().insertAuthor(index, author);
+ return index;
+ }
+}
\ No newline at end of file
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.usermodel.RichTextString;
-import org.apache.poi.xssf.usermodel.extensions.XSSFComments;
import org.apache.poi.xssf.usermodel.helpers.RichTextStringHelper;
import org.apache.poi.xssf.util.CellReference;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
public class XSSFComment implements Comment {
private CTComment comment;
- private XSSFComments comments;
+ private CommentsSource comments;
- public XSSFComment(XSSFComments comments, CTComment comment) {
+ public XSSFComment(CommentsSource comments, CTComment comment) {
this.comment = comment;
this.comments = comments;
}
- public XSSFComment(XSSFComments sheetComments) {
+ public XSSFComment(CommentsSource sheetComments) {
this(sheetComments, CTComment.Factory.newInstance());
}
import org.apache.poi.hssf.util.Region;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.Patriarch;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.xssf.usermodel.extensions.XSSFComments;
+import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
import org.apache.poi.xssf.util.CellReference;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak;
public class XSSFSheet implements Sheet {
-
protected CTSheet sheet;
protected CTWorksheet worksheet;
protected CTDialogsheet dialogsheet;
- protected CTComments comments;
protected List<Row> rows;
protected ColumnHelper columnHelper;
protected XSSFWorkbook workbook;
- protected XSSFComments sheetComments;
+ protected CommentsSource sheetComments;
public static final short LeftMargin = 0;
public static final short RightMargin = 1;
public static final short HeaderMargin = 4;
public static final short FooterMargin = 5;
- public XSSFSheet(CTSheet sheet, CTWorksheet worksheet, XSSFWorkbook workbook, XSSFComments sheetComments) {
+ public XSSFSheet(CTSheet sheet, CTWorksheet worksheet, XSSFWorkbook workbook, CommentsSource sheetComments) {
this(sheet, worksheet, workbook);
this.sheetComments = sheetComments;
}
this.sheet = sheet;
}
- private XSSFComments getComments() {
+ private CommentsSource getComments() {
if (sheetComments == null) {
- sheetComments = new XSSFComments(getCTComments());
+ sheetComments = new CommentsTable();
}
return sheetComments;
}
-
- private CTComments getCTComments() {
- if (comments == null) {
- comments = CTComments.Factory.newInstance();
- }
- return comments;
- }
-
}
+++ /dev/null
-/* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-==================================================================== */
-package org.apache.poi.xssf.usermodel.extensions;
-
-import org.apache.poi.xssf.usermodel.XSSFComment;
-import org.apache.poi.xssf.util.CellReference;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
-
-public class XSSFComments {
-
- private CTComments comments;
-
- public XSSFComments() {
- this(CTComments.Factory.newInstance());
- }
-
- public XSSFComments(CTComments comments) {
- this.comments = comments;
- }
-
- public String getAuthor(long authorId) {
- return getCommentsAuthors().getAuthorArray((int)authorId);
- }
-
- public int findAuthor(String author) {
- for (int i = 0 ; i < getCommentsAuthors().sizeOfAuthorArray() ; i++) {
- if (getCommentsAuthors().getAuthorArray(i).equals(author)) {
- return i;
- }
- }
- return addNewAuthor(author);
- }
-
- public XSSFComment findCellComment(int row, int column) {
- return findCellComment(new CellReference().convertRowColToString((short)row, (short)column));
- }
-
- public XSSFComment findCellComment(String cellRef) {
- for (CTComment comment : getCommentsList().getCommentArray()) {
- if (cellRef.equals(comment.getRef())) {
- return new XSSFComment(this, comment);
- }
- }
- return null;
- }
-
- public void setCellComment (int row, int column, XSSFComment comment) {
- XSSFComment current = findCellComment(row, column);
- if (current == null) {
- current = addComment();
- }
- current = comment;
- current.setRow(row);
- current.setColumn((short) column);
- }
-
- public void setCellComment (String cellRef, XSSFComment comment) {
- CellReference cellReference = new CellReference(cellRef);
- setCellComment(cellReference.getRow(), cellReference.getCol(), comment);
- }
-
- public XSSFComment addComment() {
- return new XSSFComment(this, getCommentsList().addNewComment());
- }
-
- private CTCommentList getCommentsList() {
- if (comments.getCommentList() == null) {
- comments.addNewCommentList();
- }
- return comments.getCommentList();
- }
-
- private CTAuthors getCommentsAuthors() {
- if (comments.getAuthors() == null) {
- comments.addNewAuthors();
- }
- return comments.getAuthors();
- }
-
- private int addNewAuthor(String author) {
- int index = getCommentsAuthors().sizeOfAuthorArray();
- getCommentsAuthors().insertAuthor(index, author);
- return index;
- }
-
-}
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.model;
+
+import org.apache.poi.xssf.usermodel.XSSFComment;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
+
+import junit.framework.TestCase;
+
+
+public class TestCommentsTable extends TestCase {
+
+ private static final String TEST_A2_TEXT = "test A2 text";
+ private static final String TEST_A1_TEXT = "test A1 text";
+ private static final String TEST_AUTHOR = "test author";
+
+ public void testfindAuthor() {
+ CTComments comments = CTComments.Factory.newInstance();
+ CommentsTable sheetComments = new CommentsTable(comments);
+
+ assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR));
+ assertEquals(1, sheetComments.findAuthor("another author"));
+ assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR));
+ }
+
+ public void testGetCellComment() {
+ CTComments comments = CTComments.Factory.newInstance();
+ CommentsTable sheetComments = new CommentsTable(comments);
+ CTCommentList commentList = comments.addNewCommentList();
+
+ // Create 2 comments for A1 and A" cells
+ CTComment comment0 = commentList.insertNewComment(0);
+ comment0.setRef("A1");
+ CTRst ctrst0 = CTRst.Factory.newInstance();
+ ctrst0.setT(TEST_A1_TEXT);
+ comment0.setText(ctrst0);
+ CTComment comment1 = commentList.insertNewComment(0);
+ comment1.setRef("A2");
+ CTRst ctrst1 = CTRst.Factory.newInstance();
+ ctrst1.setT(TEST_A2_TEXT);
+ comment1.setText(ctrst1);
+
+ // test finding the right comment for a cell
+ assertEquals(TEST_A1_TEXT, sheetComments.findCellComment("A1").getString());
+ assertEquals(TEST_A1_TEXT, sheetComments.findCellComment(0, 0).getString());
+ assertEquals(TEST_A2_TEXT, sheetComments.findCellComment("A2").getString());
+ assertEquals(TEST_A2_TEXT, sheetComments.findCellComment(1, 0).getString());
+ assertNull(sheetComments.findCellComment("A3"));
+ assertNull(sheetComments.findCellComment(2, 0));
+ }
+
+ public void testSetCellComment() {
+ CTComments comments = CTComments.Factory.newInstance();
+ CommentsTable sheetComments = new CommentsTable(comments);
+ CTCommentList commentList = comments.addNewCommentList();
+ assertEquals(0, commentList.sizeOfCommentArray());
+ XSSFComment comment = new XSSFComment(sheetComments);
+ comment.setAuthor("test A1 author");
+
+ sheetComments.setCellComment("A1", comment);
+ assertEquals(1, commentList.sizeOfCommentArray());
+ assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId()));
+
+ }
+
+}
import org.apache.poi.ss.usermodel.SharedStringSource;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.xssf.usermodel.extensions.XSSFComments;
+import org.apache.poi.xssf.model.CommentsTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
CTSheet ctSheet = CTSheet.Factory.newInstance();
CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
CTComments ctComments = CTComments.Factory.newInstance();
- XSSFComments sheetComments = new XSSFComments(ctComments);
+ CommentsTable sheetComments = new CommentsTable(ctComments);
XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, sheetComments);
assertNotNull(sheet);
CTSheet ctSheet = CTSheet.Factory.newInstance();
CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
CTComments ctComments = CTComments.Factory.newInstance();
- XSSFComments comments = new XSSFComments(ctComments);
+ CommentsTable comments = new CommentsTable(ctComments);
XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, comments);
assertNotNull(sheet);
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.ss.usermodel.RichTextString;
-import org.apache.poi.xssf.usermodel.extensions.XSSFComments;
+import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.util.CellReference;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
private static final String TEST_AUTHOR = "test_author";
public void testConstructors() {
- XSSFComments sheetComments = new XSSFComments();
+ CommentsTable sheetComments = new CommentsTable();
XSSFComment comment = new XSSFComment(sheetComments);
assertNotNull(comment);
}
public void testGetColumn() {
- XSSFComments sheetComments = new XSSFComments();
+ CommentsTable sheetComments = new CommentsTable();
CTComment ctComment = CTComment.Factory.newInstance();
ctComment.setRef("A1");
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
}
public void testGetRow() {
- XSSFComments sheetComments = new XSSFComments();
+ CommentsTable sheetComments = new CommentsTable();
CTComment ctComment = CTComment.Factory.newInstance();
ctComment.setRef("A1");
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
ctAuthors.insertAuthor(0, TEST_AUTHOR);
ctComment.setAuthorId(0);
- XSSFComments sheetComments = new XSSFComments(ctComments);
+ CommentsTable sheetComments = new CommentsTable(ctComments);
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
assertNotNull(comment);
assertEquals(TEST_AUTHOR, comment.getAuthor());
}
public void testSetColumn() {
- XSSFComments sheetComments = new XSSFComments();
+ CommentsTable sheetComments = new CommentsTable();
CTComment ctComment = CTComment.Factory.newInstance();
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
comment.setColumn((short)3);
}
public void testSetRow() {
- XSSFComments sheetComments = new XSSFComments();
+ CommentsTable sheetComments = new CommentsTable();
CTComment ctComment = CTComment.Factory.newInstance();
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
comment.setRow(20);
}
public void testSetAuthor() {
- XSSFComments sheetComments = new XSSFComments();
+ CommentsTable sheetComments = new CommentsTable();
CTComment ctComment = CTComment.Factory.newInstance();
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
comment.setAuthor(TEST_AUTHOR);
}
public void testSetString() {
- XSSFComments sheetComments = new XSSFComments();
+ CommentsTable sheetComments = new CommentsTable();
CTComment ctComment = CTComment.Factory.newInstance();
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
RichTextString richTextString = new HSSFRichTextString(TEST_RICHTEXTSTRING);
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.xssf.usermodel.extensions.XSSFComments;
+import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
CTSheet ctSheet = CTSheet.Factory.newInstance();
CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
CTComments ctComments = CTComments.Factory.newInstance();
- XSSFComments sheetComments = new XSSFComments(ctComments);
+ CommentsTable sheetComments = new CommentsTable(ctComments);
XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, sheetComments);
assertNotNull(sheet);
XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook);
Cell cell = sheet.createRow(0).createCell((short)0);
CTComments ctComments = CTComments.Factory.newInstance();
- XSSFComments comments = new XSSFComments(ctComments);
+ CommentsTable comments = new CommentsTable(ctComments);
XSSFComment comment = comments.addComment();
sheet.setCellComment("A1", comment);
+++ /dev/null
-/* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-==================================================================== */
-
-package org.apache.poi.xssf.usermodel.extensions;
-
-import org.apache.poi.xssf.usermodel.XSSFComment;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
-
-import junit.framework.TestCase;
-
-
-public class TestXSSFComments extends TestCase {
-
- private static final String TEST_A2_TEXT = "test A2 text";
- private static final String TEST_A1_TEXT = "test A1 text";
- private static final String TEST_AUTHOR = "test author";
-
- public void testfindAuthor() {
- CTComments comments = CTComments.Factory.newInstance();
- XSSFComments sheetComments = new XSSFComments(comments);
-
- assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR));
- assertEquals(1, sheetComments.findAuthor("another author"));
- assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR));
- }
-
- public void testGetCellComment() {
- CTComments comments = CTComments.Factory.newInstance();
- XSSFComments sheetComments = new XSSFComments(comments);
- CTCommentList commentList = comments.addNewCommentList();
-
- // Create 2 comments for A1 and A" cells
- CTComment comment0 = commentList.insertNewComment(0);
- comment0.setRef("A1");
- CTRst ctrst0 = CTRst.Factory.newInstance();
- ctrst0.setT(TEST_A1_TEXT);
- comment0.setText(ctrst0);
- CTComment comment1 = commentList.insertNewComment(0);
- comment1.setRef("A2");
- CTRst ctrst1 = CTRst.Factory.newInstance();
- ctrst1.setT(TEST_A2_TEXT);
- comment1.setText(ctrst1);
-
- // test finding the right comment for a cell
- assertEquals(TEST_A1_TEXT, sheetComments.findCellComment("A1").getString());
- assertEquals(TEST_A1_TEXT, sheetComments.findCellComment(0, 0).getString());
- assertEquals(TEST_A2_TEXT, sheetComments.findCellComment("A2").getString());
- assertEquals(TEST_A2_TEXT, sheetComments.findCellComment(1, 0).getString());
- assertNull(sheetComments.findCellComment("A3"));
- assertNull(sheetComments.findCellComment(2, 0));
- }
-
- public void testSetCellComment() {
- CTComments comments = CTComments.Factory.newInstance();
- XSSFComments sheetComments = new XSSFComments(comments);
- CTCommentList commentList = comments.addNewCommentList();
- assertEquals(0, commentList.sizeOfCommentArray());
- XSSFComment comment = new XSSFComment(sheetComments);
- comment.setAuthor("test A1 author");
-
- sheetComments.setCellComment("A1", comment);
- assertEquals(1, commentList.sizeOfCommentArray());
- assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId()));
-
- }
-
-}