Skip to content

Commit

Permalink
Merge pull request #358 from JakKowalCzyk/add-internal-hyperlinks
Browse files Browse the repository at this point in the history
Added internal hyperlinks
  • Loading branch information
ochedru authored Nov 16, 2023
2 parents 94ce4fc + 6f6e22e commit b7c6f6e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
46 changes: 44 additions & 2 deletions fastexcel-writer/src/main/java/org/dhatim/fastexcel/HyperLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,4 +70,8 @@ public String getDisplayStr() {
public String getLinkStr() {
return linkStr;
}

HyperLinkType getHyperLinkType() {
return hyperLinkType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.dhatim.fastexcel;

/**
* Identifies type of hyperlinks, it can be an external hyperlink or internal
*/
enum HyperLinkType {
EXTERNAL, INTERNAL
}
Original file line number Diff line number Diff line change
Expand Up @@ -876,11 +876,15 @@ public void finish() throws IOException {
writer.append("<hyperlinks>");
for (Map.Entry<HyperLink, Ref> hr : hyperlinkRanges.entrySet()) {
HyperLink hyperLink = hr.getKey();
Ref ref = hr.getValue();
String rId = relationships.setHyperLinkRels(hyperLink.getLinkStr(), "External");
writer.append("<hyperlink ");
Ref ref = hr.getValue();
writer.append("ref=\"" + ref.toString()+"\" ");
writer.append("r:id=\"" + rId +"\" ");
if (hyperLink.getHyperLinkType().equals(HyperLinkType.EXTERNAL)) {
String rId = relationships.setHyperLinkRels(hyperLink.getLinkStr(), "External");
writer.append("r:id=\"" + rId +"\" ");
}else{
writer.append("location=\"").append(hyperLink.getLinkStr()).append("\"");
}
writer.append("/>");
}
writer.append("</hyperlinks>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
}
}

0 comments on commit b7c6f6e

Please sign in to comment.