You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSSFComment.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 java.util.Map;
  26. import java.util.UUID;
  27. import com.microsoft.schemas.vml.CTShape;
  28. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  29. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  30. import org.apache.poi.ss.usermodel.*;
  31. import org.apache.poi.ss.util.CellAddress;
  32. import org.apache.poi.ss.util.CellReference;
  33. import org.apache.poi.xssf.XSSFITestDataProvider;
  34. import org.apache.poi.xssf.XSSFTestDataSamples;
  35. import org.apache.poi.xssf.model.Comments;
  36. import org.apache.poi.xssf.model.CommentsTable;
  37. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  38. import org.apache.xmlbeans.XmlObject;
  39. import org.junit.jupiter.api.Test;
  40. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
  41. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
  42. public final class TestXSSFComment extends BaseTestCellComment {
  43. private static final String TEST_RICHTEXTSTRING = "test richtextstring";
  44. public TestXSSFComment() {
  45. super(XSSFITestDataProvider.instance);
  46. }
  47. /**
  48. * test properties of a newly constructed comment
  49. */
  50. @Test
  51. void constructor() {
  52. CommentsTable sheetComments = new CommentsTable();
  53. assertNotNull(sheetComments.getCTComments().getCommentList());
  54. assertNotNull(sheetComments.getCTComments().getAuthors());
  55. assertEquals(1, sheetComments.getCTComments().getAuthors().sizeOfAuthorArray());
  56. assertEquals(1, sheetComments.getNumberOfAuthors());
  57. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  58. CTShape vmlShape = CTShape.Factory.newInstance();
  59. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  60. assertNull(comment.getString());
  61. assertEquals(0, comment.getRow());
  62. assertEquals(0, comment.getColumn());
  63. assertEquals("", comment.getAuthor());
  64. assertFalse(comment.isVisible());
  65. }
  66. @Test
  67. void getSetCol() {
  68. CommentsTable sheetComments = new CommentsTable();
  69. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  70. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  71. CTShape vmlShape = vml.newCommentShape();
  72. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  73. comment.setColumn(1);
  74. assertEquals(1, comment.getColumn());
  75. assertEquals(1, new CellReference(ctComment.getRef()).getCol());
  76. assertEquals(1, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
  77. comment.setColumn(5);
  78. assertEquals(5, comment.getColumn());
  79. assertEquals(5, new CellReference(ctComment.getRef()).getCol());
  80. assertEquals(5, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
  81. }
  82. @Test
  83. void getSetRow() {
  84. CommentsTable sheetComments = new CommentsTable();
  85. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  86. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  87. CTShape vmlShape = vml.newCommentShape();
  88. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  89. comment.setRow(1);
  90. assertEquals(1, comment.getRow());
  91. assertEquals(1, new CellReference(ctComment.getRef()).getRow());
  92. assertEquals(1, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
  93. comment.setRow(5);
  94. assertEquals(5, comment.getRow());
  95. assertEquals(5, new CellReference(ctComment.getRef()).getRow());
  96. assertEquals(5, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
  97. }
  98. @Test
  99. void setString() {
  100. XSSFWorkbook wb = new XSSFWorkbook();
  101. XSSFSheet sh = wb.createSheet();
  102. XSSFComment comment = sh.createDrawingPatriarch().createCellComment(new XSSFClientAnchor());
  103. //passing HSSFRichTextString is incorrect
  104. IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
  105. () -> comment.setString(new HSSFRichTextString(TEST_RICHTEXTSTRING)));
  106. assertEquals("Only XSSFRichTextString argument is supported", e.getMessage());
  107. //simple string argument
  108. comment.setString(TEST_RICHTEXTSTRING);
  109. assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
  110. //if the text is already set, it should be overridden, not added twice!
  111. comment.setString(TEST_RICHTEXTSTRING);
  112. CTComment ctComment = comment.getCTComment();
  113. XmlObject[] obj = ctComment.selectPath(
  114. "declare namespace w='"+NS_SPREADSHEETML+"' .//w:text");
  115. assertEquals(1, obj.length);
  116. assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
  117. //sequential call of comment.getString() should return the same XSSFRichTextString object
  118. assertSame(comment.getString(), comment.getString());
  119. XSSFRichTextString richText = new XSSFRichTextString(TEST_RICHTEXTSTRING);
  120. XSSFFont font1 = wb.createFont();
  121. font1.setFontName("Tahoma");
  122. font1.setFontHeight(8.5);
  123. font1.setItalic(true);
  124. font1.setColor(IndexedColors.BLUE_GREY.getIndex());
  125. richText.applyFont(0, 5, font1);
  126. //check the low-level stuff
  127. comment.setString(richText);
  128. obj = ctComment.selectPath(
  129. "declare namespace w='"+NS_SPREADSHEETML+"' .//w:text");
  130. assertEquals(1, obj.length);
  131. assertSame(comment.getString(), richText);
  132. //check that the rich text is set in the comment
  133. CTRPrElt rPr = richText.getCTRst().getRArray(0).getRPr();
  134. assertTrue(rPr.getIArray(0).getVal());
  135. assertEquals(8.5, rPr.getSzArray(0).getVal(), 0);
  136. assertEquals(IndexedColors.BLUE_GREY.getIndex(), rPr.getColorArray(0).getIndexed());
  137. assertEquals("Tahoma", rPr.getRFontArray(0).getVal());
  138. assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
  139. }
  140. @Test
  141. void author() {
  142. CommentsTable sheetComments = new CommentsTable();
  143. CTComment ctComment = sheetComments.newComment(CellAddress.A1);
  144. assertEquals(1, sheetComments.getNumberOfAuthors());
  145. XSSFComment comment = new XSSFComment(sheetComments, ctComment, null);
  146. assertEquals("", comment.getAuthor());
  147. comment.setAuthor("Apache POI");
  148. assertEquals("Apache POI", comment.getAuthor());
  149. assertEquals(2, sheetComments.getNumberOfAuthors());
  150. comment.setAuthor("Apache POI");
  151. assertEquals(2, sheetComments.getNumberOfAuthors());
  152. comment.setAuthor("");
  153. assertEquals("", comment.getAuthor());
  154. assertEquals(2, sheetComments.getNumberOfAuthors());
  155. }
  156. @Test
  157. void testBug58175() throws IOException {
  158. try (SXSSFWorkbook wb = new SXSSFWorkbook()) {
  159. Sheet sheet = wb.createSheet();
  160. Row row = sheet.createRow(1);
  161. Cell cell = row.createCell(3);
  162. cell.setCellValue("F4");
  163. CreationHelper factory = wb.getCreationHelper();
  164. // When the comment box is visible, have it show in a 1x3 space
  165. ClientAnchor anchor = factory.createClientAnchor();
  166. anchor.setCol1(cell.getColumnIndex());
  167. anchor.setCol2(cell.getColumnIndex() + 1);
  168. anchor.setRow1(row.getRowNum());
  169. anchor.setRow2(row.getRowNum() + 3);
  170. XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
  171. // create comments and vmlDrawing parts if they don't exist
  172. Comments comments = wb.getXSSFWorkbook()
  173. .getSheetAt(0).getCommentsTable(true);
  174. XSSFVMLDrawing vml = wb.getXSSFWorkbook()
  175. .getSheetAt(0).getVMLDrawing(true);
  176. CTShape vmlShape1 = vml.newCommentShape();
  177. if (ca.isSet()) {
  178. String position = ca.getCol1() + ", 0, " + ca.getRow1()
  179. + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2()
  180. + ", 0";
  181. vmlShape1.getClientDataArray(0).setAnchorArray(0, position);
  182. }
  183. // create the comment in two different ways and verify that there is no difference
  184. CommentsTable commentsTable = (CommentsTable)comments;
  185. XSSFComment shape1 = new XSSFComment(comments, commentsTable.newComment(CellAddress.A1), vmlShape1);
  186. shape1.setColumn(ca.getCol1());
  187. shape1.setRow(ca.getRow1());
  188. CTShape vmlShape2 = vml.newCommentShape();
  189. if (ca.isSet()) {
  190. String position = ca.getCol1() + ", 0, " + ca.getRow1()
  191. + ", 0, " + ca.getCol2() + ", 0, " + ca.getRow2()
  192. + ", 0";
  193. vmlShape2.getClientDataArray(0).setAnchorArray(0, position);
  194. }
  195. CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
  196. XSSFComment shape2 = new XSSFComment(comments, commentsTable.newComment(ref), vmlShape2);
  197. assertEquals(shape1.getAuthor(), shape2.getAuthor());
  198. assertEquals(shape1.getClientAnchor(), shape2.getClientAnchor());
  199. assertEquals(shape1.getColumn(), shape2.getColumn());
  200. assertEquals(shape1.getRow(), shape2.getRow());
  201. assertEquals(shape1.getCTComment().toString(), shape2.getCTComment().toString());
  202. assertEquals(shape1.getCTComment().getRef(), shape2.getCTComment().getRef());
  203. /*CommentsTable table1 = shape1.getCommentsTable();
  204. CommentsTable table2 = shape2.getCommentsTable();
  205. assertEquals(table1.getCTComments().toString(), table2.getCTComments().toString());
  206. assertEquals(table1.getNumberOfComments(), table2.getNumberOfComments());
  207. assertEquals(table1.getRelations(), table2.getRelations());*/
  208. assertEquals(vmlShape1.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"), vmlShape2.toString().replaceAll("_x0000_s\\d+", "_x0000_s0000"),
  209. "The vmlShapes should have equal content afterwards");
  210. }
  211. }
  212. @Test
  213. void testBug55814() throws IOException {
  214. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("55814.xlsx")) {
  215. int oldsheetIndex = wb.getSheetIndex("example");
  216. Sheet oldsheet = wb.getSheetAt(oldsheetIndex);
  217. Comment comment = oldsheet.getRow(0).getCell(0).getCellComment();
  218. assertEquals("Comment Here\n", comment.getString().getString());
  219. Sheet newsheet = wb.cloneSheet(oldsheetIndex);
  220. wb.removeSheetAt(oldsheetIndex);
  221. //wb.write(new FileOutputStream("/tmp/outnocomment.xlsx"));
  222. comment = newsheet.getRow(0).getCell(0).getCellComment();
  223. assertNotNull(comment, "Should have a comment on A1 in the new sheet");
  224. assertEquals("Comment Here\n", comment.getString().getString());
  225. Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  226. assertNotNull(wbBack);
  227. wbBack.close();
  228. }
  229. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("55814.xlsx")) {
  230. int oldsheetIndex = wb.getSheetIndex("example");
  231. Sheet newsheet = wb.getSheetAt(oldsheetIndex);
  232. Comment comment = newsheet.getRow(0).getCell(0).getCellComment();
  233. assertEquals("Comment Here\n", comment.getString().getString());
  234. }
  235. }
  236. @Test
  237. void bug57838DeleteRowsWthCommentsBug() throws IOException {
  238. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57838.xlsx");
  239. Sheet sheet=wb.getSheetAt(0);
  240. Comment comment1 = sheet.getCellComment(new CellAddress(2, 1));
  241. assertNotNull(comment1);
  242. Comment comment2 = sheet.getCellComment(new CellAddress(2, 2));
  243. assertNotNull(comment2);
  244. Row row=sheet.getRow(2);
  245. assertNotNull(row);
  246. sheet.removeRow(row); // Remove row from index 2
  247. row=sheet.getRow(2);
  248. assertNull(row); // Row is null since we deleted it.
  249. comment1 = sheet.getCellComment(new CellAddress(2, 1));
  250. assertNull(comment1); // comment should be null but will fail due to bug
  251. comment2 = sheet.getCellComment(new CellAddress(2, 2));
  252. assertNull(comment2); // comment should be null but will fail due to bug
  253. wb.close();
  254. }
  255. @Test
  256. void bug59388CommentVisible() throws IOException {
  257. try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
  258. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("59388.xlsx")) {
  259. Sheet sheet = wb.getSheetAt(0);
  260. Cell a1 = sheet.getRow(0).getCell(0);
  261. Cell d1 = sheet.getRow(0).getCell(3);
  262. Comment commentA1 = a1.getCellComment();
  263. Comment commentD1 = d1.getCellComment();
  264. // assert original visibility
  265. assertTrue(commentA1.isVisible());
  266. assertFalse(commentD1.isVisible());
  267. commentA1.setVisible(false);
  268. commentD1.setVisible(true);
  269. // assert after changing
  270. assertFalse(commentA1.isVisible());
  271. assertTrue(commentD1.isVisible());
  272. // check result
  273. wb.write(bos);
  274. try (Workbook wb2 = new XSSFWorkbook(bos.toInputStream())) {
  275. Sheet sheetWb2 = wb2.getSheetAt(0);
  276. Cell a1Wb2 = sheetWb2.getRow(0).getCell(0);
  277. Cell d1Wb2 = sheetWb2.getRow(0).getCell(3);
  278. assertFalse(a1Wb2.getCellComment().isVisible());
  279. assertTrue(d1Wb2.getCellComment().isVisible());
  280. }
  281. }
  282. }
  283. }
  284. @Test
  285. void testMoveComment() throws Exception {
  286. _testMove(new XSSFWorkbook());
  287. }
  288. @Test
  289. void testMoveCommentSXSSF() throws Exception {
  290. SXSSFWorkbook workbook = new SXSSFWorkbook();
  291. try {
  292. _testMove(workbook);
  293. } finally {
  294. workbook.dispose();
  295. }
  296. }
  297. @Test
  298. void testMoveCommentCopy() throws Exception {
  299. _testMoveCopy(new XSSFWorkbook());
  300. }
  301. @Test
  302. void testMoveCommentCopySXSSF() throws Exception {
  303. SXSSFWorkbook workbook = new SXSSFWorkbook();
  304. try {
  305. _testMoveCopy(workbook);
  306. } finally {
  307. workbook.dispose();
  308. }
  309. }
  310. @Test
  311. void testMoveCommentIsSaved() throws Exception {
  312. _testMoveIsSaved(new XSSFWorkbook());
  313. }
  314. @Test
  315. void testMoveCommentIsSavedSXSSF() throws Exception {
  316. SXSSFWorkbook workbook = new SXSSFWorkbook();
  317. try {
  318. _testMoveIsSaved(workbook);
  319. } finally {
  320. workbook.dispose();
  321. }
  322. }
  323. @Test
  324. void testModifiedCommentIsSaved() throws Exception {
  325. _testModificationIsSaved(new XSSFWorkbook());
  326. }
  327. @Test
  328. void testModifiedCommentIsSavedSXSSF() throws Exception {
  329. SXSSFWorkbook workbook = new SXSSFWorkbook();
  330. try {
  331. _testModificationIsSaved(workbook);
  332. } finally {
  333. workbook.dispose();
  334. }
  335. }
  336. private void _testMove(Workbook workbook) throws Exception {
  337. CommentsTable commentsTable = new CommentsTable();
  338. try {
  339. CreationHelper factory = workbook.getCreationHelper();
  340. Sheet sheet = workbook.createSheet();
  341. commentsTable.setSheet(sheet);
  342. Row row = sheet.createRow(0);
  343. Cell cell = row.createCell(0);
  344. cell.setCellValue("CellA1");
  345. ClientAnchor anchor = factory.createClientAnchor();
  346. anchor.setCol1(0);
  347. anchor.setCol2(1);
  348. anchor.setRow1(row.getRowNum());
  349. anchor.setRow2(row.getRowNum());
  350. XSSFComment comment = commentsTable.createNewComment(anchor);
  351. String uniqueText = UUID.randomUUID().toString();
  352. comment.setString(uniqueText);
  353. comment.setAuthor("author" + uniqueText);
  354. XSSFComment comment1 = commentsTable.findCellComment(new CellAddress("A1"));
  355. assertEquals(comment.getString().getString(), comment1.getString().getString());
  356. comment.setAddress(1, 1);
  357. assertNull(commentsTable.findCellComment(new CellAddress("A1")),
  358. "no longer a comment on cell A1?");
  359. XSSFComment comment2 = commentsTable.findCellComment(new CellAddress("B2"));
  360. assertEquals(comment.getString().getString(), comment2.getString().getString());
  361. } finally {
  362. workbook.close();
  363. }
  364. }
  365. private void _testMoveCopy(Workbook workbook) throws Exception {
  366. CommentsTable commentsTable = new CommentsTable();
  367. try {
  368. CreationHelper factory = workbook.getCreationHelper();
  369. Sheet sheet = workbook.createSheet();
  370. commentsTable.setSheet(sheet);
  371. Row row = sheet.createRow(0);
  372. Cell cell = row.createCell(0);
  373. cell.setCellValue("CellA1");
  374. ClientAnchor anchor = factory.createClientAnchor();
  375. anchor.setCol1(0);
  376. anchor.setCol2(1);
  377. anchor.setRow1(row.getRowNum());
  378. anchor.setRow2(row.getRowNum());
  379. XSSFComment comment = commentsTable.createNewComment(anchor);
  380. String uniqueText = UUID.randomUUID().toString();
  381. comment.setString(uniqueText);
  382. comment.setAuthor("author" + uniqueText);
  383. XSSFComment comment1 = commentsTable.findCellComment(new CellAddress("A1"));
  384. assertEquals(comment.getString().getString(), comment1.getString().getString());
  385. //like testMoveComment but moves the copy of the comment (comment1) instead
  386. comment1.setAddress(1, 1);
  387. assertNull(commentsTable.findCellComment(new CellAddress("A1")),
  388. "no longer a comment on cell A1?");
  389. XSSFComment comment2 = commentsTable.findCellComment(new CellAddress("B2"));
  390. assertEquals(comment.getString().getString(), comment2.getString().getString());
  391. } finally {
  392. workbook.close();
  393. }
  394. }
  395. private void _testMoveIsSaved(Workbook workbook) throws Exception {
  396. try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
  397. CreationHelper factory = workbook.getCreationHelper();
  398. Sheet sheet = workbook.createSheet();
  399. Row row = sheet.createRow(0);
  400. Cell cell = row.createCell(0);
  401. cell.setCellValue("CellA1");
  402. ClientAnchor anchor = factory.createClientAnchor();
  403. anchor.setCol1(0);
  404. anchor.setCol2(1);
  405. anchor.setRow1(row.getRowNum());
  406. anchor.setRow2(row.getRowNum());
  407. Drawing drawing = sheet.createDrawingPatriarch();
  408. Comment comment = drawing.createCellComment(anchor);
  409. String uniqueText = UUID.randomUUID().toString();
  410. comment.setString(factory.createRichTextString(uniqueText));
  411. comment.setAuthor("author" + uniqueText);
  412. Comment comment1 = sheet.getCellComment(new CellAddress("A1"));
  413. assertEquals(comment.getString().getString(), comment1.getString().getString());
  414. comment.setAddress(1, 1);
  415. Comment comment2 = sheet.getCellComment(new CellAddress("B2"));
  416. assertEquals(comment.getString().getString(), comment2.getString().getString());
  417. assertNull( sheet.getCellComment(new CellAddress("A1")), "comment still found on A1?");
  418. workbook.write(bos);
  419. try (XSSFWorkbook workbook2 = new XSSFWorkbook(bos.toInputStream())) {
  420. XSSFSheet wb2Sheet = workbook2.getSheetAt(0);
  421. Map<CellAddress, XSSFComment> cellComments = wb2Sheet.getCellComments();
  422. assertEquals(1, cellComments.size());
  423. CellAddress address0 = (CellAddress) cellComments.keySet().toArray()[0];
  424. assertEquals("B2", address0.formatAsString());
  425. }
  426. }
  427. }
  428. private void _testModificationIsSaved(Workbook workbook) throws Exception {
  429. try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
  430. CreationHelper factory = workbook.getCreationHelper();
  431. Sheet sheet = workbook.createSheet();
  432. Row row = sheet.createRow(0);
  433. Cell cell = row.createCell(0);
  434. cell.setCellValue("CellA1");
  435. ClientAnchor anchor = factory.createClientAnchor();
  436. anchor.setCol1(0);
  437. anchor.setCol2(1);
  438. anchor.setRow1(row.getRowNum());
  439. anchor.setRow2(row.getRowNum());
  440. Drawing drawing = sheet.createDrawingPatriarch();
  441. Comment comment = drawing.createCellComment(anchor);
  442. comment.setString(factory.createRichTextString("initText"));
  443. comment.setAuthor("initAuthor");
  444. String uniqueText = UUID.randomUUID().toString();
  445. comment.setString(factory.createRichTextString(uniqueText));
  446. comment.setAuthor("author" + uniqueText);
  447. workbook.write(bos);
  448. try (XSSFWorkbook workbook2 = new XSSFWorkbook(bos.toInputStream())) {
  449. XSSFSheet wb2Sheet = workbook2.getSheetAt(0);
  450. Map<CellAddress, XSSFComment> cellComments = wb2Sheet.getCellComments();
  451. assertEquals(1, cellComments.size());
  452. CellAddress address0 = (CellAddress) cellComments.keySet().toArray()[0];
  453. assertEquals("A1", address0.formatAsString());
  454. XSSFComment savedComment = cellComments.get(address0);
  455. assertEquals(comment.getString().getString(), savedComment.getString().getString());
  456. assertEquals(comment.getAuthor(), savedComment.getAuthor());
  457. }
  458. } finally {
  459. workbook.close();
  460. }
  461. }
  462. }