Applied patches for bug #44566, thanks to Paolo Mottadelli <paolo.moz@gmail.com>, <p.mottadelli@sourcesense.com>

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@634930 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ugo Cei 2008-03-08 11:19:23 +00:00
parent f848978e7c
commit c2e6c06bb4
13 changed files with 441 additions and 140 deletions

View File

@ -26,6 +26,7 @@ import org.apache.poi.ss.usermodel.CellStyle;
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;
@ -73,8 +74,7 @@ public class XSSFCell implements Cell {
}
public Comment getCellComment() {
// TODO Auto-generated method stub
return null;
return row.getSheet().getCellComment(row.getRowNum(), getCellNum());
}
public String getCellFormula() {
@ -164,13 +164,12 @@ public class XSSFCell implements Cell {
}
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) {

View File

@ -18,26 +18,28 @@ package org.apache.poi.xssf.usermodel;
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() {
@ -50,11 +52,11 @@ public class XSSFComment implements Comment {
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) {
@ -76,13 +78,22 @@ public class XSSFComment implements Comment {
}
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();
}
}

View File

@ -31,10 +31,11 @@ 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.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;
@ -42,6 +43,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins;
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;
@ -56,10 +58,11 @@ public class XSSFSheet implements Sheet {
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;
@ -68,6 +71,11 @@ public class XSSFSheet implements Sheet {
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;
@ -204,8 +212,7 @@ public class XSSFSheet implements Sheet {
}
public Comment getCellComment(int row, int column) {
// TODO Auto-generated method stub
return null;
return getComments().findCellComment(row, column);
}
public short[] getColumnBreaks() {
@ -831,6 +838,25 @@ public class XSSFSheet implements Sheet {
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
@ -858,5 +884,19 @@ public class XSSFSheet implements Sheet {
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;
}
}

View File

@ -18,7 +18,6 @@
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;

View File

@ -0,0 +1,103 @@
/* ====================================================================
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;
}
}

View File

@ -1,60 +0,0 @@
/* ====================================================================
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;
}
}

View File

@ -0,0 +1,29 @@
/* ====================================================================
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());
}
}

View File

@ -24,14 +24,24 @@ import java.util.Date;
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 {
@ -191,14 +201,6 @@ public class TestXSSFCell extends TestCase {
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>();
@ -214,4 +216,77 @@ public class TestXSSFCell extends TestCase {
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;
}
}

View File

@ -17,7 +17,9 @@
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;
@ -28,10 +30,11 @@ import junit.framework.TestCase;
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);
@ -41,7 +44,7 @@ public class TestXSSFComment extends TestCase {
}
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);
@ -52,7 +55,7 @@ public class TestXSSFComment extends TestCase {
}
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);
@ -69,14 +72,14 @@ public class TestXSSFComment extends TestCase {
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);
@ -89,7 +92,7 @@ public class TestXSSFComment extends TestCase {
}
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);
@ -102,11 +105,20 @@ public class TestXSSFComment extends TestCase {
}
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());
}
}

View File

@ -18,14 +18,18 @@
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 {
@ -480,6 +484,51 @@ 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);

View File

@ -0,0 +1,84 @@
/* ====================================================================
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()));
}
}

View File

@ -1,39 +0,0 @@
/* ====================================================================
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));
}
}

View File

@ -20,7 +20,6 @@ package org.apache.poi.xssf.usermodel.helpers;
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 {