Skip to content

Commit fd0eb2b

Browse files
This closes #1283, support set and get color index, theme and tint for sheet tab
This commit renames `TabColor` into `TabColorRGB` (but keeps an alias for backwards compatibility), as well as adds support for more tab color options (Theme, Indexed and Tint). Signed-off-by: Thomas Charbonnel <[email protected]>
1 parent ebea684 commit fd0eb2b

File tree

2 files changed

+118
-14
lines changed

2 files changed

+118
-14
lines changed

sheetpr.go

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,37 @@ type (
3333
Published bool
3434
// FitToPage is a SheetPrOption
3535
FitToPage bool
36-
// TabColor is a SheetPrOption
37-
TabColor string
36+
// TabColorIndexed is a TabColor option, within SheetPrOption
37+
TabColorIndexed int
38+
// TabColorRGB is a TabColor option, within SheetPrOption
39+
TabColorRGB string
40+
// TabColorTheme is a TabColor option, within SheetPrOption
41+
TabColorTheme int
42+
// TabColorTint is a TabColor option, within SheetPrOption
43+
TabColorTint float64
3844
// AutoPageBreaks is a SheetPrOption
3945
AutoPageBreaks bool
4046
// OutlineSummaryBelow is an outlinePr, within SheetPr option
4147
OutlineSummaryBelow bool
4248
)
4349

