Skip to content

Commit 9546121

Browse files
committed
Add {G,S}etSheetPrOptions()
Add SetSheetPrOptions and GetSheetPrOptions to allow to set options on <sheetPr> and <pageSetUpPr>. The following options are implemented: - CodeName (string) - EnableFormatConditionsCalculation (bool) - Published (bool) - FitToPage (bool) - AutoPageBreaks (bool)
1 parent 90998bf commit 9546121

File tree

3 files changed

+294
-1
lines changed

3 files changed

+294
-1
lines changed

sheetpr.go

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package excelize
2+
3+
// SheetPrOption is an option of a view of a worksheet. See SetSheetPrOptions().
4+
type SheetPrOption interface {
5+
setSheetPrOption(view *xlsxSheetPr)
6+
}
7+
8+
// SheetPrOptionPtr is a writable SheetPrOption. See GetSheetPrOptions().
9+
type SheetPrOptionPtr interface {
10+
SheetPrOption
11+
getSheetPrOption(view *xlsxSheetPr)
12+
}
13+
14+
type (
15+
// CodeName is a SheetPrOption
16+
CodeName string
17+
// EnableFormatConditionsCalculation is a SheetPrOption
18+
EnableFormatConditionsCalculation bool
19+
// Published is a SheetPrOption
20+
Published bool
21+
// FitToPage is a SheetPrOption
22+
FitToPage bool
23+
// AutoPageBreaks is a SheetPrOption
24+
AutoPageBreaks bool
25+
)
26+
27+
func (o CodeName) setSheetPrOption(pr *xlsxSheetPr) {
28+
pr.CodeName = string(o)
29+
}
30+
31+
func (o *CodeName) getSheetPrOption(pr *xlsxSheetPr) {
32+
if pr == nil {
33+
*o = ""
34+
return
35+
}
36+
*o = CodeName(pr.CodeName)
37+
}
38+
39+
func (o EnableFormatConditionsCalculation) setSheetPrOption(pr *xlsxSheetPr) {
40+
pr.EnableFormatConditionsCalculation = boolPtr(bool(o))
41+
}
42+
43+
func (o *EnableFormatConditionsCalculation) getSheetPrOption(pr *xlsxSheetPr) {
44+
if pr == nil {
45+
*o = true
46+
return
47+
}
48+
*o = EnableFormatConditionsCalculation(defaultTrue(pr.EnableFormatConditionsCalculation))
49+
}
50+
51+
func (o Published) setSheetPrOption(pr *xlsxSheetPr) {
52+
pr.Published = boolPtr(bool(o))
53+
}
54+
55+
func (o *Published) getSheetPrOption(pr *xlsxSheetPr) {
56+
if pr == nil {
57+
*o = true
58+
return
59+
}
60+
*o = Published(defaultTrue(pr.Published))
61+
}
62+
63+
func (o FitToPage) setSheetPrOption(pr *xlsxSheetPr) {
64+
if pr.PageSetUpPr == nil {
65+
if !o {
66+
return
67+
}
68+
pr.PageSetUpPr = new(xlsxPageSetUpPr)
69+
}
70+
pr.PageSetUpPr.FitToPage = bool(o)
71+
}
72+
73+
func (o *FitToPage) getSheetPrOption(pr *xlsxSheetPr) {
74+
// Excel default: false
75+
if pr == nil || pr.PageSetUpPr == nil {
76+
*o = false
77+
return
78+
}
79+
*o = FitToPage(pr.PageSetUpPr.FitToPage)
80+
}
81+
82+
func (o AutoPageBreaks) setSheetPrOption(pr *xlsxSheetPr) {
83+
if pr.PageSetUpPr == nil {
84+
if !o {
85+
return
86+
}
87+
pr.PageSetUpPr = new(xlsxPageSetUpPr)
88+
}
89+
pr.PageSetUpPr.AutoPageBreaks = bool(o)
90+
}
91+
92+
func (o *AutoPageBreaks) getSheetPrOption(pr *xlsxSheetPr) {
93+
// Excel default: false
94+
if pr == nil || pr.PageSetUpPr == nil {
95+
*o = false
96+
return
97+
}
98+
*o = AutoPageBreaks(pr.PageSetUpPr.AutoPageBreaks)
99+
}
100+
101+
// SetSheetPrOptions sets sheet properties.
102+
//
103+
// Available options:
104+
// CodeName(string)
105+
// EnableFormatConditionsCalculation(bool)
106+
// Published(bool)
107+
// FitToPage(bool)
108+
// AutoPageBreaks(bool)
109+
func (f *File) SetSheetPrOptions(name string, opts ...SheetPrOption) error {
110+
sheet := f.workSheetReader(name)
111+
pr := sheet.SheetPr
112+
if pr == nil {
113+
pr = new(xlsxSheetPr)
114+
sheet.SheetPr = pr
115+
}
116+
117+
for _, opt := range opts {
118+
opt.setSheetPrOption(pr)
119+
}
120+
return nil
121+
}
122+
123+
// SetSheetPrOptions sets sheet properties.
124+
//
125+
// Available options:
126+
// CodeName(string)
127+
// EnableFormatConditionsCalculation(bool)
128+
// Published(bool)
129+
// FitToPage(bool)
130+
// AutoPageBreaks(bool)
131+
func (f *File) GetSheetPrOptions(name string, opts ...SheetPrOptionPtr) error {
132+
sheet := f.workSheetReader(name)
133+
pr := sheet.SheetPr
134+
135+
for _, opt := range opts {
136+
opt.getSheetPrOption(pr)
137+
}
138+
return nil
139+
}

