This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
forked from qax-os/excelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheetpr_test.go
153 lines (139 loc) · 4.22 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
package excelize_test
import (
"fmt"
"reflect"
"testing"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/mohae/deepcopy"
)
var _ = []excelize.SheetPrOption{
excelize.CodeName("hello"),
excelize.EnableFormatConditionsCalculation(false),
excelize.Published(false),
excelize.FitToPage(true),
excelize.AutoPageBreaks(true),
}
var _ = []excelize.SheetPrOptionPtr{
(*excelize.CodeName)(nil),
(*excelize.EnableFormatConditionsCalculation)(nil),
(*excelize.Published)(nil),
(*excelize.FitToPage)(nil),
(*excelize.AutoPageBreaks)(nil),
}
func ExampleFile_SetSheetPrOptions() {
xl := excelize.NewFile()
const sheet = "Sheet1"
if err := xl.SetSheetPrOptions(sheet,
excelize.CodeName("code"),
excelize.EnableFormatConditionsCalculation(false),
excelize.Published(false),
excelize.FitToPage(true),
excelize.AutoPageBreaks(true),
); err != nil {
panic(err)
}
// Output:
}
func ExampleFile_GetSheetPrOptions() {
xl := excelize.NewFile()
const sheet = "Sheet1"
var (
codeName excelize.CodeName
enableFormatConditionsCalculation excelize.EnableFormatConditionsCalculation
published excelize.Published
fitToPage excelize.FitToPage
autoPageBreaks excelize.AutoPageBreaks
)
if err := xl.GetSheetPrOptions(sheet,
&codeName,
&enableFormatConditionsCalculation,
&published,
&fitToPage,
&autoPageBreaks,
); 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)
// Output:
// Defaults:
// - codeName: ""
// - enableFormatConditionsCalculation: true
// - published: true
// - fitToPage: false
// - autoPageBreaks: false
}
func TestSheetPrOptions(t *testing.T) {
const sheet = "Sheet1"
for _, test := range []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)},
} {
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)
xl := excelize.NewFile()
// Get the default value
if err := xl.GetSheetPrOptions(sheet, def); err != nil {
t.Fatalf("%T: %s", opt, err)
}
// Get again and check
if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
t.Fatalf("%T: %s", opt, err)
}
if !reflect.DeepEqual(val1, def) {
t.Fatalf("%T: value should not have changed", opt)
}
// Set the same value
if err := xl.SetSheetPrOptions(sheet, val1); err != nil {
t.Fatalf("%T: %s", opt, err)
}
// Get again and check
if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
t.Fatalf("%T: %s", opt, err)
}
if !reflect.DeepEqual(val1, def) {
t.Fatalf("%T: value should not have changed", opt)
}
// Set a different value
if err := xl.SetSheetPrOptions(sheet, test.nonDefault); err != nil {
t.Fatalf("%T: %s", opt, err)
}
if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
t.Fatalf("%T: %s", opt, err)
}
// Get again and compare
if err := xl.GetSheetPrOptions(sheet, val2); err != nil {
t.Fatalf("%T: %s", opt, err)
}
if !reflect.DeepEqual(val2, val1) {
t.Fatalf("%T: value should not have changed", opt)
}
// Value should not be the same as the default
if reflect.DeepEqual(val1, def) {
t.Fatalf("%T: value should have changed from default", opt)
}
// Restore the default value
if err := xl.SetSheetPrOptions(sheet, def); err != nil {
t.Fatalf("%T: %s", opt, err)
}
if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
t.Fatalf("%T: %s", opt, err)
}
if !reflect.DeepEqual(val1, def) {
t.Fatalf("%T: value should now be the same as default", opt)
}
}
}