From d0ffc7c0a70e5aeba507b235f7b779a57500fa1a Mon Sep 17 00:00:00 2001 From: "jakub.kowalczyk" Date: Wed, 15 Nov 2023 13:11:32 +0100 Subject: [PATCH 1/4] Added possibility to define type of HyperLink and to define internal HyperLink --- .../java/org/dhatim/fastexcel/HyperLink.java | 26 +++++++++++++++++-- .../org/dhatim/fastexcel/HyperLinkType.java | 8 ++++++ .../java/org/dhatim/fastexcel/Worksheet.java | 10 ++++--- .../org/dhatim/fastexcel/CorrectnessTest.java | 13 ++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLinkType.java 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..17bd3522 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,32 @@ public class HyperLink { private final String linkStr; + private final HyperLinkType hyperLinkType; + 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 + */ + public HyperLink(String linkStr, String displayStr, HyperLinkType hyperLinkType) { this.linkStr = linkStr; this.displayStr = displayStr; + this.hyperLinkType = hyperLinkType; } @Override @@ -32,4 +50,8 @@ public String getDisplayStr() { public String getLinkStr() { return linkStr; } + + public 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..2024f16b --- /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 + */ +public 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..7e084cd4 100644 --- a/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java +++ b/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java @@ -19,6 +19,8 @@ import org.junit.jupiter.api.Test; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.math.BigDecimal; import java.time.*; @@ -678,4 +680,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, new HyperLink("Sheet2!A1", "HyperLink", HyperLinkType.INTERNAL)); + worksheet1.hyperlink(7, 0, new HyperLink("https://github.com/dhatim/fastexcel", "Test_Hyperlink_For_Cell")); + }); + } } From 23b97fc97c0b55375c487531c15cb47d31795ee6 Mon Sep 17 00:00:00 2001 From: "jakub.kowalczyk" Date: Wed, 15 Nov 2023 13:12:13 +0100 Subject: [PATCH 2/4] Added possibility to define type of HyperLink and to define internal HyperLink --- .../src/test/java/org/dhatim/fastexcel/CorrectnessTest.java | 2 -- 1 file changed, 2 deletions(-) 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 7e084cd4..3182f294 100644 --- a/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java +++ b/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java @@ -19,8 +19,6 @@ import org.junit.jupiter.api.Test; import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.math.BigDecimal; import java.time.*; From 67a2ad7232420ca0e3cd363945a8f7fb6b727c0f Mon Sep 17 00:00:00 2001 From: "jakub.kowalczyk" Date: Wed, 15 Nov 2023 14:26:01 +0100 Subject: [PATCH 3/4] Added factory methods for HyperLinks, changed HyperLinkType visibility to package-private --- .../java/org/dhatim/fastexcel/HyperLink.java | 22 ++++++++++++++++++- .../org/dhatim/fastexcel/HyperLinkType.java | 2 +- .../org/dhatim/fastexcel/CorrectnessTest.java | 4 ++-- 3 files changed, 24 insertions(+), 4 deletions(-) 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 17bd3522..00023516 100644 --- a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java +++ b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java @@ -7,6 +7,26 @@ public class HyperLink { 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, HyperLinkType.EXTERNAL); } @@ -51,7 +71,7 @@ public String getLinkStr() { return linkStr; } - public HyperLinkType getHyperLinkType() { + 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 index 2024f16b..8d055c5c 100644 --- a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLinkType.java +++ b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLinkType.java @@ -3,6 +3,6 @@ /** * Identifies type of hyperlinks, it can be an external hyperlink or internal */ -public enum HyperLinkType { +enum HyperLinkType { EXTERNAL, INTERNAL } 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 3182f294..6a12246d 100644 --- a/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java +++ b/fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java @@ -685,8 +685,8 @@ void testInternalHyperlinks() throws Exception{ Worksheet worksheet1 = wb.newWorksheet("Sheet1"); Worksheet worksheet2 = wb.newWorksheet("Sheet2"); - worksheet1.hyperlink(1, 1, new HyperLink("Sheet2!A1", "HyperLink", HyperLinkType.INTERNAL)); - worksheet1.hyperlink(7, 0, new HyperLink("https://github.com/dhatim/fastexcel", "Test_Hyperlink_For_Cell")); + worksheet1.hyperlink(1, 1, HyperLink.internal("Sheet2!A1", "HyperLink")); + worksheet1.hyperlink(7, 0, HyperLink.external("https://github.com/dhatim/fastexcel", "Test_Hyperlink_For_Cell")); }); } } From 6f6e22edf8bf41f0542c5b349a79bc2df5819532 Mon Sep 17 00:00:00 2001 From: "jakub.kowalczyk" Date: Wed, 15 Nov 2023 14:27:33 +0100 Subject: [PATCH 4/4] Added factory methods for HyperLinks, changed HyperLinkType visibility to package-private --- .../src/main/java/org/dhatim/fastexcel/HyperLink.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 00023516..7689cea3 100644 --- a/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java +++ b/fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java @@ -47,7 +47,7 @@ public HyperLink(String linkStr, String displayStr) { * @param displayStr string which will displayed in hyperlink cell * @param hyperLinkType identifies type of the hyperlink */ - public HyperLink(String linkStr, String displayStr, HyperLinkType hyperLinkType) { + HyperLink(String linkStr, String displayStr, HyperLinkType hyperLinkType) { this.linkStr = linkStr; this.displayStr = displayStr; this.hyperLinkType = hyperLinkType;