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/08/10 19:40:03 UTC

svn commit: r1892189 - in /poi/trunk/poi-ooxml/src: main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

Author: fanningpj
Date: Tue Aug 10 19:40:03 2021
New Revision: 1892189

URL: http://svn.apache.org/viewvc?rev=1892189&view=rev
Log:
[bug-65490] add support for removing hyperlink directly from sheet

Modified:
    poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

Modified: poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=1892189&r1=1892188&r2=1892189&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Tue Aug 10 19:40:03 2021
@@ -3356,16 +3356,30 @@ public class XSSFSheet extends POIXMLDoc
     }
 
     /**
-     * Register a hyperlink in the collection of hyperlinks on this sheet
+     * Register a hyperlink in the collection of hyperlinks on this sheet.
+     * Use {@link XSSFCell#setHyperlink(Hyperlink)} if the hyperlink is just for that one cell.
+     * Use this method if you want to add a Hyperlink that covers a range of sells. If you use
+     * this method, you will need to call {@link XSSFHyperlink#setCellReference(String)} to
+     * explicitly cell the value, eg B2 or B2:C3 (the 4 cells with B2 at top left and C3 at bottom right)
      *
      * @param hyperlink the link to add
      */
-    @Internal
     public void addHyperlink(XSSFHyperlink hyperlink) {
         hyperlinks.add(hyperlink);
     }
 
     /**
+     * Remove a hyperlink in the collection of hyperlinks on this sheet.
+     * {@link XSSFCell#removeHyperlink()} can be used if the hyperlink is just for that one cell.
+     *
+     * @param hyperlink the link to remove
+     * @since POI 5.0.1
+     */
+    public void removeHyperlink(XSSFHyperlink hyperlink) {
+        hyperlinks.remove(hyperlink);
+    }
+
+    /**
      * Removes a hyperlink in the collection of hyperlinks on this sheet
      *
      * @param row row index

Modified: poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1892189&r1=1892188&r2=1892189&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java (original)
+++ poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java Tue Aug 10 19:40:03 2021
@@ -1790,29 +1790,35 @@ public final class TestXSSFBugs extends
         try (XSSFWorkbook wb = new XSSFWorkbook()) {
             XSSFSheet sheet = wb.createSheet();
             XSSFCreationHelper creationHelper = wb.getCreationHelper();
-            XSSFHyperlink hyperlink;
 
             // Try with a cell reference
-            hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
-            sheet.addHyperlink(hyperlink);
-            hyperlink.setAddress("http://myurl");
-            hyperlink.setCellReference("B4");
-            assertEquals(3, hyperlink.getFirstRow());
-            assertEquals(1, hyperlink.getFirstColumn());
-            assertEquals(3, hyperlink.getLastRow());
-            assertEquals(1, hyperlink.getLastColumn());
+            XSSFHyperlink hyperlink1 = creationHelper.createHyperlink(HyperlinkType.URL);
+            sheet.addHyperlink(hyperlink1);
+            hyperlink1.setAddress("http://myurl");
+            hyperlink1.setCellReference("B4");
+            assertEquals(3, hyperlink1.getFirstRow());
+            assertEquals(1, hyperlink1.getFirstColumn());
+            assertEquals(3, hyperlink1.getLastRow());
+            assertEquals(1, hyperlink1.getLastColumn());
 
             // Try with explicit rows / columns
-            hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
-            sheet.addHyperlink(hyperlink);
-            hyperlink.setAddress("http://myurl");
-            hyperlink.setFirstRow(5);
-            hyperlink.setFirstColumn(3);
-
-            assertEquals(5, hyperlink.getFirstRow());
-            assertEquals(3, hyperlink.getFirstColumn());
-            assertEquals(5, hyperlink.getLastRow());
-            assertEquals(3, hyperlink.getLastColumn());
+            XSSFHyperlink hyperlink2 = creationHelper.createHyperlink(HyperlinkType.URL);
+            sheet.addHyperlink(hyperlink2);
+            hyperlink2.setAddress("http://myurl");
+            hyperlink2.setFirstRow(5);
+            hyperlink2.setFirstColumn(3);
+
+            assertEquals(5, hyperlink2.getFirstRow());
+            assertEquals(3, hyperlink2.getFirstColumn());
+            assertEquals(5, hyperlink2.getLastRow());
+            assertEquals(3, hyperlink2.getLastColumn());
+
+            assertTrue(sheet.getHyperlinkList().contains(hyperlink1), "sheet contains hyperlink1");
+            assertTrue(sheet.getHyperlinkList().contains(hyperlink2), "sheet contains hyperlink2");
+
+            sheet.removeHyperlink(hyperlink1);
+            assertFalse(sheet.getHyperlinkList().contains(hyperlink1), "sheet no longer contains hyperlink1");
+            assertTrue(sheet.getHyperlinkList().contains(hyperlink2), "sheet still contains hyperlink2");
         }
     }
 



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