forked from qax-os/excelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheetpr.go
259 lines (253 loc) · 7.63 KB
/
sheetpr.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
// Package excelize providing a set of functions that allow you to write to and
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
// Supports complex components by high compatibility, and provided streaming
// API for generating or reading data from a worksheet with huge amounts of
// data. This library needs Go version 1.15 or later.
package excelize
// SetPageMargins provides a function to set worksheet page margins.
func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error {
ws, err := f.workSheetReader(sheet)
if err != nil {
return err
}
if opts == nil {
return err
}
preparePageMargins := func(ws *xlsxWorksheet) {
if ws.PageMargins == nil {
ws.PageMargins = new(xlsxPageMargins)
}
}
preparePrintOptions := func(ws *xlsxWorksheet) {
if ws.PrintOptions == nil {
ws.PrintOptions = new(xlsxPrintOptions)
}
}
if opts.Bottom != nil {
preparePageMargins(ws)
ws.PageMargins.Bottom = *opts.Bottom
}
if opts.Footer != nil {
preparePageMargins(ws)
ws.PageMargins.Footer = *opts.Footer
}
if opts.Header != nil {
preparePageMargins(ws)
ws.PageMargins.Header = *opts.Header
}
if opts.Left != nil {
preparePageMargins(ws)
ws.PageMargins.Left = *opts.Left
}
if opts.Right != nil {
preparePageMargins(ws)
ws.PageMargins.Right = *opts.Right
}
if opts.Top != nil {
preparePageMargins(ws)
ws.PageMargins.Top = *opts.Top
}
if opts.Horizontally != nil {
preparePrintOptions(ws)
ws.PrintOptions.HorizontalCentered = *opts.Horizontally
}
if opts.Vertically != nil {
preparePrintOptions(ws)
ws.PrintOptions.VerticalCentered = *opts.Vertically
}
return err
}
// GetPageMargins provides a function to get worksheet page margins.
func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error) {
opts := PageLayoutMarginsOptions{
Bottom: float64Ptr(0.75),
Footer: float64Ptr(0.3),
Header: float64Ptr(0.3),
Left: float64Ptr(0.7),
Right: float64Ptr(0.7),
Top: float64Ptr(0.75),
}
ws, err := f.workSheetReader(sheet)
if err != nil {
return opts, err
}
if ws.PageMargins != nil {
if ws.PageMargins.Bottom != 0 {
opts.Bottom = float64Ptr(ws.PageMargins.Bottom)
}
if ws.PageMargins.Footer != 0 {
opts.Footer = float64Ptr(ws.PageMargins.Footer)
}
if ws.PageMargins.Header != 0 {
opts.Header = float64Ptr(ws.PageMargins.Header)
}
if ws.PageMargins.Left != 0 {
opts.Left = float64Ptr(ws.PageMargins.Left)
}
if ws.PageMargins.Right != 0 {
opts.Right = float64Ptr(ws.PageMargins.Right)
}
if ws.PageMargins.Top != 0 {
opts.Top = float64Ptr(ws.PageMargins.Top)
}
}
if ws.PrintOptions != nil {
opts.Horizontally = boolPtr(ws.PrintOptions.HorizontalCentered)
opts.Vertically = boolPtr(ws.PrintOptions.VerticalCentered)
}
return opts, err
}
// setSheetProps set worksheet format properties by given options.
func (ws *xlsxWorksheet) setSheetProps(opts *SheetPropsOptions) {
prepareSheetPr := func(ws *xlsxWorksheet) {
if ws.SheetPr == nil {
ws.SheetPr = new(xlsxSheetPr)
}
}
preparePageSetUpPr := func(ws *xlsxWorksheet) {
prepareSheetPr(ws)
if ws.SheetPr.PageSetUpPr == nil {
ws.SheetPr.PageSetUpPr = new(xlsxPageSetUpPr)
}
}
prepareOutlinePr := func(ws *xlsxWorksheet) {
prepareSheetPr(ws)
if ws.SheetPr.OutlinePr == nil {
ws.SheetPr.OutlinePr = new(xlsxOutlinePr)
}
}
prepareTabColor := func(ws *xlsxWorksheet) {
prepareSheetPr(ws)
if ws.SheetPr.TabColor == nil {
ws.SheetPr.TabColor = new(xlsxTabColor)
}
}
if opts.CodeName != nil {
prepareSheetPr(ws)
ws.SheetPr.CodeName = *opts.CodeName
}
if opts.EnableFormatConditionsCalculation != nil {
prepareSheetPr(ws)
ws.SheetPr.EnableFormatConditionsCalculation = opts.EnableFormatConditionsCalculation
}
if opts.Published != nil {
prepareSheetPr(ws)
ws.SheetPr.Published = opts.Published
}
if opts.AutoPageBreaks != nil {
preparePageSetUpPr(ws)
ws.SheetPr.PageSetUpPr.AutoPageBreaks = *opts.AutoPageBreaks
}
if opts.FitToPage != nil {
preparePageSetUpPr(ws)
ws.SheetPr.PageSetUpPr.FitToPage = *opts.FitToPage
}
if opts.OutlineSummaryBelow != nil {
prepareOutlinePr(ws)
ws.SheetPr.OutlinePr.SummaryBelow = *opts.OutlineSummaryBelow
}
if opts.TabColorIndexed != nil {
prepareTabColor(ws)
ws.SheetPr.TabColor.Indexed = *opts.TabColorIndexed
}
if opts.TabColorRGB != nil {
prepareTabColor(ws)
ws.SheetPr.TabColor.RGB = *opts.TabColorRGB
}
if opts.TabColorTheme != nil {
prepareTabColor(ws)
ws.SheetPr.TabColor.Theme = *opts.TabColorTheme
}
if opts.TabColorTint != nil {
prepareTabColor(ws)
ws.SheetPr.TabColor.Tint = *opts.TabColorTint
}
}
// SetSheetProps provides a function to set worksheet properties.
func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error {
ws, err := f.workSheetReader(sheet)
if err != nil {
return err
}
if opts == nil {
return err
}
ws.setSheetProps(opts)
if ws.SheetFormatPr == nil {
ws.SheetFormatPr = &xlsxSheetFormatPr{DefaultRowHeight: defaultRowHeight}
}
if opts.BaseColWidth != nil {
ws.SheetFormatPr.BaseColWidth = *opts.BaseColWidth
}
if opts.DefaultColWidth != nil {
ws.SheetFormatPr.DefaultColWidth = *opts.DefaultColWidth
}
if opts.DefaultRowHeight != nil {
ws.SheetFormatPr.DefaultRowHeight = *opts.DefaultRowHeight
}
if opts.CustomHeight != nil {
ws.SheetFormatPr.CustomHeight = *opts.CustomHeight
}
if opts.ZeroHeight != nil {
ws.SheetFormatPr.ZeroHeight = *opts.ZeroHeight
}
if opts.ThickTop != nil {
ws.SheetFormatPr.ThickTop = *opts.ThickTop
}
if opts.ThickBottom != nil {
ws.SheetFormatPr.ThickBottom = *opts.ThickBottom
}
return err
}
// GetSheetProps provides a function to get worksheet properties.
func (f *File) GetSheetProps(sheet string) (SheetPropsOptions, error) {
baseColWidth := uint8(8)
opts := SheetPropsOptions{
EnableFormatConditionsCalculation: boolPtr(true),
Published: boolPtr(true),
AutoPageBreaks: boolPtr(true),
OutlineSummaryBelow: boolPtr(true),
BaseColWidth: &baseColWidth,
}
ws, err := f.workSheetReader(sheet)
if err != nil {
return opts, err
}
if ws.SheetPr != nil {
opts.CodeName = stringPtr(ws.SheetPr.CodeName)
if ws.SheetPr.EnableFormatConditionsCalculation != nil {
opts.EnableFormatConditionsCalculation = ws.SheetPr.EnableFormatConditionsCalculation
}
if ws.SheetPr.Published != nil {
opts.Published = ws.SheetPr.Published
}
if ws.SheetPr.PageSetUpPr != nil {
opts.AutoPageBreaks = boolPtr(ws.SheetPr.PageSetUpPr.AutoPageBreaks)
opts.FitToPage = boolPtr(ws.SheetPr.PageSetUpPr.FitToPage)
}
if ws.SheetPr.OutlinePr != nil {
opts.OutlineSummaryBelow = boolPtr(ws.SheetPr.OutlinePr.SummaryBelow)
}
if ws.SheetPr.TabColor != nil {
opts.TabColorIndexed = intPtr(ws.SheetPr.TabColor.Indexed)
opts.TabColorRGB = stringPtr(ws.SheetPr.TabColor.RGB)
opts.TabColorTheme = intPtr(ws.SheetPr.TabColor.Theme)
opts.TabColorTint = float64Ptr(ws.SheetPr.TabColor.Tint)
}
}
if ws.SheetFormatPr != nil {
opts.BaseColWidth = &ws.SheetFormatPr.BaseColWidth
opts.DefaultColWidth = float64Ptr(ws.SheetFormatPr.DefaultColWidth)
opts.DefaultRowHeight = float64Ptr(ws.SheetFormatPr.DefaultRowHeight)
opts.CustomHeight = boolPtr(ws.SheetFormatPr.CustomHeight)
opts.ZeroHeight = boolPtr(ws.SheetFormatPr.ZeroHeight)
opts.ThickTop = boolPtr(ws.SheetFormatPr.ThickTop)
opts.ThickBottom = boolPtr(ws.SheetFormatPr.ThickBottom)
}
return opts, err
}