-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.go
74 lines (64 loc) · 1.6 KB
/
file.go
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
package xlsxtra
import (
"fmt"
"github.com/tealeg/xlsx"
)
// File extends xlsx.File
type File struct {
*xlsx.File
filename string
}
// NewFile creates new spreadsheet file
func NewFile() *File {
return &File{File: xlsx.NewFile(), filename: "?"}
}
// OpenFile opens a new excel file
func OpenFile(fn string) (*File, error) {
f, err := xlsx.OpenFile(fn)
if err != nil {
return nil, fmt.Errorf("OpenFile: %v", err)
}
return &File{File: f, filename: fn}, nil
}
// AddSheet with certain name to spreadsheet file
func (f *File) AddSheet(name string) (*Sheet, error) {
sheet, err := f.File.AddSheet(name)
if err != nil {
return nil, fmt.Errorf("AddSheet: %v", err)
}
return &Sheet{Sheet: sheet}, nil
}
// SheetByName get sheet by name from spreadsheet
func (f *File) SheetByName(name string) (*Sheet, error) {
sheet, ok := f.Sheet[name]
if !ok {
return nil, fmt.Errorf(
"SheetByName(%q): file %q does not contain this sheet",
name, f.filename)
}
return &Sheet{Sheet: sheet}, nil
}
// SheetByIndex get sheet by index from spreadsheet
func (f *File) SheetByIndex(index int) *Sheet {
return &Sheet{f.Sheets[index]}
}
// SheetRange returns sheet range including end sheet.
// Negative indices can be used.
func (f *File) SheetRange(start, end int) []*Sheet {
n := len(f.Sheets)
if start < 0 {
start += n
}
if end <= 0 {
end += n
}
return Sheets(f.Sheets[start : end+1])
}
// SheetMap returns a map of sheets by name
func (f *File) SheetMap() map[string]*Sheet {
sheetMap := make(map[string]*Sheet)
for _, sheet := range f.Sheets {
sheetMap[sheet.Name] = &Sheet{Sheet: sheet}
}
return sheetMap
}