Skip to content

Commit 308776e

Browse files
committed
Optimize code, go test and godoc updated.
1 parent 1d54bd4 commit 308776e

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

cell.go

+16-20
Original file line numberDiff line numberDiff line change
@@ -215,34 +215,30 @@ func (f *File) SetCellFormula(sheet, axis, formula string) {
215215
// style, _ := xlsx.NewStyle(`{"font":{"color":"#1265BE","underline":"single"}}`)
216216
// xlsx.SetCellStyle("Sheet1", "A3", "A3", style)
217217
//
218-
// A this is another example for "Location"
218+
// A this is another example for "Location":
219219
//
220220
// xlsx.SetCellHyperLink("Sheet1", "A3", "Sheet1!A40", "Location")
221+
//
221222
func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) {
222223
xlsx := f.workSheetReader(sheet)
223224
axis = f.mergeCellsParser(xlsx, axis)
224-
var hyperlink xlsxHyperlink
225+
linkTypes := map[string]xlsxHyperlink{
226+
"External": {},
227+
"Location": {Location: link},
228+
}
229+
hyperlink, ok := linkTypes[linkType]
230+
if !ok || axis == "" {
231+
return
232+
}
233+
hyperlink.Ref = axis
225234
if linkType == "External" {
226-
rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
227-
hyperlink = xlsxHyperlink{
228-
Ref: axis,
229-
RID: "rId" + strconv.Itoa(rID),
230-
}
231-
} else if linkType == "Location" {
232-
hyperlink = xlsxHyperlink{
233-
Ref: axis,
234-
Location: link,
235-
}
235+
rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, linkType)
236+
hyperlink.RID = "rId" + strconv.Itoa(rID)
236237
}
237-
if hyperlink.Ref == "" {
238-
panic("linkType only support External and Location now")
239-
} else if xlsx.Hyperlinks != nil {
240-
xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
241-
} else {
242-
hyperlinks := xlsxHyperlinks{}
243-
hyperlinks.Hyperlink = append(hyperlinks.Hyperlink, hyperlink)
244-
xlsx.Hyperlinks = &hyperlinks
238+
if xlsx.Hyperlinks == nil {
239+
xlsx.Hyperlinks = &xlsxHyperlinks{}
245240
}
241+
xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
246242
}
247243

248244
// MergeCell provides function to merge cells by given coordinate area and sheet

excelize_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ func TestSetCellHyperLink(t *testing.T) {
213213
xlsx.SetCellHyperLink("sheet1", "B19", "https://github.com/xuri/excelize", "External")
214214
// Test add first hyperlink in a work sheet.
215215
xlsx.SetCellHyperLink("sheet2", "C1", "https://github.com/xuri/excelize", "External")
216+
// Test add Location hyperlink in a work sheet.
217+
xlsx.SetCellHyperLink("sheet2", "D6", "Sheet1!D8", "Location")
218+
xlsx.SetCellHyperLink("sheet2", "C3", "Sheet1!D8", "")
219+
xlsx.SetCellHyperLink("sheet2", "", "Sheet1!D60", "Location")
216220
err = xlsx.Save()
217221
if err != nil {
218222
t.Log(err)

sheet.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ func parseFormatPanesSet(formatSet string) *formatPanes {
547547
// | regions. In that case, this value specifies the right
548548
// | pane.
549549
//
550-
// Pane state type is restricted to the values supported currently listed in the following table:
550+
// Pane state type is restricted to the values supported currently listed in the following table:
551551
//
552552
// Enumeration Value | Description
553553
// --------------------------------+-------------------------------------------------------------

table.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,11 @@ func (f *File) addSheetTable(sheet string, rID int) {
8888
table := &xlsxTablePart{
8989
RID: "rId" + strconv.Itoa(rID),
9090
}
91-
if xlsx.TableParts != nil {
92-
xlsx.TableParts.Count++
93-
xlsx.TableParts.TableParts = append(xlsx.TableParts.TableParts, table)
94-
} else {
95-
xlsx.TableParts = &xlsxTableParts{
96-
Count: 1,
97-
TableParts: []*xlsxTablePart{table},
98-
}
91+
if xlsx.TableParts == nil {
92+
xlsx.TableParts = &xlsxTableParts{}
9993
}
100-
94+
xlsx.TableParts.Count++
95+
xlsx.TableParts.TableParts = append(xlsx.TableParts.TableParts, table)
10196
}
10297

10398
// addTable provides function to add table by given sheet index, coordinate area

0 commit comments

Comments
 (0)