aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-10-18 10:48:04 +0000
committerPJ Fanning <fanningpj@apache.org>2021-10-18 10:48:04 +0000
commitb906b72d29f244a507b08360850fc9676210a87f (patch)
treeaac6a3f27e908e788a9e8a1659a71d3db8d398d4 /poi-ooxml
parent87a764956c280960f6117eee1bfb351d94c3a23b (diff)
downloadpoi-b906b72d29f244a507b08360850fc9676210a87f.tar.gz
poi-b906b72d29f244a507b08360850fc9676210a87f.zip
[bug-59388] Set comment with option isVisible in .xlsx. Thanks to ryoii. This closes #239
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894347 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java29
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java38
2 files changed, 60 insertions, 7 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java
index 441f242f4a..369a8bdc05 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java
@@ -26,6 +26,7 @@ import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.model.CommentsTable;
+import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STTrueFalseBlank;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
@@ -108,8 +109,13 @@ public class XSSFComment implements Comment {
public boolean isVisible() {
boolean visible = false;
if(_vmlShape != null) {
- String style = _vmlShape.getStyle();
- visible = style != null && style.contains("visibility:visible");
+ if (_vmlShape.sizeOfClientDataArray() > 0) {
+ CTClientData clientData = _vmlShape.getClientDataArray(0);
+ visible = clientData != null && clientData.sizeOfVisibleArray() > 0;
+ } else {
+ String style = _vmlShape.getStyle();
+ visible = style != null && style.contains("visibility:visible");
+ }
}
return visible;
}
@@ -121,11 +127,20 @@ public class XSSFComment implements Comment {
*/
@Override
public void setVisible(boolean visible) {
- if(_vmlShape != null){
- String style;
- if(visible) style = "position:absolute;visibility:visible";
- else style = "position:absolute;visibility:hidden";
- _vmlShape.setStyle(style);
+ if(_vmlShape != null) {
+ if (visible) {
+ _vmlShape.setStyle("position:absolute");
+ CTClientData clientData = _vmlShape.getClientDataArray(0);
+ if (clientData != null && clientData.sizeOfVisibleArray() == 0) {
+ clientData.addVisible(STTrueFalseBlank.X);
+ }
+ } else {
+ _vmlShape.setStyle("position:absolute;visibility:hidden");
+ CTClientData clientData = _vmlShape.getClientDataArray(0);
+ if (clientData != null && clientData.sizeOfVisibleArray() > 0) {
+ clientData.removeVisible(0);
+ }
+ }
}
}
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java
index e1b57d56e1..caea41f0d2 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java
@@ -29,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import com.microsoft.schemas.vml.CTShape;
+import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.ss.usermodel.BaseTestCellComment;
import org.apache.poi.ss.usermodel.Cell;
@@ -311,4 +312,41 @@ public final class TestXSSFComment extends BaseTestCellComment {
wb.close();
}
+
+ @Test
+ void bug59388CommentVisible() throws IOException {
+ try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
+ try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("59388.xlsx")) {
+ Sheet sheet = wb.getSheetAt(0);
+ Cell a1 = sheet.getRow(0).getCell(0);
+ Cell d1 = sheet.getRow(0).getCell(3);
+
+ Comment commentA1 = a1.getCellComment();
+ Comment commentD1 = d1.getCellComment();
+
+ // assert original visibility
+ assertTrue(commentA1.isVisible());
+ assertFalse(commentD1.isVisible());
+
+ commentA1.setVisible(false);
+ commentD1.setVisible(true);
+
+ // assert after changing
+ assertFalse(commentA1.isVisible());
+ assertTrue(commentD1.isVisible());
+
+ // check result
+ wb.write(bos);
+
+ try (Workbook wb2 = new XSSFWorkbook(bos.toInputStream())) {
+ Sheet sheetWb2 = wb2.getSheetAt(0);
+ Cell a1Wb2 = sheetWb2.getRow(0).getCell(0);
+ Cell d1Wb2 = sheetWb2.getRow(0).getCell(3);
+
+ assertFalse(a1Wb2.getCellComment().isVisible());
+ assertTrue(d1Wb2.getCellComment().isVisible());
+ }
+ }
+ }
+ }
}