Skip to content

Commit 53b6515

Browse files
committed
This closes qax-os#1940, SetCellHyperLink function now support remove hyperlink by None linkType
- Update unit tests
1 parent 7999a49 commit 53b6515

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

cell.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -957,14 +957,36 @@ type HyperlinkOpts struct {
957957
Tooltip *string
958958
}
959959

960+
// removeHyperLink remove hyperlink for worksheet and delete relationships for
961+
// the worksheet by given sheet name and cell reference. Note that if the cell
962+
// in a range reference, the whole hyperlinks will be deleted.
963+
func (f *File) removeHyperLink(ws *xlsxWorksheet, sheet, cell string) error {
964+
for idx := 0; idx < len(ws.Hyperlinks.Hyperlink); idx++ {
965+
link := ws.Hyperlinks.Hyperlink[idx]
966+
ok, err := f.checkCellInRangeRef(cell, link.Ref)
967+
if err != nil {
968+
return err
969+
}
970+
if link.Ref == cell || ok {
971+
ws.Hyperlinks.Hyperlink = append(ws.Hyperlinks.Hyperlink[:idx], ws.Hyperlinks.Hyperlink[idx+1:]...)
972+
idx--
973+
f.deleteSheetRelationships(sheet, link.RID)
974+
}
975+
}
976+
if len(ws.Hyperlinks.Hyperlink) == 0 {
977+
ws.Hyperlinks = nil
978+
}
979+
return nil
980+
}
981+
960982
// SetCellHyperLink provides a function to set cell hyperlink by given
961-
// worksheet name and link URL address. LinkType defines two types of
983+
// worksheet name and link URL address. LinkType defines three types of
962984
// hyperlink "External" for website or "Location" for moving to one of cell in
963-
// this workbook. Maximum limit hyperlinks in a worksheet is 65530. This
964-
// function is only used to set the hyperlink of the cell and doesn't affect
965-
// the value of the cell. If you need to set the value of the cell, please use
966-
// the other functions such as `SetCellStyle` or `SetSheetRow`. The below is
967-
// example for external link.
985+
// this workbook or "None" for remove hyperlink. Maximum limit hyperlinks in a
986+
// worksheet is 65530. This function is only used to set the hyperlink of the
987+
// cell and doesn't affect the value of the cell. If you need to set the value
988+
// of the cell, please use the other functions such as `SetCellStyle` or
989+
// `SetSheetRow`. The below is example for external link.
968990
//
969991
// display, tooltip := "https://github.com/xuri/excelize", "Excelize on GitHub"
970992
// if err := f.SetCellHyperLink("Sheet1", "A3",
@@ -1032,6 +1054,8 @@ func (f *File) SetCellHyperLink(sheet, cell, link, linkType string, opts ...Hype
10321054
Ref: cell,
10331055
Location: link,
10341056
}
1057+
case "None":
1058+
return f.removeHyperLink(ws, sheet, cell)
10351059
default:
10361060
return newInvalidLinkTypeError(linkType)
10371061
}

excelize_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,18 @@ func TestSetCellHyperLink(t *testing.T) {
455455
assert.Equal(t, link, true)
456456
assert.Equal(t, "https://github.com/xuri/excelize", target)
457457
assert.NoError(t, err)
458+
459+
// Test remove hyperlink for a cell
460+
f = NewFile()
461+
assert.NoError(t, f.SetCellHyperLink("Sheet1", "A1", "Sheet1!D8", "Location"))
462+
ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
463+
assert.True(t, ok)
464+
ws.(*xlsxWorksheet).Hyperlinks.Hyperlink[0].Ref = "A1:D4"
465+
assert.NoError(t, f.SetCellHyperLink("Sheet1", "B2", "", "None"))
466+
// Test remove hyperlink for a cell with invalid cell reference
467+
assert.NoError(t, f.SetCellHyperLink("Sheet1", "A1", "Sheet1!D8", "Location"))
468+
ws.(*xlsxWorksheet).Hyperlinks.Hyperlink[0].Ref = "A:A"
469+
assert.Error(t, f.SetCellHyperLink("Sheet1", "B2", "", "None"), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")))
458470
}
459471

460472
func TestGetCellHyperLink(t *testing.T) {

0 commit comments

Comments
 (0)