You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2021/10/18 10:48:04 UTC

svn commit: r1894347 - in /poi/trunk: poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java test-data/spreadsheet/59388.xlsx

Author: fanningpj
Date: Mon Oct 18 10:48:04 2021
New Revision: 1894347

URL: http://svn.apache.org/viewvc?rev=1894347&view=rev
Log:
[bug-59388] Set comment with option isVisible in .xlsx. Thanks to ryoii. This closes #239

Added:
    poi/trunk/test-data/spreadsheet/59388.xlsx   (with props)
Modified:
    poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java
    poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java

Modified: poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java?rev=1894347&r1=1894346&r2=1894347&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java (original)
+++ poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFComment.java Mon Oct 18 10:48:04 2021
@@ -26,6 +26,7 @@ import org.apache.poi.ss.usermodel.RichT
 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 Comm
     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 Comm
      */
     @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);
+                }
+            }
         }
     }
     

Modified: poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java?rev=1894347&r1=1894346&r2=1894347&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java (original)
+++ poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java Mon Oct 18 10:48:04 2021
@@ -29,6 +29,7 @@ import static org.junit.jupiter.api.Asse
 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 exten
 
         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());
+                }
+            }
+        }
+    }
 }

Added: poi/trunk/test-data/spreadsheet/59388.xlsx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/59388.xlsx?rev=1894347&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/59388.xlsx
------------------------------------------------------------------------------
--- svn:mime-type (added)
+++ svn:mime-type Mon Oct 18 10:48:04 2021
@@ -0,0 +1 @@
+application/vnd.openxmlformats-officedocument.spreadsheetml.sheet



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org