]> source.dussan.org Git - poi.git/commitdiff
support removing hyperlink from one cell when hyperlink is shared by many cells
authorPJ Fanning <fanningpj@apache.org>
Wed, 11 Aug 2021 21:25:00 +0000 (21:25 +0000)
committerPJ Fanning <fanningpj@apache.org>
Wed, 11 Aug 2021 21:25:00 +0000 (21:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1892249 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java

index d278785d576e571326faa9bc5b999c0a249e0fd0..05892c9b8fd10ad3938e8070a3fdc7ecbc3aea58 100644 (file)
@@ -32,6 +32,8 @@ import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.junit.jupiter.api.Test;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
 
 import static org.junit.jupiter.api.Assertions.*;
 
@@ -491,6 +493,32 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
         testRemoveSharedHyperlinkFromOneCell("D3:D5", new CellAddress("D5"));
     }
 
+    @Test
+    void testRemoveSharedHyperlinkFromOneCellWithCellRefs() throws IOException {
+        try (XSSFWorkbook wb = new XSSFWorkbook()) {
+            XSSFSheet sheet = wb.createSheet();
+            XSSFHyperlink hyperlink = new XSSFHyperlink(HyperlinkType.URL);
+            hyperlink.setAddress("https://poi.apache.org");
+            hyperlink.setLocation("poi-location");
+            hyperlink.setLabel("poi-label");
+            hyperlink.setCellReference("A1:E5");
+            sheet.addHyperlink(hyperlink);
+            CellAddress cellAddress = new CellAddress("C3");
+            sheet.removeHyperlink(cellAddress.getRow(), cellAddress.getColumn());
+            assertNull(sheet.getHyperlink(cellAddress), "cell " + cellAddress.formatAsString() + "should no longer has a hyperlink");
+            ArrayList<String> newRefs = new ArrayList<>();
+            for (XSSFHyperlink testHyperlink : sheet.getHyperlinkList()) {
+                newRefs.add(testHyperlink.getCellRef());
+            }
+            assertEquals(4, newRefs.size());
+            HashSet<String> set = new HashSet<>(newRefs);
+            assertTrue(set.contains("A1:B5"), "contains A1:B5");
+            assertTrue(set.contains("D1:E5"), "contains D1:E5");
+            assertTrue(set.contains("C1:C2"), "contains C1:C2");
+            assertTrue(set.contains("C4:C5"), "contains C4:C5");
+        }
+    }
+
     private void testRemoveSharedHyperlinkFromOneCell(String area, CellAddress cellAddress) throws IOException {
         try (XSSFWorkbook wb = new XSSFWorkbook()) {
             XSSFSheet sheet = wb.createSheet();