import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.SharedStringSource;
+import org.apache.poi.xssf.util.CellReference;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
}
public Comment getCellComment() {
- // TODO Auto-generated method stub
- return null;
+ return row.getSheet().getCellComment(row.getRowNum(), getCellNum());
}
public String getCellFormula() {
}
public void setAsActiveCell() {
- // TODO Auto-generated method stub
-
+ row.getSheet().setActiveCell(cell.getR());
}
public void setCellComment(Comment comment) {
- // TODO Auto-generated method stub
-
+ String cellRef = new CellReference().convertRowColToString((short) row.getRowNum(), getCellNum());
+ row.getSheet().setCellComment(cellRef, (XSSFComment)comment);
}
public void setCellErrorValue(byte value) {
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.RichTextString;
-import org.apache.poi.xssf.usermodel.extensions.XSSFSheetComments;
+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;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
public class XSSFComment implements Comment {
private CTComment comment;
- private XSSFSheetComments sheetComments;
+ private XSSFComments comments;
- public XSSFComment(XSSFSheetComments sheetComments, CTComment comment) {
+ public XSSFComment(XSSFComments comments, CTComment comment) {
this.comment = comment;
- this.sheetComments = sheetComments;
+ this.comments = comments;
}
- public XSSFComment(XSSFSheetComments sheetComments) {
+ public XSSFComment(XSSFComments sheetComments) {
this(sheetComments, CTComment.Factory.newInstance());
}
public String getAuthor() {
- return sheetComments.getAuthor(comment.getAuthorId());
+ return comments.getAuthor(comment.getAuthorId());
}
public short getColumn() {
public boolean isVisible() {
// TODO Auto-generated method stub
- return false;
+ return true;
}
public void setAuthor(String author) {
- sheetComments.findAuthor(author);
+ comments.findAuthor(author);
}
public void setColumn(short col) {
}
public void setString(RichTextString string) {
- // TODO Auto-generated method stub
-
+ CTRst text = comment.addNewText();
+ RichTextStringHelper.convertToRst(string, text);
+ }
+
+ public void setString(String string) {
+ RichTextString richTextString = new XSSFRichTextString(string);
+ setString(richTextString);
}
public void setVisible(boolean visible) {
// TODO Auto-generated method stub
}
+
+ public String getString() {
+ return comment.getText().getT().toString();
+ }
}
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.usermodel.helpers.ColumnHelper;
import org.apache.poi.xssf.util.CellReference;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
+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;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetUpPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPrintOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSelection;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetPr;
protected CTSheet sheet;
protected CTWorksheet worksheet;
protected CTDialogsheet dialogsheet;
- protected CTComment comment;
+ protected CTComments comments;
protected List<Row> rows;
protected ColumnHelper columnHelper;
protected XSSFWorkbook workbook;
+ protected XSSFComments 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) {
+ this(sheet, worksheet, workbook);
+ this.sheetComments = sheetComments;
+ }
+
public XSSFSheet(CTSheet sheet, CTWorksheet worksheet, XSSFWorkbook workbook) {
this.workbook = workbook;
this.sheet = sheet;
}
public Comment getCellComment(int row, int column) {
- // TODO Auto-generated method stub
- return null;
+ return getComments().findCellComment(row, column);
}
public short[] getColumnBreaks() {
CTSheetView view = getDefaultSheetView();
return view != null && view.getTabSelected();
}
+
+ public void setCellComment(String cellRef, XSSFComment comment) {
+ getComments().setCellComment(cellRef, comment);
+ }
+
+ public String getActiveCell() {
+ return getSheetTypeSelection().getActiveCell();
+ }
+
+ public void setActiveCell(String cellRef) {
+ getSheetTypeSelection().setActiveCell(cellRef);
+ }
+
+ private CTSelection getSheetTypeSelection() {
+ if (getSheetTypeSheetView().sizeOfSelectionArray() == 0) {
+ getSheetTypeSheetView().insertNewSelection(0);
+ }
+ return getSheetTypeSheetView().getSelectionArray(0);
+ }
/**
* Return the default sheet view. This is the last one if the sheet's views, according to sec. 3.3.1.83
private void setSheet(CTSheet sheet) {
this.sheet = sheet;
}
+
+ private XSSFComments getComments() {
+ if (sheetComments == null) {
+ sheetComments = new XSSFComments(getCTComments());
+ }
+ return sheetComments;
+ }
+
+ private CTComments getCTComments() {
+ if (comments == null) {
+ comments = CTComments.Factory.newInstance();
+ }
+ return comments;
+ }
}
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Sheet;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
--- /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;
+ }
+
+}
-/* ====================================================================
- 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.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
-
-public class XSSFSheetComments {
-
- private CTComments comments;
-
- public XSSFSheetComments() {
- this(CTComments.Factory.newInstance());
- }
-
- public XSSFSheetComments(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);
- }
-
- 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.usermodel.helpers;
+
+import org.apache.poi.ss.usermodel.RichTextString;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
+
+public class RichTextStringHelper {
+
+ public static void convertToRst(RichTextString string, CTRst text) {
+ // TODO: implement RichTextString to Rst conversion
+ text.setT(string.getString());
+ }
+
+}
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.SharedStringSource;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.extensions.XSSFComments;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
public class TestXSSFCell extends TestCase {
- /**
+ private static final String TEST_C10_AUTHOR = "test C10 author";
+
+ /**
* Test setting and getting boolean values.
*/
public void testSetGetBoolean() throws Exception {
row.setRowNum(32767);
assertEquals("IV32768", cell.formatPosition());
}
-
- private XSSFRow createParentObjects() {
- XSSFWorkbook wb = new XSSFWorkbook();
- wb.setSharedStringSource(new DummySharedStringSource());
- XSSFSheet sheet = new XSSFSheet(wb);
- XSSFRow row = new XSSFRow(sheet);
- return row;
- }
public static class DummySharedStringSource implements SharedStringSource {
ArrayList<String> strs = new ArrayList<String>();
return strs.size() - 1;
}
}
+
+ public void testGetCellComment() {
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ CTSheet ctSheet = CTSheet.Factory.newInstance();
+ CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
+ CTComments ctComments = CTComments.Factory.newInstance();
+ XSSFComments sheetComments = new XSSFComments(ctComments);
+ XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, sheetComments);
+ assertNotNull(sheet);
+
+ // Create C10 cell
+ Row row = sheet.createRow(9);
+ Cell cell = row.createCell((short)2);
+ Cell cell3 = row.createCell((short)3);
+
+
+ // Set a comment for C10 cell
+ CTComment ctComment = ctComments.addNewCommentList().insertNewComment(0);
+ ctComment.setRef("C10");
+ ctComment.setAuthorId(sheetComments.findAuthor(TEST_C10_AUTHOR));
+
+ assertNotNull(sheet.getRow(9).getCell((short)2));
+ assertNotNull(sheet.getRow(9).getCell((short)2).getCellComment());
+ assertEquals(TEST_C10_AUTHOR, sheet.getRow(9).getCell((short)2).getCellComment().getAuthor());
+ assertNull(sheet.getRow(9).getCell((short)3).getCellComment());
+ }
+
+ public void testSetCellComment() {
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ CTSheet ctSheet = CTSheet.Factory.newInstance();
+ CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
+ CTComments ctComments = CTComments.Factory.newInstance();
+ XSSFComments comments = new XSSFComments(ctComments);
+ XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, comments);
+ assertNotNull(sheet);
+
+ // Create C10 cell
+ Row row = sheet.createRow(9);
+ Cell cell = row.createCell((short)2);
+ Cell cell3 = row.createCell((short)3);
+
+ // Create a comment
+ Comment comment = comments.addComment();
+ comment.setAuthor(TEST_C10_AUTHOR);
+
+ // Set a comment for C10 cell
+ cell.setCellComment(comment);
+
+ CTCell ctCell = ctWorksheet.getSheetData().getRowArray(0).getCArray(0);
+ assertNotNull(ctCell);
+ assertEquals("C10", ctCell.getR());
+ long authorId = ctComments.getCommentList().getCommentArray(0).getAuthorId();
+ assertEquals(TEST_C10_AUTHOR, comments.getAuthor(authorId));
+ }
+
+ public void testSetAsActiveCell() {
+ Workbook workbook = new XSSFWorkbook();
+ CTSheet ctSheet = CTSheet.Factory.newInstance();
+ CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
+ XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook);
+ Cell cell = sheet.createRow(0).createCell((short)0);
+ cell.setAsActiveCell();
+
+ assertEquals("A1", ctWorksheet.getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell());
+ }
+
+ private XSSFRow createParentObjects() {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ wb.setSharedStringSource(new DummySharedStringSource());
+ XSSFSheet sheet = new XSSFSheet(wb);
+ XSSFRow row = new XSSFRow(sheet);
+ return row;
+ }
}
package org.apache.poi.xssf.usermodel;
-import org.apache.poi.xssf.usermodel.extensions.XSSFSheetComments;
+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.util.CellReference;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
public class TestXSSFComment extends TestCase {
+ private static final String TEST_RICHTEXTSTRING = "test richtextstring";
private static final String TEST_AUTHOR = "test_author";
public void testConstructors() {
- XSSFSheetComments sheetComments = new XSSFSheetComments();
+ XSSFComments sheetComments = new XSSFComments();
XSSFComment comment = new XSSFComment(sheetComments);
assertNotNull(comment);
}
public void testGetColumn() {
- XSSFSheetComments sheetComments = new XSSFSheetComments();
+ XSSFComments sheetComments = new XSSFComments();
CTComment ctComment = CTComment.Factory.newInstance();
ctComment.setRef("A1");
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
}
public void testGetRow() {
- XSSFSheetComments sheetComments = new XSSFSheetComments();
+ XSSFComments sheetComments = new XSSFComments();
CTComment ctComment = CTComment.Factory.newInstance();
ctComment.setRef("A1");
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
ctAuthors.insertAuthor(0, TEST_AUTHOR);
ctComment.setAuthorId(0);
- XSSFSheetComments sheetComments = new XSSFSheetComments(ctComments);
+ XSSFComments sheetComments = new XSSFComments(ctComments);
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
assertNotNull(comment);
assertEquals(TEST_AUTHOR, comment.getAuthor());
}
public void testSetColumn() {
- XSSFSheetComments sheetComments = new XSSFSheetComments();
+ XSSFComments sheetComments = new XSSFComments();
CTComment ctComment = CTComment.Factory.newInstance();
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
comment.setColumn((short)3);
}
public void testSetRow() {
- XSSFSheetComments sheetComments = new XSSFSheetComments();
+ XSSFComments sheetComments = new XSSFComments();
CTComment ctComment = CTComment.Factory.newInstance();
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
comment.setRow(20);
}
public void testSetAuthor() {
- XSSFSheetComments sheetComments = new XSSFSheetComments();
+ XSSFComments sheetComments = new XSSFComments();
CTComment ctComment = CTComment.Factory.newInstance();
XSSFComment comment = new XSSFComment(sheetComments, ctComment);
comment.setAuthor(TEST_AUTHOR);
assertEquals(TEST_AUTHOR, comment.getAuthor());
}
+
+ public void testSetString() {
+ XSSFComments sheetComments = new XSSFComments();
+ CTComment ctComment = CTComment.Factory.newInstance();
+ XSSFComment comment = new XSSFComment(sheetComments, ctComment);
+ RichTextString richTextString = new HSSFRichTextString(TEST_RICHTEXTSTRING);
+ comment.setString(richTextString);
+ assertEquals(TEST_RICHTEXTSTRING, ctComment.getText().getT());
+ }
}
package org.apache.poi.xssf.usermodel;
import java.util.Iterator;
-
import junit.framework.TestCase;
-
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.helpers.ColumnHelper;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
public class TestXSSFSheet extends TestCase {
assertNull(sheet6.getRow(7));
assertEquals(8, sheet6.getPhysicalNumberOfRows());
}
+
+ public void testGetCellComment() {
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ CTSheet ctSheet = CTSheet.Factory.newInstance();
+ CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
+ CTComments ctComments = CTComments.Factory.newInstance();
+ XSSFComments sheetComments = new XSSFComments(ctComments);
+ XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, workbook, sheetComments);
+ assertNotNull(sheet);
+
+ CTComment ctComment = ctComments.addNewCommentList().insertNewComment(0);
+ ctComment.setRef("C10");
+ ctComment.setAuthorId(sheetComments.findAuthor("test C10 author"));
+
+ assertNotNull(sheet.getCellComment(9, 2));
+ assertEquals("test C10 author", sheet.getCellComment(9, 2).getAuthor());
+ }
+
+ public void testSetCellComment() {
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ CTSheet ctSheet = CTSheet.Factory.newInstance();
+ CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
+ 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);
+ XSSFComment comment = comments.addComment();
+
+ sheet.setCellComment("A1", comment);
+ assertEquals("A1", ctComments.getCommentList().getCommentArray(0).getRef());
+ comment.setAuthor("test A1 author");
+ assertEquals("test A1 author", comments.getAuthor(ctComments.getCommentList().getCommentArray(0).getAuthorId()));
+ }
+
+ public void testGetActiveCell() {
+ Workbook workbook = new XSSFWorkbook();
+ CTSheet ctSheet = CTSheet.Factory.newInstance();
+ CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
+ XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook);
+ ctWorksheet.addNewSheetViews().addNewSheetView().addNewSelection().setActiveCell("R5");
+
+ assertEquals("R5", sheet.getActiveCell());
+
+ }
+
private XSSFSheet createSheet(XSSFWorkbook workbook, String name) {
XSSFSheet sheet = (XSSFSheet) workbook.createSheet(name);
--- /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()));
+
+ }
+
+}
-/* ====================================================================
- 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.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
-
-import junit.framework.TestCase;
-
-
-public class TestXSSFSheetComments extends TestCase {
-
- private static final String TEST_AUTHOR = "test author";
-
- public void testfindAuthor() {
- CTComments comments = CTComments.Factory.newInstance();
- XSSFSheetComments sheetComments = new XSSFSheetComments(comments);
-
- assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR));
- assertEquals(1, sheetComments.findAuthor("another author"));
- assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR));
-
- }
-
-}
import junit.framework.TestCase;
import org.apache.poi.xssf.usermodel.helpers.HeaderFooterHelper;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
public class TestHeaderFooterHelper extends TestCase {