This closes #1940, SetCellHyperLink function now support remove hyperlink by None linkType

- Update unit tests
This commit is contained in:
xuri 2024-07-07 17:22:13 +08:00
parent 7999a492a4
commit 53b65150ce
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 42 additions and 6 deletions

36
cell.go
View File

@ -957,14 +957,36 @@ type HyperlinkOpts struct {
Tooltip *string Tooltip *string
} }
// removeHyperLink remove hyperlink for worksheet and delete relationships for
// the worksheet by given sheet name and cell reference. Note that if the cell
// in a range reference, the whole hyperlinks will be deleted.
func (f *File) removeHyperLink(ws *xlsxWorksheet, sheet, cell string) error {
for idx := 0; idx < len(ws.Hyperlinks.Hyperlink); idx++ {
link := ws.Hyperlinks.Hyperlink[idx]
ok, err := f.checkCellInRangeRef(cell, link.Ref)
if err != nil {
return err
}
if link.Ref == cell || ok {
ws.Hyperlinks.Hyperlink = append(ws.Hyperlinks.Hyperlink[:idx], ws.Hyperlinks.Hyperlink[idx+1:]...)
idx--
f.deleteSheetRelationships(sheet, link.RID)
}
}
if len(ws.Hyperlinks.Hyperlink) == 0 {
ws.Hyperlinks = nil
}
return nil
}
// SetCellHyperLink provides a function to set cell hyperlink by given // SetCellHyperLink provides a function to set cell hyperlink by given
// worksheet name and link URL address. LinkType defines two types of // worksheet name and link URL address. LinkType defines three types of
// hyperlink "External" for website or "Location" for moving to one of cell in // hyperlink "External" for website or "Location" for moving to one of cell in
// this workbook. Maximum limit hyperlinks in a worksheet is 65530. This // this workbook or "None" for remove hyperlink. Maximum limit hyperlinks in a
// function is only used to set the hyperlink of the cell and doesn't affect // worksheet is 65530. This function is only used to set the hyperlink of the
// the value of the cell. If you need to set the value of the cell, please use // cell and doesn't affect the value of the cell. If you need to set the value
// the other functions such as `SetCellStyle` or `SetSheetRow`. The below is // of the cell, please use the other functions such as `SetCellStyle` or
// example for external link. // `SetSheetRow`. The below is example for external link.
// //
// display, tooltip := "https://github.com/xuri/excelize", "Excelize on GitHub" // display, tooltip := "https://github.com/xuri/excelize", "Excelize on GitHub"
// if err := f.SetCellHyperLink("Sheet1", "A3", // if err := f.SetCellHyperLink("Sheet1", "A3",
@ -1032,6 +1054,8 @@ func (f *File) SetCellHyperLink(sheet, cell, link, linkType string, opts ...Hype
Ref: cell, Ref: cell,
Location: link, Location: link,
} }
case "None":
return f.removeHyperLink(ws, sheet, cell)
default: default:
return newInvalidLinkTypeError(linkType) return newInvalidLinkTypeError(linkType)
} }

View File

@ -455,6 +455,18 @@ func TestSetCellHyperLink(t *testing.T) {
assert.Equal(t, link, true) assert.Equal(t, link, true)
assert.Equal(t, "https://github.com/xuri/excelize", target) assert.Equal(t, "https://github.com/xuri/excelize", target)
assert.NoError(t, err) assert.NoError(t, err)
// Test remove hyperlink for a cell
f = NewFile()
assert.NoError(t, f.SetCellHyperLink("Sheet1", "A1", "Sheet1!D8", "Location"))
ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
assert.True(t, ok)
ws.(*xlsxWorksheet).Hyperlinks.Hyperlink[0].Ref = "A1:D4"
assert.NoError(t, f.SetCellHyperLink("Sheet1", "B2", "", "None"))
// Test remove hyperlink for a cell with invalid cell reference
assert.NoError(t, f.SetCellHyperLink("Sheet1", "A1", "Sheet1!D8", "Location"))
ws.(*xlsxWorksheet).Hyperlinks.Hyperlink[0].Ref = "A:A"
assert.Error(t, f.SetCellHyperLink("Sheet1", "B2", "", "None"), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")))
} }
func TestGetCellHyperLink(t *testing.T) { func TestGetCellHyperLink(t *testing.T) {