diff --git a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java index e196642b..7689cea3 100644 --- a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java +++ b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java @@ -5,14 +5,52 @@ public class HyperLink { private final String linkStr; + private final HyperLinkType hyperLinkType; + + /** + * Static factory method which allows to create external HyperLink + * @param linkStr external link for which the hyperlink will lead to + * @param displayStr string which will displayed in hyperlink cell + * @return External HyperLink + */ + public static HyperLink external(String linkStr, String displayStr) { + return new HyperLink(linkStr, displayStr, HyperLinkType.EXTERNAL); + } + + /** + * Static factory method which allows to create internal HyperLink + * @param linkStr link for which the hyperlink will lead to + * @param displayStr string which will displayed in hyperlink cell + * @return Internal HyperLink + */ + public static HyperLink internal(String linkStr, String displayStr) { + return new HyperLink(linkStr, displayStr, HyperLinkType.INTERNAL); + } + public HyperLink(String linkStr) { - this.linkStr = linkStr; - this.displayStr = linkStr; + this(linkStr, linkStr, HyperLinkType.EXTERNAL); } + /** + * Default Constructor + * By default, the HyperLink will be marked as an external + * @param linkStr external link for which the hyperlink will lead to + * @param displayStr string which will displayed in hyperlink cell + */ public HyperLink(String linkStr, String displayStr) { + this(linkStr, displayStr, HyperLinkType.EXTERNAL); + } + + /** + * Constructor + * @param linkStr link for which the hyperlink will lead to + * @param displayStr string which will displayed in hyperlink cell + * @param hyperLinkType identifies type of the hyperlink + */ + HyperLink(String linkStr, String displayStr, HyperLinkType hyperLinkType) { this.linkStr = linkStr; this.displayStr = displayStr; + this.hyperLinkType = hyperLinkType; } @Override @@ -32,4 +70,8 @@ public String getDisplayStr() { public String getLinkStr() { return linkStr; } + + HyperLinkType getHyperLinkType() { + return hyperLinkType; + } } diff --git a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLinkType.java b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLinkType.java new file mode 100644 index 00000000..8d055c5c --- /dev/null +++ b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLinkType.java @@ -0,0 +1,8 @@ +package org.dhatim.fastexcel; + +/** + * Identifies type of hyperlinks, it can be an external hyperlink or internal + */ +enum HyperLinkType { + EXTERNAL, INTERNAL +} diff --git a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Worksheet.java b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Worksheet.java index 45d6b265..6a848ec1 100644 --- a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Worksheet.java +++ b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/Worksheet.java @@ -876,11 +876,15 @@ public void finish() throws IOException { writer.append(""); for (Map.Entry hr : hyperlinkRanges.entrySet()) { HyperLink hyperLink = hr.getKey(); - Ref ref = hr.getValue(); - String rId = relationships.setHyperLinkRels(hyperLink.getLinkStr(), "External"); writer.append(""); } writer.append(""); diff --git a/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java b/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java index 2c2664cb..6a12246d 100644 --- a/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java +++ b/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java @@ -678,4 +678,15 @@ void testCustomValidation() throws Exception { .errorStyle(DataValidationErrorStyle.STOP); }); } + + @Test + void testInternalHyperlinks() throws Exception{ + writeWorkbook(wb -> { + Worksheet worksheet1 = wb.newWorksheet("Sheet1"); + Worksheet worksheet2 = wb.newWorksheet("Sheet2"); + + worksheet1.hyperlink(1, 1, HyperLink.internal("Sheet2!A1", "HyperLink")); + worksheet1.hyperlink(7, 0, HyperLink.external("https://github.com/dhatim/fastexcel", "Test_Hyperlink_For_Cell")); + }); + } }