Add extra argument "Linktype" to SetCellHyperLink

it support "External" and "Location"

Signed-off-by: Youngwan Kim <y103.kim@gmail.com>
This commit is contained in:
Youngwan Kim 2017-07-30 14:52:37 +09:00
parent bbed2f6dc9
commit 5a4870d1cf
2 changed files with 31 additions and 17 deletions

26
cell.go
View File

@ -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)
var hyperlink xlsxHyperlink
if linkType == "External" {
rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
hyperlink := xlsxHyperlink{
hyperlink = xlsxHyperlink{
Ref: axis,
RID: "rId" + strconv.Itoa(rID),
}
if xlsx.Hyperlinks != nil {
} else if linkType == "Location" {
hyperlink = xlsxHyperlink{
Ref: axis,
Location: link,
}
}
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{}

View File

@ -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")