sheetpr_test.go

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package excelize_test
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/mohae/deepcopy"
9+
10+
"github.com/360EntSecGroup-Skylar/excelize"
11+
)
12+
13+
var _ = []excelize.SheetPrOption{
14+
excelize.CodeName("hello"),
15+
excelize.EnableFormatConditionsCalculation(false),
16+
excelize.Published(false),
17+
excelize.FitToPage(true),
18+
excelize.AutoPageBreaks(true),
19+
}
20+
21+
var _ = []excelize.SheetPrOptionPtr{
22+
(*excelize.CodeName)(nil),
23+
(*excelize.EnableFormatConditionsCalculation)(nil),
24+
(*excelize.Published)(nil),
25+
(*excelize.FitToPage)(nil),
26+
(*excelize.AutoPageBreaks)(nil),
27+
}
28+
29+
func ExampleFile_SetSheetPrOptions() {
30+
xl := excelize.NewFile()
31+
const sheet = "Sheet1"
32+
33+
if err := xl.SetSheetPrOptions(sheet,
34+
excelize.CodeName("code"),
35+
excelize.EnableFormatConditionsCalculation(false),
36+
excelize.Published(false),
37+
excelize.FitToPage(true),
38+
excelize.AutoPageBreaks(true),
39+
); err != nil {
40+
panic(err)
41+
}
42+
// Output:
43+
}
44+
45+
func ExampleFile_GetSheetPrOptions() {
46+
xl := excelize.NewFile()
47+
const sheet = "Sheet1"
48+
49+
var (
50+
codeName excelize.CodeName
51+
enableFormatConditionsCalculation excelize.EnableFormatConditionsCalculation
52+
published excelize.Published
53+
fitToPage excelize.FitToPage
54+
autoPageBreaks excelize.AutoPageBreaks
55+
)
56+
57+
if err := xl.GetSheetPrOptions(sheet,
58+
&codeName,
59+
&enableFormatConditionsCalculation,
60+
&published,
61+
&fitToPage,
62+
&autoPageBreaks,
63+
); err != nil {
64+
panic(err)
65+
}
66+
fmt.Println("Defaults:")
67+
fmt.Printf("- codeName: %q\n", codeName)
68+
fmt.Println("- enableFormatConditionsCalculation:", enableFormatConditionsCalculation)
69+
fmt.Println("- published:", published)
70+
fmt.Println("- fitToPage:", fitToPage)
71+
fmt.Println("- autoPageBreaks:", autoPageBreaks)
72+
// Output:
73+
// Defaults:
74+
// - codeName: ""
75+
// - enableFormatConditionsCalculation: true
76+
// - published: true
77+
// - fitToPage: false
78+
// - autoPageBreaks: false
79+
}
80+
81+
func TestSheetPrOptions(t *testing.T) {
82+
const sheet = "Sheet1"
83+
for _, test := range []struct {
84+
container excelize.SheetPrOptionPtr
85+
nonDefault excelize.SheetPrOption
86+
}{
87+
{new(excelize.CodeName), excelize.CodeName("xx")},
88+
{new(excelize.EnableFormatConditionsCalculation), excelize.EnableFormatConditionsCalculation(false)},
89+
{new(excelize.Published), excelize.Published(false)},
90+
{new(excelize.FitToPage), excelize.FitToPage(true)},
91+
{new(excelize.AutoPageBreaks), excelize.AutoPageBreaks(true)},
92+
} {
93+
opt := test.nonDefault
94+
t.Logf("option %T", opt)
95+
96+
def := deepcopy.Copy(test.container).(excelize.SheetPrOptionPtr)
97+
val1 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
98+
val2 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
99+
100+
xl := excelize.NewFile()
101+
// Get the default value
102+
if err := xl.GetSheetPrOptions(sheet, def); err != nil {
103+
t.Fatalf("%T: %s", opt, err)
104+
}
105+
// Get again and check
106+
if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
107+
t.Fatalf("%T: %s", opt, err)
108+
}
109+
if !reflect.DeepEqual(val1, def) {
110+
t.Fatalf("%T: value should not have changed", opt)
111+
}
112+
// Set the same value
113+
if err := xl.SetSheetPrOptions(sheet, val1); err != nil {
114+
t.Fatalf("%T: %s", opt, err)
115+
}
116+
// Get again and check
117+
if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
118+
t.Fatalf("%T: %s", opt, err)
119+
}
120+
if !reflect.DeepEqual(val1, def) {
121+
t.Fatalf("%T: value should not have changed", opt)
122+
}
123+
124+
// Set a different value
125+
if err := xl.SetSheetPrOptions(sheet, test.nonDefault); err != nil {
126+
t.Fatalf("%T: %s", opt, err)
127+
}
128+
if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
129+
t.Fatalf("%T: %s", opt, err)
130+
}
131+
// Get again and compare
132+
if err := xl.GetSheetPrOptions(sheet, val2); err != nil {
133+
t.Fatalf("%T: %s", opt, err)
134+
}
135+
if !reflect.DeepEqual(val2, val1) {
136+
t.Fatalf("%T: value should not have changed", opt)
137+
}
138+
// Value should not be the same as the default
139+
if reflect.DeepEqual(val1, def) {
140+
t.Fatalf("%T: value should have changed from default", opt)
141+
}
142+
143+
// Restore the default value
144+
if err := xl.SetSheetPrOptions(sheet, def); err != nil {
145+
t.Fatalf("%T: %s", opt, err)
146+
}
147+
if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
148+
t.Fatalf("%T: %s", opt, err)
149+
}
150+
if !reflect.DeepEqual(val1, def) {
151+
t.Fatalf("%T: value should now be the same as default", opt)
152+
}
153+
}
154+
}

xmlWorksheet.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ type xlsxSheetPr struct {
196196
CodeName string `xml:"codeName,attr,omitempty"`
197197
EnableFormatConditionsCalculation *bool `xml:"enableFormatConditionsCalculation,attr"`
198198
FilterMode bool `xml:"filterMode,attr,omitempty"`
199-
Published bool `xml:"published,attr,omitempty"`
199+
Published *bool `xml:"published,attr"`
200200
SyncHorizontal bool `xml:"syncHorizontal,attr,omitempty"`
201201
SyncVertical bool `xml:"syncVertical,attr,omitempty"`
202202
TransitionEntry bool `xml:"transitionEntry,attr,omitempty"`

0 commit comments

Comments
 (0)