選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TestXSSFComment.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import static org.apache.poi.xssf.usermodel.XSSFRelation.NS_SPREADSHEETML;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertSame;
  21. import static org.junit.Assert.fail;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.OutputStream;
  25. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  26. import org.apache.poi.ss.usermodel.BaseTestCellComment;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.usermodel.ClientAnchor;
  29. import org.apache.poi.ss.usermodel.Comment;
  30. import org.apache.poi.ss.usermodel.CreationHelper;
  31. import org.apache.poi.ss.usermodel.Drawing;
  32. import org.apache.poi.ss.usermodel.IndexedColors;
  33. import org.apache.poi.ss.usermodel.RichTextString;
  34. import org.apache.poi.ss.usermodel.Row;
  35. import org.apache.poi.ss.usermodel.Sheet;
  36. import org.apache.poi.ss.usermodel.Workbook;
  37. import org.apache.poi.ss.util.CellAddress;
  38. import org.apache.poi.ss.util.CellReference;
  39. import org.apache.poi.xssf.XSSFITestDataProvider;
  40. import org.apache.poi.xssf.XSSFTestDataSamples;
  41. import org.apache.poi.xssf.model.CommentsTable;
  42. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  43. import org.apache.xmlbeans.XmlObject;
  44. import org.junit.Ignore;
  45. import org.junit.Test;
  46. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
  47. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
  48. import com.microsoft.schemas.vml.CTShape;
  49. public final class TestXSSFComment extends BaseTestCellComment {
  50. private static final String TEST_RICHTEXTSTRING = "test richtextstring";
  51. public TestXSSFComment() {
  52. super(XSSFITestDataProvider.instance);
  53. }
  54. /**
  55. * test properties of a newly constructed comment
  56. */
  57. @Test
  58. public void constructor() {
  59. CommentsTable sheetComments = new CommentsTable();
  60. assertNotNull(sheetComments.getCTComments().getCommentList());
  61. assertNotNull(sheetComments.getCTComments().getAuthors());
  62. assertEquals(1, sheetComments.getCTComments().getAuthors().sizeOfAuthorArray());
  63. assertEquals(1, sheetComments.getNumberOfAuthors());
  64. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  65. CTShape vmlShape = CTShape.Factory.newInstance();
  66. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  67. assertEquals(null, comment.getString());
  68. assertEquals(0, comment.getRow());
  69. assertEquals(0, comment.getColumn());
  70. assertEquals("", comment.getAuthor());
  71. assertEquals(false, comment.isVisible());
  72. }
  73. @Test
  74. public void getSetCol() {
  75. CommentsTable sheetComments = new CommentsTable();
  76. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  77. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  78. CTShape vmlShape = vml.newCommentShape();
  79. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  80. comment.setColumn(1);
  81. assertEquals(1, comment.getColumn());
  82. assertEquals(1, new CellReference(ctComment.getRef()).getCol());
  83. assertEquals(1, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
  84. comment.setColumn(5);
  85. assertEquals(5, comment.getColumn());
  86. assertEquals(5, new CellReference(ctComment.getRef()).getCol());
  87. assertEquals(5, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
  88. }
  89. @Test
  90. public void getSetRow() {
  91. CommentsTable sheetComments = new CommentsTable();
  92. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  93. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  94. CTShape vmlShape = vml.newCommentShape();
  95. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  96. comment.setRow(1);
  97. assertEquals(1, comment.getRow());
  98. assertEquals(1, new CellReference(ctComment.getRef()).getRow());
  99. assertEquals(1, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
  100. comment.setRow(5);
  101. assertEquals(5, comment.getRow());
  102. assertEquals(5, new CellReference(ctComment.getRef()).getRow());
  103. assertEquals(5, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
  104. }
  105. @Test
  106. public void setString() {
  107. XSSFWorkbook wb = new XSSFWorkbook();
  108. XSSFSheet sh = wb.createSheet();
  109. XSSFComment comment = sh.createDrawingPatriarch().createCellComment(new XSSFClientAnchor());
  110. //passing HSSFRichTextString is incorrect
  111. try {
  112. comment.setString(new HSSFRichTextString(TEST_RICHTEXTSTRING));
  113. fail("expected exception");
  114. } catch (IllegalArgumentException e){
  115. assertEquals("Only XSSFRichTextString argument is supported", e.getMessage());
  116. }
  117. //simple string argument
  118. comment.setString(TEST_RICHTEXTSTRING);
  119. assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
  120. //if the text is already set, it should be overridden, not added twice!
  121. comment.setString(TEST_RICHTEXTSTRING);
  122. CTComment ctComment = comment.getCTComment();
  123. XmlObject[] obj = ctComment.selectPath(
  124. "declare namespace w='"+NS_SPREADSHEETML+"' .//w:text");
  125. assertEquals(1, obj.length);
  126. assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
  127. //sequential call of comment.getString() should return the same XSSFRichTextString object
  128. assertSame(comment.getString(), comment.getString());
  129. XSSFRichTextString richText = new XSSFRichTextString(TEST_RICHTEXTSTRING);
  130. XSSFFont font1 = wb.createFont();
  131. font1.setFontName("Tahoma");
  132. font1.setFontHeight(8.5);
  133. font1.setItalic(true);
  134. font1.setColor(IndexedColors.BLUE_GREY.getIndex());
  135. richText.applyFont(0, 5, font1);
  136. //check the low-level stuff
  137. comment.setString(richText);
  138. obj = ctComment.selectPath(
  139. "declare namespace w='"+NS_SPREADSHEETML+"' .//w:text");
  140. assertEquals(1, obj.length);
  141. assertSame(comment.getString(), richText);
  142. //check that the rich text is set in the comment
  143. CTRPrElt rPr = richText.getCTRst().getRArray(0).getRPr();
  144. assertEquals(true, rPr.getIArray(0).getVal());
  145. assertEquals(8.5, rPr.getSzArray(0).getVal(), 0);
  146. assertEquals(IndexedColors.BLUE_GREY.getIndex(), rPr.getColorArray(0).getIndexed());
  147. assertEquals("Tahoma", rPr.getRFontArray(0).getVal());
  148. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  149. }
  150. @Test
  151. public void author() {
  152. CommentsTable sheetComments = new CommentsTable();
  153. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  154. assertEquals(1, sheetComments.getNumberOfAuthors());
  155. XSSFComment comment = new XSSFComment(sheetComments, ctComment, null);
  156. assertEquals("", comment.getAuthor());
  157. comment.setAuthor("Apache POI");
  158. assertEquals("Apache POI", comment.getAuthor());
  159. assertEquals(2, sheetComments.getNumberOfAuthors());
  160. comment.setAuthor("Apache POI");
  161. assertEquals(2, sheetComments.getNumberOfAuthors());
  162. comment.setAuthor("");
  163. assertEquals("", comment.getAuthor());
  164. assertEquals(2, sheetComments.getNumberOfAuthors());
  165. }
  166. @Test
  167. public void testBug58175() throws IOException {
  168. Workbook wb = new SXSSFWorkbook();
  169. try {
  170. Sheet sheet = wb.createSheet();
  171. Row row = sheet.createRow(1);
  172. Cell cell = row.createCell(3);
  173. cell.setCellValue("F4");
  174. CreationHelper factory = wb.getCreationHelper();
  175. // When the comment box is visible, have it show in a 1x3 space
  176. ClientAnchor anchor = factory.createClientAnchor();
  177. anchor.setCol1(cell.getColumnIndex());
  178. anchor.setCol2(cell.getColumnIndex() + 1);
  179. anchor.setRow1(row.getRowNum());
  180. anchor.setRow2(row.getRowNum() + 3);
  181. XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
  182. // create comments and vmlDrawing parts if they don't exist
  183. CommentsTable comments = ((SXSSFWorkbook) wb).getXSSFWorkbook()
  184. .getSheetAt(0).getCommentsTable(true);
  185. XSSFVMLDrawing vml = ((SXSSFWorkbook) wb).getXSSFWorkbook()
  186. .getSheetAt(0).getVMLDrawing(true);
  187. CTShape vmlShape1 = vml.newCommentShape();
  188. if (ca.isSet()) {
  189. String position = ca.getCol1() + ", 0, " + ca.getRow1()
  190. + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2()
  191. + ", 0";
  192. vmlShape1.getClientDataArray(0).setAnchorArray(0, position);
  193. }
  194. // create the comment in two different ways and verify that there is no difference
  195. XSSFComment shape1 = new XSSFComment(comments, comments.newComment(CellAddress.A1), vmlShape1);
  196. shape1.setColumn(ca.getCol1());
  197. shape1.setRow(ca.getRow1());
  198. CTShape vmlShape2 = vml.newCommentShape();
  199. if (ca.isSet()) {
  200. String position = ca.getCol1() + ", 0, " + ca.getRow1()
  201. + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2()
  202. + ", 0";
  203. vmlShape2.getClientDataArray(0).setAnchorArray(0, position);
  204. }
  205. CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
  206. XSSFComment shape2 = new XSSFComment(comments, comments.newComment(ref), vmlShape2);
  207. assertEquals(shape1.getAuthor(), shape2.getAuthor());
  208. assertEquals(shape1.getClientAnchor(), shape2.getClientAnchor());
  209. assertEquals(shape1.getColumn(), shape2.getColumn());
  210. assertEquals(shape1.getRow(), shape2.getRow());
  211. assertEquals(shape1.getCTComment().toString(), shape2.getCTComment().toString());
  212. assertEquals(shape1.getCTComment().getRef(), shape2.getCTComment().getRef());
  213. /*CommentsTable table1 = shape1.getCommentsTable();
  214. CommentsTable table2 = shape2.getCommentsTable();
  215. assertEquals(table1.getCTComments().toString(), table2.getCTComments().toString());
  216. assertEquals(table1.getNumberOfComments(), table2.getNumberOfComments());
  217. assertEquals(table1.getRelations(), table2.getRelations());*/
  218. assertEquals("The vmlShapes should have equal content afterwards",
  219. vmlShape1.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"), vmlShape2.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"));
  220. } finally {
  221. wb.close();
  222. }
  223. }
  224. @Ignore("Used for manual testing with opening the resulting Workbook in Excel")
  225. @Test
  226. public void testBug58175a() throws IOException {
  227. Workbook wb = new SXSSFWorkbook();
  228. try {
  229. Sheet sheet = wb.createSheet();
  230. Row row = sheet.createRow(1);
  231. Cell cell = row.createCell(3);
  232. cell.setCellValue("F4");
  233. Drawing<?> drawing = sheet.createDrawingPatriarch();
  234. CreationHelper factory = wb.getCreationHelper();
  235. // When the comment box is visible, have it show in a 1x3 space
  236. ClientAnchor anchor = factory.createClientAnchor();
  237. anchor.setCol1(cell.getColumnIndex());
  238. anchor.setCol2(cell.getColumnIndex() + 1);
  239. anchor.setRow1(row.getRowNum());
  240. anchor.setRow2(row.getRowNum() + 3);
  241. // Create the comment and set the text+author
  242. Comment comment = drawing.createCellComment(anchor);
  243. RichTextString str = factory.createRichTextString("Hello, World!");
  244. comment.setString(str);
  245. comment.setAuthor("Apache POI");
  246. /* fixed the problem as well
  247. * comment.setColumn(cell.getColumnIndex());
  248. * comment.setRow(cell.getRowIndex());
  249. */
  250. // Assign the comment to the cell
  251. cell.setCellComment(comment);
  252. OutputStream out = new FileOutputStream("C:\\temp\\58175.xlsx");
  253. try {
  254. wb.write(out);
  255. } finally {
  256. out.close();
  257. }
  258. } finally {
  259. wb.close();
  260. }
  261. }
  262. @Test
  263. public void testBug55814() throws IOException {
  264. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("55814.xlsx")) {
  265. int oldsheetIndex = wb.getSheetIndex("example");
  266. Sheet oldsheet = wb.getSheetAt(oldsheetIndex);
  267. Comment comment = oldsheet.getRow(0).getCell(0).getCellComment();
  268. assertEquals("Comment Here\n", comment.getString().getString());
  269. Sheet newsheet = wb.cloneSheet(oldsheetIndex);
  270. wb.removeSheetAt(oldsheetIndex);
  271. //wb.write(new FileOutputStream("/tmp/outnocomment.xlsx"));
  272. comment = newsheet.getRow(0).getCell(0).getCellComment();
  273. assertNotNull("Should have a comment on A1 in the new sheet", comment);
  274. assertEquals("Comment Here\n", comment.getString().getString());
  275. Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  276. assertNotNull(wbBack);
  277. wbBack.close();
  278. }
  279. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("55814.xlsx")) {
  280. int oldsheetIndex = wb.getSheetIndex("example");
  281. Sheet newsheet = wb.getSheetAt(oldsheetIndex);
  282. Comment comment = newsheet.getRow(0).getCell(0).getCellComment();
  283. assertEquals("Comment Here\n", comment.getString().getString());
  284. }
  285. }
  286. @Test
  287. public void bug57838DeleteRowsWthCommentsBug() throws IOException {
  288. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57838.xlsx");
  289. Sheet sheet=wb.getSheetAt(0);
  290. Comment comment1 = sheet.getCellComment(new CellAddress(2, 1));
  291. assertNotNull(comment1);
  292. Comment comment2 = sheet.getCellComment(new CellAddress(2, 2));
  293. assertNotNull(comment2);
  294. Row row=sheet.getRow(2);
  295. assertNotNull(row);
  296. sheet.removeRow(row); // Remove row from index 2
  297. row=sheet.getRow(2);
  298. assertNull(row); // Row is null since we deleted it.
  299. comment1 = sheet.getCellComment(new CellAddress(2, 1));
  300. assertNull(comment1); // comment should be null but will fail due to bug
  301. comment2 = sheet.getCellComment(new CellAddress(2, 2));
  302. assertNull(comment2); // comment should be null but will fail due to bug
  303. wb.close();
  304. }
  305. }