Add extra argument "Linktype" to SetCellHyperLink

it support "External" and "Location"

Signed-off-by: Youngwan Kim <y103.kim@gmail.com>
formula
Youngwan Kim 8 years ago
parent bbed2f6dc9
commit 5a4870d1cf

@ -206,23 +206,37 @@ func (f *File) SetCellFormula(sheet, axis, formula string) {
} }
// SetCellHyperLink provides function to set cell hyperlink by given sheet index // SetCellHyperLink provides function to set cell hyperlink by given sheet index
// and link URL address. Only support external link currently. For example: add // and link URL address. LinkType defines two types of hyperlink "External" for
// hyperLink for Sheet1!A3: // 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. // // Set underline and font color style for the cell.
// style, _ := xlsx.NewStyle(`{"font":{"color":"#1265BE","underline":"single"}}`) // style, _ := xlsx.NewStyle(`{"font":{"color":"#1265BE","underline":"single"}}`)
// xlsx.SetCellStyle("Sheet1", "A3", "A3", style) // 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) xlsx := f.workSheetReader(sheet)
axis = f.mergeCellsParser(xlsx, axis) axis = f.mergeCellsParser(xlsx, axis)
var hyperlink xlsxHyperlink
if linkType == "External" {
rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External") rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
hyperlink := xlsxHyperlink{ hyperlink = xlsxHyperlink{
Ref: axis, Ref: axis,
RID: "rId" + strconv.Itoa(rID), 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) xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
} else { } else {
hyperlinks := xlsxHyperlinks{} hyperlinks := xlsxHyperlinks{}

@ -210,9 +210,9 @@ func TestSetCellHyperLink(t *testing.T) {
t.Log(err) t.Log(err)
} }
// Test set cell hyperlink in a work sheet already have hyperlinks. // 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. // 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() err = xlsx.Save()
if err != nil { if err != nil {
t.Log(err) t.Log(err)
@ -275,7 +275,7 @@ func TestMergeCell(t *testing.T) {
xlsx.SetCellValue("Sheet1", "G11", "set value in merged cell") xlsx.SetCellValue("Sheet1", "G11", "set value in merged cell")
xlsx.SetCellInt("Sheet1", "H11", 100) xlsx.SetCellInt("Sheet1", "H11", 100)
xlsx.SetCellValue("Sheet1", "I11", float64(0.5)) 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.SetCellFormula("Sheet1", "G12", "SUM(Sheet1!B19,Sheet1!C19)")
xlsx.GetCellValue("Sheet1", "H11") xlsx.GetCellValue("Sheet1", "H11")
xlsx.GetCellFormula("Sheet1", "G12") xlsx.GetCellFormula("Sheet1", "G12")
@ -786,7 +786,7 @@ func TestInsertCol(t *testing.T) {
xlsx.SetCellStr("Sheet1", axis, axis) 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") xlsx.MergeCell("sheet1", "A1", "C3")
err := xlsx.AutoFilter("Sheet1", "A2", "B2", `{"column":"B","expression":"x != blanks"}`) err := xlsx.AutoFilter("Sheet1", "A2", "B2", `{"column":"B","expression":"x != blanks"}`)
t.Log(err) t.Log(err)
@ -805,8 +805,8 @@ func TestRemoveCol(t *testing.T) {
xlsx.SetCellStr("Sheet1", axis, axis) 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.SetCellHyperLink("Sheet1", "C5", "https://github.com") xlsx.SetCellHyperLink("Sheet1", "C5", "https://github.com", "External")
xlsx.MergeCell("sheet1", "A1", "B1") xlsx.MergeCell("sheet1", "A1", "B1")
xlsx.MergeCell("sheet1", "A2", "B2") xlsx.MergeCell("sheet1", "A2", "B2")
xlsx.RemoveCol("Sheet1", "A") xlsx.RemoveCol("Sheet1", "A")
@ -825,7 +825,7 @@ func TestInsertRow(t *testing.T) {
xlsx.SetCellStr("Sheet1", axis, axis) 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", -1)
xlsx.InsertRow("Sheet1", 4) xlsx.InsertRow("Sheet1", 4)
err := xlsx.SaveAs("./test/Workbook_insertrow.xlsx") err := xlsx.SaveAs("./test/Workbook_insertrow.xlsx")
@ -857,7 +857,7 @@ func TestRemoveRow(t *testing.T) {
xlsx.SetCellStr("Sheet1", axis, axis) 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", -1)
xlsx.RemoveRow("Sheet1", 4) xlsx.RemoveRow("Sheet1", 4)
xlsx.MergeCell("sheet1", "B3", "B5") xlsx.MergeCell("sheet1", "B3", "B5")

Loading…
Cancel
Save