-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.go
More file actions
92 lines (80 loc) · 2.41 KB
/
table.go
File metadata and controls
92 lines (80 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Package table provides functions to create and render xlsx tables.
It is built atop of the tealeg/xlsx (https://github.com/tealeg/xlsx) package.
*/
package table
import (
"fmt"
"github.com/tealeg/xlsx/v3"
)
// Table represents a table in a xlsx file.
type Table struct {
file *xlsx.File // file is the xlsx file.
sheets []*Sheet // sheets is the list of sheets in the file.
sheetsMap map[string]*Sheet // sheetsMap is a map of sheets in the file.
}
// TableRenderOpts are the options for rendering a table.
type TableRenderOpts struct {
SetAutoColWidth bool // SetAutoColWidth sets the automatic column width for the specified columns during rendering.
}
// NewTable creates a new table.
func NewTable() *Table {
return &Table{file: xlsx.NewFile(), sheets: []*Sheet{}, sheetsMap: map[string]*Sheet{}}
}
// Save writes the table to the specified path.
func (t *Table) Save(path string) error {
return t.file.Save(path)
}
// AddSheet adds a sheet to the table.
func (t *Table) AddSheet(name string) (*Sheet, error) {
if _, ok := t.sheetsMap[name]; ok {
return nil, fmt.Errorf("sheet %s already exists", name)
}
sheet := &Sheet{name: name, rows: []*Row{}}
t.sheetsMap[name] = sheet
t.sheets = append(t.sheets, sheet)
return sheet, nil
}
// GetSheetFromName returns a sheet with the specified name from the table.
func (t *Table) GetSheetFromName(name string) (*Sheet, error) {
sh, ok := t.sheetsMap[name]
if !ok {
return nil, fmt.Errorf("sheet %s not found", name)
}
return sh, nil
}
// GetSheetFromIndex returns a sheet with the specified index from the table.
func (t *Table) GetSheetFromIndex(index int) (*Sheet, error) {
if index > len(t.sheets) || index < 0 {
return nil, fmt.Errorf("sheet #%d out of range", index)
}
return t.sheets[index], nil
}
// Render renders the table.
func (t *Table) Render(tableRenderOpts ...*TableRenderOpts) error {
var opt *TableRenderOpts
if len(tableRenderOpts) > 1 {
opt = tableRenderOpts[0]
} else {
opt = &TableRenderOpts{SetAutoColWidth: false}
}
for _, sh := range t.sheets {
sheet, err := sh.render()
if err != nil {
return err
}
if opt.SetAutoColWidth {
for i := 1; i <= sheet.MaxCol; i++ {
if err := sheet.SetColAutoWidth(i, func(s string) float64 {
return float64(len(s))
}); err != nil {
return err
}
}
}
if _, err := t.file.AppendSheet(*sheet, sh.name); err != nil {
return err
}
}
return nil
}