From a8cf38ebd5537b678a3747cd246fd1c511c73331 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Tue, 8 Aug 2017 20:08:54 +0800 Subject: [PATCH] - New function `GetCellHyperLink()` added, relate issue #98; - go test added --- cell.go | 28 ++++++++++++++++++++++++++++ excelize_test.go | 15 +++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/cell.go b/cell.go index 5d9557c..b182e2b 100644 --- a/cell.go +++ b/cell.go @@ -241,6 +241,34 @@ func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) { xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink) } +// GetCellHyperLink provides function to get cell hyperlink by given sheet index +// and axis. Boolean type value link will be ture if the cell has a hyperlink +// and the target is the address of the hyperlink. Otherwise, the value of link +// will be false and the value of the target will be a blank string. For example +// get hyperlink of Sheet1!H6: +// +// link, target := xlsx.GetCellHyperLink("Sheet1", "H6") +// +func (f *File) GetCellHyperLink(sheet, axis string) (bool, string) { + var link bool + var target string + xlsx := f.workSheetReader(sheet) + axis = f.mergeCellsParser(xlsx, axis) + if xlsx.Hyperlinks == nil || axis == "" { + return link, target + } + for _, h := range xlsx.Hyperlinks.Hyperlink { + if h.Ref == axis { + link = true + target = h.Location + if h.RID != "" { + target = f.getSheetRelationshipsTargetByID(sheet, h.RID) + } + } + } + return link, target +} + // MergeCell provides function to merge cells by given coordinate area and sheet // name. For example create a merged cell of D3:E9 on Sheet1: // diff --git a/excelize_test.go b/excelize_test.go index 89c8ce9..2798319 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -223,6 +223,21 @@ func TestSetCellHyperLink(t *testing.T) { } } +func TestGetCellHyperLink(t *testing.T) { + xlsx, err := OpenFile("./test/Workbook1.xlsx") + if err != nil { + t.Log(err) + } + link, target := xlsx.GetCellHyperLink("Sheet1", "") + t.Log(link, target) + link, target = xlsx.GetCellHyperLink("Sheet1", "B19") + t.Log(link, target) + link, target = xlsx.GetCellHyperLink("Sheet2", "D6") + t.Log(link, target) + link, target = xlsx.GetCellHyperLink("Sheet3", "H3") + t.Log(link, target) +} + func TestSetCellFormula(t *testing.T) { xlsx, err := OpenFile("./test/Workbook1.xlsx") if err != nil {