Skip to content

Commit 591819d

Browse files
committed
feat: data/table: AutoLink URLs
1 parent 46e9192 commit 591819d

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

data/table/table.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ var DebugReadCSV = false // should not need to use this.
1010

1111
// Table is useful for working on CSV data
1212
type Table struct {
13-
Name string
14-
Columns []string
15-
Records [][]string
16-
FormatMap map[int]string
17-
FormatFunc func(val string, colIdx uint) (interface{}, error)
18-
ID string
19-
Class string
20-
Style string
13+
Name string
14+
Columns []string
15+
Records [][]string
16+
FormatMap map[int]string
17+
FormatFunc func(val string, colIdx uint) (interface{}, error)
18+
FormatAutoLink bool
19+
ID string
20+
Class string
21+
Style string
2122
}
2223

2324
func NewTable() Table {

data/table/write.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io/ioutil"
77
"os"
8+
"regexp"
89
"strconv"
910
"strings"
1011
"time"
@@ -146,6 +147,8 @@ func (tbl *Table) FormatterFunc() func(val string, colIdx uint) (interface{}, er
146147
}
147148
}
148149

150+
var rxUrlHttpOrHttps = regexp.MustCompile(`^(i?)https?://.`)
151+
149152
// WriteXLSX writes a table as an Excel XLSX file with
150153
// row formatter option.
151154
func WriteXLSX(path string, tables ...*Table) error {
@@ -180,6 +183,11 @@ func WriteXLSX(path string, tables ...*Table) error {
180183
return errors.Wrap(err, "gocharts/data/tables/write.go/WriteXLSXFormatted.Error.FormatCellValue")
181184
}
182185
f.SetCellValue(sheetname, cellLocation, formattedVal)
186+
if tbl.FormatAutoLink {
187+
if rxUrlHttpOrHttps.MatchString(cellValue) {
188+
f.SetCellHyperLink(sheetname, cellLocation, cellValue, "External")
189+
}
190+
}
183191
}
184192
}
185193
// Set active sheet of the workbook.

0 commit comments

Comments
 (0)