50+
const (
51+
TabColorThemeLight1 int = iota // Inverted compared to the spec because that's how Excel maps them
52+
TabColorThemeDark1
53+
TabColorThemeLight2
54+
TabColorThemeDark2
55+
TabColorThemeAccent1
56+
TabColorThemeAccent2
57+
TabColorThemeAccent3
58+
TabColorThemeAccent4
59+
TabColorThemeAccent5
60+
TabColorThemeAccent6
61+
TabColorThemeHyperlink
62+
TabColorThemeFollowedHyperlink
63+
64+
TabColorUnsetValue int = -1
65+
)
66+
4467
// setSheetPrOption implements the SheetPrOption interface.
4568
func (o OutlineSummaryBelow) setSheetPrOption(pr *xlsxSheetPr) {
4669
if pr.OutlinePr == nil {
@@ -129,9 +152,28 @@ func (o *FitToPage) getSheetPrOption(pr *xlsxSheetPr) {
129152
*o = FitToPage(pr.PageSetUpPr.FitToPage)
130153
}
131154

155+
// setSheetPrOption implements the SheetPrOption interface and sets the
156+
// TabColor Indexed.
157+
func (o TabColorIndexed) setSheetPrOption(pr *xlsxSheetPr) {
158+
if pr.TabColor == nil {
159+
pr.TabColor = new(xlsxTabColor)
160+
}
161+
pr.TabColor.Indexed = int(o)
162+
}
163+
164+
// getSheetPrOption implements the SheetPrOptionPtr interface and gets the
165+
// TabColor Indexed. Defaults to -1 if no indexed has been set.
166+
func (o *TabColorIndexed) getSheetPrOption(pr *xlsxSheetPr) {
167+
if pr == nil || pr.TabColor == nil {
168+
*o = TabColorIndexed(TabColorUnsetValue)
169+
return
170+
}
171+
*o = TabColorIndexed(pr.TabColor.Indexed)
172+
}
173+
132174
// setSheetPrOption implements the SheetPrOption interface and specifies a
133175
// stable name of the sheet.
134-
func (o TabColor) setSheetPrOption(pr *xlsxSheetPr) {
176+
func (o TabColorRGB) setSheetPrOption(pr *xlsxSheetPr) {
135177
if pr.TabColor == nil {
136178
if string(o) == "" {
137179
return
@@ -143,12 +185,50 @@ func (o TabColor) setSheetPrOption(pr *xlsxSheetPr) {
143185

144186
// getSheetPrOption implements the SheetPrOptionPtr interface and get the
145187
// stable name of the sheet.
146-
func (o *TabColor) getSheetPrOption(pr *xlsxSheetPr) {
188+
func (o *TabColorRGB) getSheetPrOption(pr *xlsxSheetPr) {
147189
if pr == nil || pr.TabColor == nil {
148190
*o = ""
149191
return
150192
}
151-
*o = TabColor(strings.TrimPrefix(pr.TabColor.RGB, "FF"))
193+
*o = TabColorRGB(strings.TrimPrefix(pr.TabColor.RGB, "FF"))
194+
}
195+
196+
// setSheetPrOption implements the SheetPrOption interface and sets the
197+
// TabColor Theme. Warning: it does not create a clrScheme!
198+
func (o TabColorTheme) setSheetPrOption(pr *xlsxSheetPr) {
199+
if pr.TabColor == nil {
200+
pr.TabColor = new(xlsxTabColor)
201+
}
202+
pr.TabColor.Theme = int(o)
203+
}
204+
205+
// getSheetPrOption implements the SheetPrOptionPtr interface and gets the
206+
// TabColor Theme. Defaults to -1 if no theme has been set.
207+
func (o *TabColorTheme) getSheetPrOption(pr *xlsxSheetPr) {
208+
if pr == nil || pr.TabColor == nil {
209+
*o = TabColorTheme(TabColorUnsetValue)
210+
return
211+
}
212+
*o = TabColorTheme(pr.TabColor.Theme)
213+
}
214+
215+
// setSheetPrOption implements the SheetPrOption interface and sets the
216+
// TabColor Tint.
217+
func (o TabColorTint) setSheetPrOption(pr *xlsxSheetPr) {
218+
if pr.TabColor == nil {
219+
pr.TabColor = new(xlsxTabColor)
220+
}
221+
pr.TabColor.Tint = float64(o)
222+
}
223+
224+
// getSheetPrOption implements the SheetPrOptionPtr interface and gets the
225+
// TabColor Tint. Defaults to 0.0 if no tint has been set.
226+
func (o *TabColorTint) getSheetPrOption(pr *xlsxSheetPr) {
227+
if pr == nil || pr.TabColor == nil {
228+
*o = 0.0
229+
return
230+
}
231+
*o = TabColorTint(pr.TabColor.Tint)
152232
}
153233

154234
// setSheetPrOption implements the SheetPrOption interface.

sheetpr_test.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ var _ = []SheetPrOption{
1313
EnableFormatConditionsCalculation(false),
1414
Published(false),
1515
FitToPage(true),
16-
TabColor("#FFFF00"),
16+
TabColorIndexed(42),
17+
TabColorRGB("#FFFF00"),
18+
TabColorTheme(TabColorThemeLight2),
19+
TabColorTint(0.5),
1720
AutoPageBreaks(true),
1821
OutlineSummaryBelow(true),
1922
}
@@ -23,7 +26,10 @@ var _ = []SheetPrOptionPtr{
2326
(*EnableFormatConditionsCalculation)(nil),
2427
(*Published)(nil),
2528
(*FitToPage)(nil),
26-
(*TabColor)(nil),
29+
(*TabColorIndexed)(nil),
30+
(*TabColorRGB)(nil),
31+
(*TabColorTheme)(nil),
32+
(*TabColorTint)(nil),
2733
(*AutoPageBreaks)(nil),
2834
(*OutlineSummaryBelow)(nil),
2935
}
@@ -37,7 +43,10 @@ func ExampleFile_SetSheetPrOptions() {
3743
EnableFormatConditionsCalculation(false),
3844
Published(false),
3945
FitToPage(true),
40-
TabColor("#FFFF00"),
46+
TabColorIndexed(42),
47+
TabColorRGB("#FFFF00"),
48+
TabColorTheme(TabColorThemeLight2),
49+
TabColorTint(0.5),
4150
AutoPageBreaks(true),
4251
OutlineSummaryBelow(false),
4352
); err != nil {
@@ -55,7 +64,10 @@ func ExampleFile_GetSheetPrOptions() {
5564
enableFormatConditionsCalculation EnableFormatConditionsCalculation
5665
published Published
5766
fitToPage FitToPage
58-
tabColor TabColor
67+
tabColorIndexed TabColorIndexed
68+
tabColorRGB TabColorRGB
69+
tabColorTheme TabColorTheme
70+
tabColorTint TabColorTint
5971
autoPageBreaks AutoPageBreaks
6072
outlineSummaryBelow OutlineSummaryBelow
6173
)
@@ -65,7 +77,10 @@ func ExampleFile_GetSheetPrOptions() {
6577
&enableFormatConditionsCalculation,
6678
&published,
6779
&fitToPage,
68-
&tabColor,
80+
&tabColorIndexed,
81+
&tabColorRGB,
82+
&tabColorTheme,
83+
&tabColorTint,
6984
&autoPageBreaks,
7085
&outlineSummaryBelow,
7186
); err != nil {
@@ -76,7 +91,10 @@ func ExampleFile_GetSheetPrOptions() {
7691
fmt.Println("- enableFormatConditionsCalculation:", enableFormatConditionsCalculation)
7792
fmt.Println("- published:", published)
7893
fmt.Println("- fitToPage:", fitToPage)
79-
fmt.Printf("- tabColor: %q\n", tabColor)
94+
fmt.Printf("- tabColorIndexed: %d\n", tabColorIndexed)
95+
fmt.Printf("- tabColorRGB: %q\n", tabColorRGB)
96+
fmt.Printf("- tabColorTheme: %d\n", tabColorTheme)
97+
fmt.Printf("- tabColorTint: %f\n", tabColorTint)
8098
fmt.Println("- autoPageBreaks:", autoPageBreaks)
8199
fmt.Println("- outlineSummaryBelow:", outlineSummaryBelow)
82100
// Output:
@@ -85,7 +103,10 @@ func ExampleFile_GetSheetPrOptions() {
85103
// - enableFormatConditionsCalculation: true
86104
// - published: true
87105
// - fitToPage: false
88-
// - tabColor: ""
106+
// - tabColorIndexed: -1
107+
// - tabColorRGB: ""
108+
// - tabColorTheme: -1
109+
// - tabColorTint: 0.000000
89110
// - autoPageBreaks: false
90111
// - outlineSummaryBelow: true
91112
}
@@ -101,7 +122,10 @@ func TestSheetPrOptions(t *testing.T) {
101122
{new(EnableFormatConditionsCalculation), EnableFormatConditionsCalculation(false)},
102123
{new(Published), Published(false)},
103124
{new(FitToPage), FitToPage(true)},
104-
{new(TabColor), TabColor("FFFF00")},
125+
{new(TabColorIndexed), TabColorIndexed(42)},
126+
{new(TabColorRGB), TabColorRGB("FFFF00")},
127+
{new(TabColorTheme), TabColorTheme(TabColorThemeLight2)},
128+
{new(TabColorTint), TabColorTint(0.5)},
105129
{new(AutoPageBreaks), AutoPageBreaks(true)},
106130
{new(OutlineSummaryBelow), OutlineSummaryBelow(false)},
107131
}
@@ -154,7 +178,7 @@ func TestSheetPrOptions(t *testing.T) {
154178

155179
func TestSetSheetPrOptions(t *testing.T) {
156180
f := NewFile()
157-
assert.NoError(t, f.SetSheetPrOptions("Sheet1", TabColor("")))
181+
assert.NoError(t, f.SetSheetPrOptions("Sheet1", TabColorRGB("")))
158182
// Test SetSheetPrOptions on not exists worksheet.
159183
assert.EqualError(t, f.SetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
160184
}

0 commit comments

Comments
 (0)