]> source.dussan.org Git - poi.git/commitdiff
[bug-65993] support copying hssf hyperlinks to xssf and respect full cell range
authorPJ Fanning <fanningpj@apache.org>
Sat, 2 Apr 2022 18:55:56 +0000 (18:55 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 2 Apr 2022 18:55:56 +0000 (18:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899521 13f79535-47bb-0310-9956-ffa450edef68

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

index dca5a130e96e41dfe701ae18f189a837cd2241c4..a51ef12b29add9884c7aa50aac07b389f2677954 100644 (file)
@@ -121,7 +121,11 @@ public class XSSFHyperlink implements Hyperlink, Duplicatable {
             _location = other.getAddress();
             _externalRel = null;
             _ctHyperlink = CTHyperlink.Factory.newInstance();
-            setCellReference(new CellReference(other.getFirstRow(), other.getFirstColumn()));
+            _ctHyperlink.setDisplay(other.getLabel());
+            setFirstColumn(other.getFirstColumn());
+            setLastColumn(other.getLastColumn());
+            setFirstRow(other.getFirstRow());
+            setLastRow(other.getLastRow());
         }
     }
     /**
index 05892c9b8fd10ad3938e8070a3fdc7ecbc3aea58..11573bb8c12de7b26981a9dbad605ae311389f07 100644 (file)
@@ -209,6 +209,78 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
         }
     }
 
+    @Test
+    void testCopyHSSFHyperlink() throws IOException {
+        try (HSSFWorkbook hssfworkbook = new HSSFWorkbook()) {
+            HSSFHyperlink hlink = hssfworkbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
+            hlink.setAddress("http://poi.apache.org/");
+            hlink.setFirstColumn(3);
+            hlink.setFirstRow(2);
+            hlink.setLastColumn(3);
+            hlink.setLastRow(2);
+            hlink.setLabel("label");
+            XSSFHyperlink xlink = new XSSFHyperlink(hlink);
+
+            assertEquals("http://poi.apache.org/", xlink.getAddress());
+            assertEquals(new CellReference(2, 3), new CellReference(xlink.getCellRef()));
+            // Are HSSFHyperlink.label and XSSFHyperlink.tooltip the same? If so, perhaps one of these needs renamed for a consistent Hyperlink interface
+            // assertEquals("label", xlink.getTooltip());
+        }
+    }
+
+    @Test
+    void setHyperlinkOnCellFromHSSF() throws Exception {
+        try (XSSFWorkbook xssfWorkbook = new XSSFWorkbook()) {
+            XSSFSheet xssfSheet = xssfWorkbook.createSheet();
+            XSSFRow xssfRow = xssfSheet.createRow(0);
+            XSSFCell xssfCell = xssfRow.createCell(0);
+            xssfCell.setCellValue("link");
+            HSSFHyperlink hssfHyperlink = new HSSFHyperlink(HyperlinkType.URL) {
+
+            };
+            hssfHyperlink.setLabel("label");
+            hssfHyperlink.setAddress("https://example.com/index.html#label");
+            hssfHyperlink.setFirstColumn(2);
+            hssfHyperlink.setLastColumn(3);
+            hssfHyperlink.setFirstRow(4);
+            hssfHyperlink.setLastRow(5);
+            xssfCell.setHyperlink(hssfHyperlink);
+            XSSFHyperlink xssfHyperlink = xssfCell.getHyperlink();
+            assertEquals(hssfHyperlink.getType(), xssfHyperlink.getType());
+            assertEquals(hssfHyperlink.getAddress(), xssfHyperlink.getAddress());
+            assertEquals(hssfHyperlink.getLabel(), xssfHyperlink.getLabel());
+            assertEquals("A1", xssfHyperlink.getCellRef());
+        }
+    }
+
+    @Test
+    void setHyperlinkOnSheetFromHSSF() throws Exception {
+        try (XSSFWorkbook xssfWorkbook = new XSSFWorkbook()) {
+            XSSFSheet xssfSheet = xssfWorkbook.createSheet();
+            XSSFRow xssfRow = xssfSheet.createRow(0);
+            XSSFCell xssfCell = xssfRow.createCell(0);
+            xssfCell.setCellValue("link");
+            HSSFHyperlink hssfHyperlink = new HSSFHyperlink(HyperlinkType.URL) {
+
+            };
+            hssfHyperlink.setLabel("label");
+            hssfHyperlink.setAddress("https://example.com/index.html#label");
+            hssfHyperlink.setFirstColumn(2);
+            hssfHyperlink.setLastColumn(3);
+            hssfHyperlink.setFirstRow(4);
+            hssfHyperlink.setLastRow(5);
+            XSSFHyperlink xssfHyperlink = new XSSFHyperlink(hssfHyperlink);
+            xssfSheet.addHyperlink(xssfHyperlink);
+            assertEquals(hssfHyperlink.getType(), xssfHyperlink.getType());
+            assertEquals(hssfHyperlink.getAddress(), xssfHyperlink.getAddress());
+            assertEquals(hssfHyperlink.getLabel(), xssfHyperlink.getLabel());
+            assertEquals(hssfHyperlink.getFirstColumn(), xssfHyperlink.getFirstColumn());
+            assertEquals(hssfHyperlink.getLastColumn(), xssfHyperlink.getLastColumn());
+            assertEquals(hssfHyperlink.getFirstRow(), xssfHyperlink.getFirstRow());
+            assertEquals(hssfHyperlink.getLastRow(), xssfHyperlink.getLastRow());
+        }
+    }
+
     /**
      * Only for WithMoreVariousData.xlsx !
      */
@@ -300,26 +372,6 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
         return new XSSFHyperlink(link);
     }
 
-    @Test
-    void testCopyHSSFHyperlink() throws IOException {
-        HSSFWorkbook hssfworkbook = new HSSFWorkbook();
-        HSSFHyperlink hlink = hssfworkbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
-        hlink.setAddress("http://poi.apache.org/");
-        hlink.setFirstColumn(3);
-        hlink.setFirstRow(2);
-        hlink.setLastColumn(5);
-        hlink.setLastRow(6);
-        hlink.setLabel("label");
-        XSSFHyperlink xlink = new XSSFHyperlink(hlink);
-
-        assertEquals("http://poi.apache.org/", xlink.getAddress());
-        assertEquals(new CellReference(2, 3), new CellReference(xlink.getCellRef()));
-        // Are HSSFHyperlink.label and XSSFHyperlink.tooltip the same? If so, perhaps one of these needs renamed for a consistent Hyperlink interface
-        // assertEquals("label", xlink.getTooltip());
-
-        hssfworkbook.close();
-    }
-
     /* bug 59775: XSSFHyperlink has wrong type if it contains a location (CTHyperlink#getLocation)
      * URLs with a hash mark (#) are still URL hyperlinks, not document links
      */