forked from qax-os/excelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheetpr_test.go
309 lines (276 loc) · 8.99 KB
/
sheetpr_test.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package excelize_test
import (
"fmt"
"testing"
"github.com/mohae/deepcopy"
"github.com/stretchr/testify/assert"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
var _ = []excelize.SheetPrOption{
excelize.CodeName("hello"),
excelize.EnableFormatConditionsCalculation(false),
excelize.Published(false),
excelize.FitToPage(true),
excelize.AutoPageBreaks(true),
excelize.OutlineSummaryBelow(true),
}
var _ = []excelize.SheetPrOptionPtr{
(*excelize.CodeName)(nil),
(*excelize.EnableFormatConditionsCalculation)(nil),
(*excelize.Published)(nil),
(*excelize.FitToPage)(nil),
(*excelize.AutoPageBreaks)(nil),
(*excelize.OutlineSummaryBelow)(nil),
}
func ExampleFile_SetSheetPrOptions() {
f := excelize.NewFile()
const sheet = "Sheet1"
if err := f.SetSheetPrOptions(sheet,
excelize.CodeName("code"),
excelize.EnableFormatConditionsCalculation(false),
excelize.Published(false),
excelize.FitToPage(true),
excelize.AutoPageBreaks(true),
excelize.OutlineSummaryBelow(false),
); err != nil {
panic(err)
}
// Output:
}
func ExampleFile_GetSheetPrOptions() {
f := excelize.NewFile()
const sheet = "Sheet1"
var (
codeName excelize.CodeName
enableFormatConditionsCalculation excelize.EnableFormatConditionsCalculation
published excelize.Published
fitToPage excelize.FitToPage
autoPageBreaks excelize.AutoPageBreaks
outlineSummaryBelow excelize.OutlineSummaryBelow
)
if err := f.GetSheetPrOptions(sheet,
&codeName,
&enableFormatConditionsCalculation,
&published,
&fitToPage,
&autoPageBreaks,
&outlineSummaryBelow,
); err != nil {
panic(err)
}
fmt.Println("Defaults:")
fmt.Printf("- codeName: %q\n", codeName)
fmt.Println("- enableFormatConditionsCalculation:", enableFormatConditionsCalculation)
fmt.Println("- published:", published)
fmt.Println("- fitToPage:", fitToPage)
fmt.Println("- autoPageBreaks:", autoPageBreaks)
fmt.Println("- outlineSummaryBelow:", outlineSummaryBelow)
// Output:
// Defaults:
// - codeName: ""
// - enableFormatConditionsCalculation: true
// - published: true
// - fitToPage: false
// - autoPageBreaks: false
// - outlineSummaryBelow: true
}
func TestSheetPrOptions(t *testing.T) {
const sheet = "Sheet1"
testData := []struct {
container excelize.SheetPrOptionPtr
nonDefault excelize.SheetPrOption
}{
{new(excelize.CodeName), excelize.CodeName("xx")},
{new(excelize.EnableFormatConditionsCalculation), excelize.EnableFormatConditionsCalculation(false)},
{new(excelize.Published), excelize.Published(false)},
{new(excelize.FitToPage), excelize.FitToPage(true)},
{new(excelize.AutoPageBreaks), excelize.AutoPageBreaks(true)},
{new(excelize.OutlineSummaryBelow), excelize.OutlineSummaryBelow(false)},
}
for i, test := range testData {
t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
opt := test.nonDefault
t.Logf("option %T", opt)
def := deepcopy.Copy(test.container).(excelize.SheetPrOptionPtr)
val1 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
val2 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
f := excelize.NewFile()
// Get the default value
assert.NoError(t, f.GetSheetPrOptions(sheet, def), opt)
// Get again and check
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
if !assert.Equal(t, val1, def, opt) {
t.FailNow()
}
// Set the same value
assert.NoError(t, f.SetSheetPrOptions(sheet, val1), opt)
// Get again and check
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
t.FailNow()
}
// Set a different value
assert.NoError(t, f.SetSheetPrOptions(sheet, test.nonDefault), opt)
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
// Get again and compare
assert.NoError(t, f.GetSheetPrOptions(sheet, val2), opt)
if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
t.FailNow()
}
// Value should not be the same as the default
if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
t.FailNow()
}
// Restore the default value
assert.NoError(t, f.SetSheetPrOptions(sheet, def), opt)
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
if !assert.Equal(t, def, val1) {
t.FailNow()
}
})
}
}
func TestSetSheetrOptions(t *testing.T) {
f := excelize.NewFile()
// Test SetSheetrOptions on not exists worksheet.
assert.EqualError(t, f.SetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
}
func TestGetSheetPrOptions(t *testing.T) {
f := excelize.NewFile()
// Test GetSheetPrOptions on not exists worksheet.
assert.EqualError(t, f.GetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
}
var _ = []excelize.PageMarginsOptions{
excelize.PageMarginBottom(1.0),
excelize.PageMarginFooter(1.0),
excelize.PageMarginHeader(1.0),
excelize.PageMarginLeft(1.0),
excelize.PageMarginRight(1.0),
excelize.PageMarginTop(1.0),
}
var _ = []excelize.PageMarginsOptionsPtr{
(*excelize.PageMarginBottom)(nil),
(*excelize.PageMarginFooter)(nil),
(*excelize.PageMarginHeader)(nil),
(*excelize.PageMarginLeft)(nil),
(*excelize.PageMarginRight)(nil),
(*excelize.PageMarginTop)(nil),
}
func ExampleFile_SetPageMargins() {
f := excelize.NewFile()
const sheet = "Sheet1"
if err := f.SetPageMargins(sheet,
excelize.PageMarginBottom(1.0),
excelize.PageMarginFooter(1.0),
excelize.PageMarginHeader(1.0),
excelize.PageMarginLeft(1.0),
excelize.PageMarginRight(1.0),
excelize.PageMarginTop(1.0),
); err != nil {
panic(err)
}
// Output:
}
func ExampleFile_GetPageMargins() {
f := excelize.NewFile()
const sheet = "Sheet1"
var (
marginBottom excelize.PageMarginBottom
marginFooter excelize.PageMarginFooter
marginHeader excelize.PageMarginHeader
marginLeft excelize.PageMarginLeft
marginRight excelize.PageMarginRight
marginTop excelize.PageMarginTop
)
if err := f.GetPageMargins(sheet,
&marginBottom,
&marginFooter,
&marginHeader,
&marginLeft,
&marginRight,
&marginTop,
); err != nil {
panic(err)
}
fmt.Println("Defaults:")
fmt.Println("- marginBottom:", marginBottom)
fmt.Println("- marginFooter:", marginFooter)
fmt.Println("- marginHeader:", marginHeader)
fmt.Println("- marginLeft:", marginLeft)
fmt.Println("- marginRight:", marginRight)
fmt.Println("- marginTop:", marginTop)
// Output:
// Defaults:
// - marginBottom: 0.75
// - marginFooter: 0.3
// - marginHeader: 0.3
// - marginLeft: 0.7
// - marginRight: 0.7
// - marginTop: 0.75
}
func TestPageMarginsOption(t *testing.T) {
const sheet = "Sheet1"
testData := []struct {
container excelize.PageMarginsOptionsPtr
nonDefault excelize.PageMarginsOptions
}{
{new(excelize.PageMarginTop), excelize.PageMarginTop(1.0)},
{new(excelize.PageMarginBottom), excelize.PageMarginBottom(1.0)},
{new(excelize.PageMarginLeft), excelize.PageMarginLeft(1.0)},
{new(excelize.PageMarginRight), excelize.PageMarginRight(1.0)},
{new(excelize.PageMarginHeader), excelize.PageMarginHeader(1.0)},
{new(excelize.PageMarginFooter), excelize.PageMarginFooter(1.0)},
}
for i, test := range testData {
t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
opt := test.nonDefault
t.Logf("option %T", opt)
def := deepcopy.Copy(test.container).(excelize.PageMarginsOptionsPtr)
val1 := deepcopy.Copy(def).(excelize.PageMarginsOptionsPtr)
val2 := deepcopy.Copy(def).(excelize.PageMarginsOptionsPtr)
f := excelize.NewFile()
// Get the default value
assert.NoError(t, f.GetPageMargins(sheet, def), opt)
// Get again and check
assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
if !assert.Equal(t, val1, def, opt) {
t.FailNow()
}
// Set the same value
assert.NoError(t, f.SetPageMargins(sheet, val1), opt)
// Get again and check
assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
t.FailNow()
}
// Set a different value
assert.NoError(t, f.SetPageMargins(sheet, test.nonDefault), opt)
assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
// Get again and compare
assert.NoError(t, f.GetPageMargins(sheet, val2), opt)
if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
t.FailNow()
}
// Value should not be the same as the default
if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
t.FailNow()
}
// Restore the default value
assert.NoError(t, f.SetPageMargins(sheet, def), opt)
assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
if !assert.Equal(t, def, val1) {
t.FailNow()
}
})
}
}
func TestSetPageMargins(t *testing.T) {
f := excelize.NewFile()
// Test set page margins on not exists worksheet.
assert.EqualError(t, f.SetPageMargins("SheetN"), "sheet SheetN is not exist")
}
func TestGetPageMargins(t *testing.T) {
f := excelize.NewFile()
// Test get page margins on not exists worksheet.
assert.EqualError(t, f.GetPageMargins("SheetN"), "sheet SheetN is not exist")
}