summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cell.go32
-rw-r--r--excelize_test.go16
2 files changed, 31 insertions, 17 deletions
diff --git a/cell.go b/cell.go
index 652dc9f..6125583 100644
--- a/cell.go
+++ b/cell.go
@@ -206,23 +206,37 @@ func (f *File) SetCellFormula(sheet, axis, formula string) {
}
// SetCellHyperLink provides function to set cell hyperlink by given sheet index
-// and link URL address. Only support external link currently. For example: add
-// hyperLink for Sheet1!A3:
+// and link URL address. LinkType defines two types of hyperlink "External" for
+// web site or "Location" for moving to one of cell in this workbook. The below
+// is example for external link.
//
-// xlsx.SetCellHyperLink("Sheet1", "A3", "https://github.com/xuri/excelize")
+// xlsx.SetCellHyperLink("Sheet1", "A3", "https://github.com/xuri/excelize", "External")
// // Set underline and font color style for the cell.
// style, _ := xlsx.NewStyle(`{"font":{"color":"#1265BE","underline":"single"}}`)
// xlsx.SetCellStyle("Sheet1", "A3", "A3", style)
//
-func (f *File) SetCellHyperLink(sheet, axis, link string) {
+// A this is another example for "Location"
+//
+// xlsx.SetCellHyperLink("Sheet1", "A3", "Sheet1!A40", "Location")
+func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) {
xlsx := f.workSheetReader(sheet)
axis = f.mergeCellsParser(xlsx, axis)
- rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
- hyperlink := xlsxHyperlink{
- Ref: axis,
- RID: "rId" + strconv.Itoa(rID),
+ var hyperlink xlsxHyperlink
+ if linkType == "External" {
+ rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
+ hyperlink = xlsxHyperlink{
+ Ref: axis,
+ RID: "rId" + strconv.Itoa(rID),
+ }
+ } else if linkType == "Location" {
+ hyperlink = xlsxHyperlink{
+ Ref: axis,
+ Location: link,
+ }
}
- if xlsx.Hyperlinks != nil {
+ if hyperlink.Ref == "" {
+ panic("linkType only support External and Location now")
+ } else if xlsx.Hyperlinks != nil {
xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
} else {
hyperlinks := xlsxHyperlinks{}
diff --git a/excelize_test.go b/excelize_test.go
index 613da1b..970973a 100644
--- a/excelize_test.go
+++ b/excelize_test.go
@@ -210,9 +210,9 @@ func TestSetCellHyperLink(t *testing.T) {
t.Log(err)
}
// Test set cell hyperlink in a work sheet already have hyperlinks.
- xlsx.SetCellHyperLink("sheet1", "B19", "https://github.com/xuri/excelize")
+ xlsx.SetCellHyperLink("sheet1", "B19", "https://github.com/xuri/excelize", "External")
// Test add first hyperlink in a work sheet.
- xlsx.SetCellHyperLink("sheet2", "C1", "https://github.com/xuri/excelize")
+ xlsx.SetCellHyperLink("sheet2", "C1", "https://github.com/xuri/excelize", "External")
err = xlsx.Save()
if err != nil {
t.Log(err)
@@ -275,7 +275,7 @@ func TestMergeCell(t *testing.T) {
xlsx.SetCellValue("Sheet1", "G11", "set value in merged cell")
xlsx.SetCellInt("Sheet1", "H11", 100)
xlsx.SetCellValue("Sheet1", "I11", float64(0.5))
- xlsx.SetCellHyperLink("Sheet1", "J11", "https://github.com/xuri/excelize")
+ xlsx.SetCellHyperLink("Sheet1", "J11", "https://github.com/xuri/excelize", "External")
xlsx.SetCellFormula("Sheet1", "G12", "SUM(Sheet1!B19,Sheet1!C19)")
xlsx.GetCellValue("Sheet1", "H11")
xlsx.GetCellFormula("Sheet1", "G12")
@@ -786,7 +786,7 @@ func TestInsertCol(t *testing.T) {
xlsx.SetCellStr("Sheet1", axis, axis)
}
}
- xlsx.SetCellHyperLink("Sheet1", "A5", "https://github.com/xuri/excelize")
+ xlsx.SetCellHyperLink("Sheet1", "A5", "https://github.com/xuri/excelize", "External")
xlsx.MergeCell("sheet1", "A1", "C3")
err := xlsx.AutoFilter("Sheet1", "A2", "B2", `{"column":"B","expression":"x != blanks"}`)
t.Log(err)
@@ -805,8 +805,8 @@ func TestRemoveCol(t *testing.T) {
xlsx.SetCellStr("Sheet1", axis, axis)
}
}
- xlsx.SetCellHyperLink("Sheet1", "A5", "https://github.com/xuri/excelize")
- xlsx.SetCellHyperLink("Sheet1", "C5", "https://github.com")
+ xlsx.SetCellHyperLink("Sheet1", "A5", "https://github.com/xuri/excelize", "External")
+ xlsx.SetCellHyperLink("Sheet1", "C5", "https://github.com", "External")
xlsx.MergeCell("sheet1", "A1", "B1")
xlsx.MergeCell("sheet1", "A2", "B2")
xlsx.RemoveCol("Sheet1", "A")
@@ -825,7 +825,7 @@ func TestInsertRow(t *testing.T) {
xlsx.SetCellStr("Sheet1", axis, axis)
}
}
- xlsx.SetCellHyperLink("Sheet1", "A5", "https://github.com/xuri/excelize")
+ xlsx.SetCellHyperLink("Sheet1", "A5", "https://github.com/xuri/excelize", "External")
xlsx.InsertRow("Sheet1", -1)
xlsx.InsertRow("Sheet1", 4)
err := xlsx.SaveAs("./test/Workbook_insertrow.xlsx")
@@ -857,7 +857,7 @@ func TestRemoveRow(t *testing.T) {
xlsx.SetCellStr("Sheet1", axis, axis)
}
}
- xlsx.SetCellHyperLink("Sheet1", "A5", "https://github.com/xuri/excelize")
+ xlsx.SetCellHyperLink("Sheet1", "A5", "https://github.com/xuri/excelize", "External")
xlsx.RemoveRow("Sheet1", -1)
xlsx.RemoveRow("Sheet1", 4)
xlsx.MergeCell("sheet1", "B3", "B5")