Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TestXSSFComment.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.jupiter.api.Assertions.assertEquals;
  18. import static org.junit.jupiter.api.Assertions.assertFalse;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;
  20. import static org.junit.jupiter.api.Assertions.assertNull;
  21. import static org.junit.jupiter.api.Assertions.assertSame;
  22. import static org.junit.jupiter.api.Assertions.assertThrows;
  23. import static org.junit.jupiter.api.Assertions.assertTrue;
  24. import java.io.IOException;
  25. import com.microsoft.schemas.vml.CTShape;
  26. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  27. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  28. import org.apache.poi.ss.usermodel.BaseTestCellComment;
  29. import org.apache.poi.ss.usermodel.Cell;
  30. import org.apache.poi.ss.usermodel.ClientAnchor;
  31. import org.apache.poi.ss.usermodel.Comment;
  32. import org.apache.poi.ss.usermodel.CreationHelper;
  33. import org.apache.poi.ss.usermodel.IndexedColors;
  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.jupiter.api.Test;
  45. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
  46. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
  47. public final class TestXSSFComment extends BaseTestCellComment {
  48. private static final String TEST_RICHTEXTSTRING = "test richtextstring";
  49. public TestXSSFComment() {
  50. super(XSSFITestDataProvider.instance);
  51. }
  52. /**
  53. * test properties of a newly constructed comment
  54. */
  55. @Test
  56. void constructor() {
  57. CommentsTable sheetComments = new CommentsTable();
  58. assertNotNull(sheetComments.getCTComments().getCommentList());
  59. assertNotNull(sheetComments.getCTComments().getAuthors());
  60. assertEquals(1, sheetComments.getCTComments().getAuthors().sizeOfAuthorArray());
  61. assertEquals(1, sheetComments.getNumberOfAuthors());
  62. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  63. CTShape vmlShape = CTShape.Factory.newInstance();
  64. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  65. assertNull(comment.getString());
  66. assertEquals(0, comment.getRow());
  67. assertEquals(0, comment.getColumn());
  68. assertEquals("", comment.getAuthor());
  69. assertFalse(comment.isVisible());
  70. }
  71. @Test
  72. void getSetCol() {
  73. CommentsTable sheetComments = new CommentsTable();
  74. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  75. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  76. CTShape vmlShape = vml.newCommentShape();
  77. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  78. comment.setColumn(1);
  79. assertEquals(1, comment.getColumn());
  80. assertEquals(1, new CellReference(ctComment.getRef()).getCol());
  81. assertEquals(1, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
  82. comment.setColumn(5);
  83. assertEquals(5, comment.getColumn());
  84. assertEquals(5, new CellReference(ctComment.getRef()).getCol());
  85. assertEquals(5, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
  86. }
  87. @Test
  88. void getSetRow() {
  89. CommentsTable sheetComments = new CommentsTable();
  90. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  91. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  92. CTShape vmlShape = vml.newCommentShape();
  93. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  94. comment.setRow(1);
  95. assertEquals(1, comment.getRow());
  96. assertEquals(1, new CellReference(ctComment.getRef()).getRow());
  97. assertEquals(1, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
  98. comment.setRow(5);
  99. assertEquals(5, comment.getRow());
  100. assertEquals(5, new CellReference(ctComment.getRef()).getRow());
  101. assertEquals(5, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
  102. }
  103. @Test
  104. void setString() {
  105. XSSFWorkbook wb = new XSSFWorkbook();
  106. XSSFSheet sh = wb.createSheet();
  107. XSSFComment comment = sh.createDrawingPatriarch().createCellComment(new XSSFClientAnchor());
  108. //passing HSSFRichTextString is incorrect
  109. IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
  110. () -> comment.setString(new HSSFRichTextString(TEST_RICHTEXTSTRING)));
  111. assertEquals("Only XSSFRichTextString argument is supported", e.getMessage());
  112. //simple string argument
  113. comment.setString(TEST_RICHTEXTSTRING);
  114. assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
  115. //if the text is already set, it should be overridden, not added twice!
  116. comment.setString(TEST_RICHTEXTSTRING);
  117. CTComment ctComment = comment.getCTComment();
  118. XmlObject[] obj = ctComment.selectPath(
  119. "declare namespace w='"+NS_SPREADSHEETML+"' .//w:text");
  120. assertEquals(1, obj.length);
  121. assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
  122. //sequential call of comment.getString() should return the same XSSFRichTextString object
  123. assertSame(comment.getString(), comment.getString());
  124. XSSFRichTextString richText = new XSSFRichTextString(TEST_RICHTEXTSTRING);
  125. XSSFFont font1 = wb.createFont();
  126. font1.setFontName("Tahoma");
  127. font1.setFontHeight(8.5);
  128. font1.setItalic(true);
  129. font1.setColor(IndexedColors.BLUE_GREY.getIndex());
  130. richText.applyFont(0, 5, font1);
  131. //check the low-level stuff
  132. comment.setString(richText);
  133. obj = ctComment.selectPath(
  134. "declare namespace w='"+NS_SPREADSHEETML+"' .//w:text");
  135. assertEquals(1, obj.length);
  136. assertSame(comment.getString(), richText);
  137. //check that the rich text is set in the comment
  138. CTRPrElt rPr = richText.getCTRst().getRArray(0).getRPr();
  139. assertTrue(rPr.getIArray(0).getVal());
  140. assertEquals(8.5, rPr.getSzArray(0).getVal(), 0);
  141. assertEquals(IndexedColors.BLUE_GREY.getIndex(), rPr.getColorArray(0).getIndexed());
  142. assertEquals("Tahoma", rPr.getRFontArray(0).getVal());
  143. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  144. }
  145. @Test
  146. void author() {
  147. CommentsTable sheetComments = new CommentsTable();
  148. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  149. assertEquals(1, sheetComments.getNumberOfAuthors());
  150. XSSFComment comment = new XSSFComment(sheetComments, ctComment, null);
  151. assertEquals("", comment.getAuthor());
  152. comment.setAuthor("Apache POI");
  153. assertEquals("Apache POI", comment.getAuthor());
  154. assertEquals(2, sheetComments.getNumberOfAuthors());
  155. comment.setAuthor("Apache POI");
  156. assertEquals(2, sheetComments.getNumberOfAuthors());
  157. comment.setAuthor("");
  158. assertEquals("", comment.getAuthor());
  159. assertEquals(2, sheetComments.getNumberOfAuthors());
  160. }
  161. @Test
  162. void testBug58175() throws IOException {
  163. try (SXSSFWorkbook wb = new SXSSFWorkbook()) {
  164. Sheet sheet = wb.createSheet();
  165. Row row = sheet.createRow(1);
  166. Cell cell = row.createCell(3);
  167. cell.setCellValue("F4");
  168. CreationHelper factory = wb.getCreationHelper();
  169. // When the comment box is visible, have it show in a 1x3 space
  170. ClientAnchor anchor = factory.createClientAnchor();
  171. anchor.setCol1(cell.getColumnIndex());
  172. anchor.setCol2(cell.getColumnIndex() + 1);
  173. anchor.setRow1(row.getRowNum());
  174. anchor.setRow2(row.getRowNum() + 3);
  175. XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
  176. // create comments and vmlDrawing parts if they don't exist
  177. CommentsTable comments = wb.getXSSFWorkbook()
  178. .getSheetAt(0).getCommentsTable(true);
  179. XSSFVMLDrawing vml = wb.getXSSFWorkbook()
  180. .getSheetAt(0).getVMLDrawing(true);
  181. CTShape vmlShape1 = vml.newCommentShape();
  182. if (ca.isSet()) {
  183. String position = ca.getCol1() + ", 0, " + ca.getRow1()
  184. + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2()
  185. + ", 0";
  186. vmlShape1.getClientDataArray(0).setAnchorArray(0, position);
  187. }
  188. // create the comment in two different ways and verify that there is no difference
  189. XSSFComment shape1 = new XSSFComment(comments, comments.newComment(CellAddress.A1), vmlShape1);
  190. shape1.setColumn(ca.getCol1());
  191. shape1.setRow(ca.getRow1());
  192. CTShape vmlShape2 = vml.newCommentShape();
  193. if (ca.isSet()) {
  194. String position = ca.getCol1() + ", 0, " + ca.getRow1()
  195. + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2()
  196. + ", 0";
  197. vmlShape2.getClientDataArray(0).setAnchorArray(0, position);
  198. }
  199. CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
  200. XSSFComment shape2 = new XSSFComment(comments, comments.newComment(ref), vmlShape2);
  201. assertEquals(shape1.getAuthor(), shape2.getAuthor());
  202. assertEquals(shape1.getClientAnchor(), shape2.getClientAnchor());
  203. assertEquals(shape1.getColumn(), shape2.getColumn());
  204. assertEquals(shape1.getRow(), shape2.getRow());
  205. assertEquals(shape1.getCTComment().toString(), shape2.getCTComment().toString());
  206. assertEquals(shape1.getCTComment().getRef(), shape2.getCTComment().getRef());
  207. /*CommentsTable table1 = shape1.getCommentsTable();
  208. CommentsTable table2 = shape2.getCommentsTable();
  209. assertEquals(table1.getCTComments().toString(), table2.getCTComments().toString());
  210. assertEquals(table1.getNumberOfComments(), table2.getNumberOfComments());
  211. assertEquals(table1.getRelations(), table2.getRelations());*/
  212. assertEquals(vmlShape1.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"), vmlShape2.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"),
  213. "The vmlShapes should have equal content afterwards");
  214. }
  215. }
  216. @Test
  217. void testBug55814() throws IOException {
  218. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("55814.xlsx")) {
  219. int oldsheetIndex = wb.getSheetIndex("example");
  220. Sheet oldsheet = wb.getSheetAt(oldsheetIndex);
  221. Comment comment = oldsheet.getRow(0).getCell(0).getCellComment();
  222. assertEquals("Comment Here\n", comment.getString().getString());
  223. Sheet newsheet = wb.cloneSheet(oldsheetIndex);
  224. wb.removeSheetAt(oldsheetIndex);
  225. //wb.write(new FileOutputStream("/tmp/outnocomment.xlsx"));
  226. comment = newsheet.getRow(0).getCell(0).getCellComment();
  227. assertNotNull(comment, "Should have a comment on A1 in the new sheet");
  228. assertEquals("Comment Here\n", comment.getString().getString());
  229. Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  230. assertNotNull(wbBack);
  231. wbBack.close();
  232. }
  233. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("55814.xlsx")) {
  234. int oldsheetIndex = wb.getSheetIndex("example");
  235. Sheet newsheet = wb.getSheetAt(oldsheetIndex);
  236. Comment comment = newsheet.getRow(0).getCell(0).getCellComment();
  237. assertEquals("Comment Here\n", comment.getString().getString());
  238. }
  239. }
  240. @Test
  241. void bug57838DeleteRowsWthCommentsBug() throws IOException {
  242. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57838.xlsx");
  243. Sheet sheet=wb.getSheetAt(0);
  244. Comment comment1 = sheet.getCellComment(new CellAddress(2, 1));
  245. assertNotNull(comment1);
  246. Comment comment2 = sheet.getCellComment(new CellAddress(2, 2));
  247. assertNotNull(comment2);
  248. Row row=sheet.getRow(2);
  249. assertNotNull(row);
  250. sheet.removeRow(row); // Remove row from index 2
  251. row=sheet.getRow(2);
  252. assertNull(row); // Row is null since we deleted it.
  253. comment1 = sheet.getCellComment(new CellAddress(2, 1));
  254. assertNull(comment1); // comment should be null but will fail due to bug
  255. comment2 = sheet.getCellComment(new CellAddress(2, 2));
  256. assertNull(comment2); // comment should be null but will fail due to bug
  257. wb.close();
  258. }
  259. @Test
  260. void bug59388CommentVisible() throws IOException {
  261. try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
  262. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("59388.xlsx")) {
  263. Sheet sheet = wb.getSheetAt(0);
  264. Cell a1 = sheet.getRow(0).getCell(0);
  265. Cell d1 = sheet.getRow(0).getCell(3);
  266. Comment commentA1 = a1.getCellComment();
  267. Comment commentD1 = d1.getCellComment();
  268. // assert original visibility
  269. assertTrue(commentA1.isVisible());
  270. assertFalse(commentD1.isVisible());
  271. commentA1.setVisible(false);
  272. commentD1.setVisible(true);
  273. // assert after changing
  274. assertFalse(commentA1.isVisible());
  275. assertTrue(commentD1.isVisible());
  276. // check result
  277. wb.write(bos);
  278. try (Workbook wb2 = new XSSFWorkbook(bos.toInputStream())) {
  279. Sheet sheetWb2 = wb2.getSheetAt(0);
  280. Cell a1Wb2 = sheetWb2.getRow(0).getCell(0);
  281. Cell d1Wb2 = sheetWb2.getRow(0).getCell(3);
  282. assertFalse(a1Wb2.getCellComment().isVisible());
  283. assertTrue(d1Wb2.getCellComment().isVisible());
  284. }
  285. }
  286. }
  287. }
  288. }