Skip to content

Commit

Permalink
Add extra argument "Linktype" to SetCellHyperLink
Browse files Browse the repository at this point in the history
it support "External" and "Location"

Signed-off-by: Youngwan Kim <[email protected]>
  • Loading branch information
y103kim committed Jul 30, 2017
1 parent bbed2f6 commit 5a4870d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
32 changes: 23 additions & 9 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
16 changes: 8 additions & 8 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 5a4870d

Please sign in to comment.