]> source.dussan.org Git - poi.git/commitdiff
bug 59872: add Sheet.getHyperlink(CellAddress)
authorJaven O'Neal <onealj@apache.org>
Sun, 17 Jul 2016 04:41:20 +0000 (04:41 +0000)
committerJaven O'Neal <onealj@apache.org>
Sun, 17 Jul 2016 04:41:20 +0000 (04:41 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753009 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/java/org/apache/poi/ss/usermodel/Sheet.java
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java

index 3ff7005ba4a7eaa4ec3a11d9dd7def6380a9c890..e9e3cf6f75b44dd54d70404549c14597bd3530bc 100644 (file)
@@ -2215,6 +2215,17 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
         return null;
     }
     
+    /**
+     * Get a Hyperlink in this sheet located in a cell specified by {code addr}
+     *
+     * @param addr The address of the cell containing the hyperlink
+     * @return hyperlink if there is a hyperlink anchored at {@code addr}; otherwise returns {@code null}
+     */
+    @Override
+    public HSSFHyperlink getHyperlink(CellAddress addr) {
+        return getHyperlink(addr.getRow(), addr.getColumn());
+    }
+    
     /**
      * Get a list of Hyperlinks in this sheet
      *
index 74ad8690e2418e4eb1ddcad1f468fea9d790d3ab..3d449348ec7d7116e1d19618fa32c2b3cd0122fd 100644 (file)
@@ -1158,6 +1158,14 @@ public interface Sheet extends Iterable<Row> {
      */
     public Hyperlink getHyperlink(int row, int column);
     
+    /**
+     * Get a Hyperlink in this sheet located in a cell specified by {code addr}
+     *
+     * @param addr The address of the cell containing the hyperlink
+     * @return hyperlink if there is a hyperlink anchored at {@code addr}; otherwise returns {@code null}
+     */
+    public Hyperlink getHyperlink(CellAddress addr);
+    
     /**
      * Get a list of Hyperlinks in this sheet
      *
index 41e4e17f211a99899b978b57bd3aa1dd08a70830..eeb321d924c0f6b07a8586f7a1818b9f8dec7d7f 100644 (file)
@@ -1665,6 +1665,17 @@ public class SXSSFSheet implements Sheet
         return _sh.getHyperlink(row, column);
     }
     
+    /**
+     * Get a Hyperlink in this sheet located in a cell specified by {code addr}
+     *
+     * @param addr The address of the cell containing the hyperlink
+     * @return hyperlink if there is a hyperlink anchored at {@code addr}; otherwise returns {@code null}
+     */
+    @Override
+    public XSSFHyperlink getHyperlink(CellAddress addr) {
+        return _sh.getHyperlink(addr);
+    }
+    
     /**
      * Get a list of Hyperlinks in this sheet
      *
index 44474b0b4cfb4dfa95d8383db39610723bbef496..771986b263f7a0484c62273d9bd7bd9f633bb2a4 100644 (file)
@@ -806,7 +806,18 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
      */
     @Override
     public XSSFHyperlink getHyperlink(int row, int column) {
-        String ref = new CellReference(row, column).formatAsString();
+        return getHyperlink(new CellAddress(row, column));
+    }
+    
+    /**
+     * Get a Hyperlink in this sheet located in a cell specified by {code addr}
+     *
+     * @param addr The address of the cell containing the hyperlink
+     * @return hyperlink if there is a hyperlink anchored at {@code addr}; otherwise returns {@code null}
+     */
+    @Override
+    public XSSFHyperlink getHyperlink(CellAddress addr) {
+        String ref = addr.formatAsString();
         for(XSSFHyperlink hyperlink : hyperlinks) {
             if(hyperlink.getCellRef().equals(ref)) {
                 return hyperlink;
index b7b85cf730c443702cea513eefeded6ac2303297..65565f17bf8014101e15f0ba2e45c54ec564f3df 100644 (file)
@@ -1208,6 +1208,27 @@ public abstract class BaseTestSheet {
         workbook.close();
         wb.close();
     }
+    
+    @Test
+    public void getHyperlink() throws IOException {
+        Workbook workbook = _testDataProvider.createWorkbook();
+        Hyperlink hyperlink = workbook.getCreationHelper().createHyperlink(Hyperlink.LINK_URL);
+        hyperlink.setAddress("https://poi.apache.org/");
+        
+        Sheet sheet = workbook.createSheet();
+        Cell cell = sheet.createRow(5).createCell(1);
+        
+        assertEquals("list size before add", 0, sheet.getHyperlinkList().size());
+        cell.setHyperlink(hyperlink);
+        assertEquals("list size after add", 1, sheet.getHyperlinkList().size());
+        
+        assertEquals("list", hyperlink, sheet.getHyperlinkList().get(0));
+        assertEquals("row, col", hyperlink, sheet.getHyperlink(5, 1));
+        assertEquals("addr", hyperlink, sheet.getHyperlink(new CellAddress("B4")));
+        assertEquals("no hyperlink at A1", null, sheet.getHyperlink(CellAddress.A1));
+        
+        workbook.close();
+    }
 
 
     @Test