From fb95dfdd93bc36b03cb7b303e6efe1bbe4868747 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 2 Apr 2022 18:55:56 +0000 Subject: [PATCH] [bug-65993] support copying hssf hyperlinks to xssf and respect full cell range git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899521 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/usermodel/XSSFHyperlink.java | 6 +- .../poi/xssf/usermodel/TestXSSFHyperlink.java | 92 +++++++++++++++---- 2 files changed, 77 insertions(+), 21 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java index dca5a130e9..a51ef12b29 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java @@ -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()); } } /** diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java index 05892c9b8f..11573bb8c1 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java @@ -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 */ -- 2.39.5