-
Notifications
You must be signed in to change notification settings - Fork 0
/
title.go
197 lines (179 loc) · 4.32 KB
/
title.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package exportToExcel
import (
"github.com/xuri/excelize/v2"
)
func NewTitle(sheet *Sheet) Title {
return Title{sheet: sheet, FontScaleFactor: 2, BaseFontSize: 12, MaxFontSize: 20, MaxLevel: 5}
}
type Title struct {
sheet *Sheet
titles []*TitleItem
MaxLevel int
FontScaleFactor float64
BaseFontSize float64
MaxFontSize float64
occupiedRow []int
colNum int
}
// Gen
// generate a TitleItem
func (t *Title) Gen(titles ...*TitleItem) (err error) {
for _, title := range titles {
//insert empty row
if len(t.occupiedRow) > 0 {
for thisRow := title.InitRow; thisRow < title.MergeRowTo; thisRow++ {
var sameRowExisted bool
for _, occRow := range t.occupiedRow {
if occRow == thisRow {
sameRowExisted = true
break
}
}
if !sameRowExisted {
t.sheet.rowNum++
_ = t.sheet.file.InsertRows(t.sheet.SheetName(), title.InitRow, 1)
t.occupiedRow = append(t.occupiedRow, thisRow)
}
}
} else if t.sheet.rowNum > title.InitRow {
_ = t.sheet.file.InsertRows(t.sheet.SheetName(), title.InitRow, title.MergeRowTo-title.InitRow+1)
}
title.dataCheck()
var (
cellL = GetCellCoord(title.InitRow, title.InitCol)
cellR = GetCellCoord(title.MergeRowTo, title.MergeColTo)
)
//set text
if err = t.sheet.file.SetCellStr(t.sheet.SheetName(), cellL, title.Text); err != nil {
return err
}
//Merge cells
if title.MergeColTo != 0 && title.MergeRowTo != 0 && (title.MergeRowTo != title.InitRow || title.MergeColTo != title.InitCol) {
if err = t.sheet.file.MergeCell(t.sheet.SheetName(), cellL, cellR); err != nil {
return err
}
}
//set Style
if title.style == nil {
title.style = t.sheet.titleStyle()
title.style.Font.Size = t.setFontSize(title.Level)
}
headerStyleID, errs := t.sheet.file.NewStyle(title.style)
if errs != nil {
return
}
if err = t.sheet.file.SetCellStyle(t.sheet.SheetName(), cellL, cellR, headerStyleID); err != nil {
return
}
colNum := title.MergeColTo - title.InitCol + 1
if t.colNum < colNum {
t.colNum = colNum
}
t.titles = append(t.titles, title)
}
return nil
}
// setFontSize
func (t *Title) setFontSize(titleLevel int) float64 {
if titleLevel > t.MaxLevel || titleLevel < 0 {
return t.BaseFontSize
}
fontSize := t.MaxFontSize - float64(titleLevel-1)*t.FontScaleFactor
if fontSize < t.BaseFontSize {
return t.BaseFontSize
}
return fontSize
}
// NewTitleItem
func (t *Title) NewTitleItem(level int, text string, initRow, initCol int, style ...*excelize.Style) *TitleItem {
tm := &TitleItem{
Level: level,
Text: text,
InitRow: initRow,
InitCol: initCol,
sheet: t.sheet,
}
tm.dataCheck()
if style != nil {
tm.style = style[0]
}
t.titles = append(t.titles, tm)
tm.id = len(t.titles) - 1
return tm
}
type TitleItem struct {
id int
Level int
Text string
InitRow int //所在行
InitCol int //default is 1
MergeColTo int
MergeRowTo int
style *excelize.Style
sheet *Sheet
}
// GetId
func (t *TitleItem) GetId() int {
return t.id
}
// SetStyle
// By default, the style is set automatically based on the title level.
func (t *TitleItem) SetStyle(style *excelize.Style) *TitleItem {
t.style = style
return t
}
// SetMergeColNum
func (t *TitleItem) SetMergeColNum(num int) *TitleItem {
t.MergeColTo = t.InitCol + num - 1
return t
}
// SetMergeRowNum
func (t *TitleItem) SetMergeRowNum(num int) *TitleItem {
t.MergeRowTo = t.InitRow + num - 1
return t
}
// SetMergeColTo
func (t *TitleItem) SetMergeColTo(colTo int) *TitleItem {
t.MergeColTo = colTo
return t
}
// SetMergeRowTo
func (t *TitleItem) SetMergeRowTo(colTo int) *TitleItem {
t.MergeColTo = colTo
return t
}
// SetMergeRowTo
func (t *TitleItem) SetMergeTo(row, col int) *TitleItem {
t.MergeRowTo = row
t.MergeColTo = col
return t
}
// SetFullHorizontalMerge
func (t *TitleItem) SetFullHorizontalMerge(defaultColNum ...int) *TitleItem {
t.InitCol = 1
if defaultColNum != nil {
t.MergeColTo = defaultColNum[0]
} else {
t.MergeColTo = len(t.sheet.Fields())
t.dataCheck()
}
return t
}
// dataCheck
func (t *TitleItem) dataCheck() {
if t.InitCol == 0 {
t.InitCol = 1
}
if t.InitRow == 0 {
t.InitRow = 1
}
if t.MergeRowTo == 0 {
t.MergeRowTo = t.InitRow
}
if t.MergeColTo == 0 {
t.MergeColTo = t.InitCol
}
if t.Level == 0 {
t.Level = 5
}
}