Skip to content

Commit b7c6f6e

Browse files
authored
Merge pull request #358 from JakKowalCzyk/add-internal-hyperlinks
Added internal hyperlinks
2 parents 94ce4fc + 6f6e22e commit b7c6f6e

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,52 @@ public class HyperLink {
55

66
private final String linkStr;
77

8+
private final HyperLinkType hyperLinkType;
9+
10+
/**
11+
* Static factory method which allows to create external HyperLink
12+
* @param linkStr external link for which the hyperlink will lead to
13+
* @param displayStr string which will displayed in hyperlink cell
14+
* @return External HyperLink
15+
*/
16+
public static HyperLink external(String linkStr, String displayStr) {
17+
return new HyperLink(linkStr, displayStr, HyperLinkType.EXTERNAL);
18+
}
19+
20+
/**
21+
* Static factory method which allows to create internal HyperLink
22+
* @param linkStr link for which the hyperlink will lead to
23+
* @param displayStr string which will displayed in hyperlink cell
24+
* @return Internal HyperLink
25+
*/
26+
public static HyperLink internal(String linkStr, String displayStr) {
27+
return new HyperLink(linkStr, displayStr, HyperLinkType.INTERNAL);
28+
}
29+
830
public HyperLink(String linkStr) {
9-
this.linkStr = linkStr;
10-
this.displayStr = linkStr;
31+
this(linkStr, linkStr, HyperLinkType.EXTERNAL);
1132
}
1233

34+
/**
35+
* Default Constructor
36+
* By default, the HyperLink will be marked as an external
37+
* @param linkStr external link for which the hyperlink will lead to
38+
* @param displayStr string which will displayed in hyperlink cell
39+
*/
1340
public HyperLink(String linkStr, String displayStr) {
41+
this(linkStr, displayStr, HyperLinkType.EXTERNAL);
42+
}
43+
44+
/**
45+
* Constructor
46+
* @param linkStr link for which the hyperlink will lead to
47+
* @param displayStr string which will displayed in hyperlink cell
48+
* @param hyperLinkType identifies type of the hyperlink
49+
*/
50+
HyperLink(String linkStr, String displayStr, HyperLinkType hyperLinkType) {
1451
this.linkStr = linkStr;
1552
this.displayStr = displayStr;
53+
this.hyperLinkType = hyperLinkType;
1654
}
1755

1856
@Override
@@ -32,4 +70,8 @@ public String getDisplayStr() {
3270
public String getLinkStr() {
3371
return linkStr;
3472
}
73+
74+
HyperLinkType getHyperLinkType() {
75+
return hyperLinkType;
76+
}
3577
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.dhatim.fastexcel;
2+
3+
/**
4+
* Identifies type of hyperlinks, it can be an external hyperlink or internal
5+
*/
6+
enum HyperLinkType {
7+
EXTERNAL, INTERNAL
8+
}

fastexcel-writer/src/main/java/org/dhatim/fastexcel/Worksheet.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -876,11 +876,15 @@ public void finish() throws IOException {
876876
writer.append("<hyperlinks>");
877877
for (Map.Entry<HyperLink, Ref> hr : hyperlinkRanges.entrySet()) {
878878
HyperLink hyperLink = hr.getKey();
879-
Ref ref = hr.getValue();
880-
String rId = relationships.setHyperLinkRels(hyperLink.getLinkStr(), "External");
881879
writer.append("<hyperlink ");
880+
Ref ref = hr.getValue();
882881
writer.append("ref=\"" + ref.toString()+"\" ");
883-
writer.append("r:id=\"" + rId +"\" ");
882+
if (hyperLink.getHyperLinkType().equals(HyperLinkType.EXTERNAL)) {
883+
String rId = relationships.setHyperLinkRels(hyperLink.getLinkStr(), "External");
884+
writer.append("r:id=\"" + rId +"\" ");
885+
}else{
886+
writer.append("location=\"").append(hyperLink.getLinkStr()).append("\"");
887+
}
884888
writer.append("/>");
885889
}
886890
writer.append("</hyperlinks>");

fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,4 +678,15 @@ void testCustomValidation() throws Exception {
678678
.errorStyle(DataValidationErrorStyle.STOP);
679679
});
680680
}
681+
682+
@Test
683+
void testInternalHyperlinks() throws Exception{
684+
writeWorkbook(wb -> {
685+
Worksheet worksheet1 = wb.newWorksheet("Sheet1");
686+
Worksheet worksheet2 = wb.newWorksheet("Sheet2");
687+
688+
worksheet1.hyperlink(1, 1, HyperLink.internal("Sheet2!A1", "HyperLink"));
689+
worksheet1.hyperlink(7, 0, HyperLink.external("https://github.com/dhatim/fastexcel", "Test_Hyperlink_For_Cell"));
690+
});
691+
}
681692
}

0 commit comments

Comments
 (0)