-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet.go
108 lines (96 loc) · 2.56 KB
/
sheet.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package exportToExcel
import (
"errors"
"github.com/xuri/excelize/v2"
"reflect"
)
func newSheet(file *excelize.File, sheetName string, baseDataType any, opts ...Option) *Sheet {
var a = &Sheet{file: file, baseDataType: baseDataType}
a.Title = NewTitle(a)
a.sheetId, _ = file.NewSheet(sheetName)
a.titleStyle = DefaultTitleStyle
a.dataStyle = DefaultDataStyle
a.minColWidth = DefaultColMinWidth
a.maxColWidth = DefaultColMaxWidth
for _, opt := range opts {
opt(a)
}
return a
}
type Sheet struct {
Title Title
fieldSort []string
file *excelize.File
sheetId int
rowNum int
titleStyle func() *excelize.Style
dataStyle func() *excelize.Style
data any
baseDataType any //the base type of the data, it used to search appropriate writer(IDataWriter.Supported)
firstEmptyRow int
minColWidth float64
maxColWidth float64
}
func (s *Sheet) SetSheetName(sheetName string) {
_ = s.file.SetSheetName(s.file.GetSheetName(s.sheetId), sheetName)
}
func (s *Sheet) FillData(data any) error {
s.data = data
if s.file == nil {
return errors.New("file object is Empty!")
}
s.firstEmptyRow = GetFirstEmptyRowIndex(s.file, s.SheetName())
s.rowNum = s.firstEmptyRow
dataType := reflect.TypeOf(data)
switch dataType.Kind() {
case reflect.Slice, reflect.Array:
value := reflect.ValueOf(data)
s.rowNum += value.Len()
default:
s.rowNum += 1
}
return writers.WriteData(s)
}
func (s *Sheet) SetDataType(t any) {
s.baseDataType = t
}
func (s *Sheet) SetFieldSort(fieldSort []string) {
s.fieldSort = fieldSort
}
func (s *Sheet) SheetName() string {
return s.file.GetSheetName(s.sheetId)
}
func (s *Sheet) Data() any {
return s.data
}
func (s *Sheet) TitleStyle() func() *excelize.Style {
return s.titleStyle
}
func (s *Sheet) DataStyle() func() *excelize.Style {
return s.dataStyle
}
func (s *Sheet) SetTitleStyle(style func() *excelize.Style) {
s.titleStyle = style
}
func (s *Sheet) SetDataStyle(style func() *excelize.Style) {
s.dataStyle = style
}
func (s *Sheet) MinColWidth() float64 {
return s.minColWidth
}
func (s *Sheet) SetMinColWidth(minColWidth float64) {
s.minColWidth = minColWidth
}
func (s *Sheet) SetMaxColWidth(maxColWidth float64) {
s.maxColWidth = maxColWidth
}
func (s *Sheet) MaxColWidth() float64 {
return s.maxColWidth
}
// The data is written to the table in the order of these fields
func (s *Sheet) Fields(recalculate ...bool) []string {
if (s.fieldSort == nil && s.baseDataType != nil) || (len(recalculate) == 1 && recalculate[0] == true) {
return writers.FieldSort(s)
}
return s.fieldSort
}