Skip to content

Commit

Permalink
add support for tabColor Theme and split RGB qax-os#1283
Browse files Browse the repository at this point in the history
This commit renames `TabColor` into `TabColorRGB` (but keeps an alias
for backwards compatibility), as well as adds support for tab color
  theme.

Signed-off-by: Thomas Charbonnel <[email protected]>
  • Loading branch information
Thomas Charbonnel committed Jul 22, 2022
1 parent ebea684 commit 2e9ea92
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
33 changes: 28 additions & 5 deletions sheetpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ type (
Published bool
// FitToPage is a SheetPrOption
FitToPage bool
// TabColor is a SheetPrOption
TabColor string
// TabColorTheme is a TabColor option, within SheetPrOption
TabColorTheme int
// TabColorRGB is a TabColor option, within SheetPrOption
TabColorRGB string
// Equivalent to TabColorRGB, alias added for backwards compatibility
TabColor = TabColorRGB
// AutoPageBreaks is a SheetPrOption
AutoPageBreaks bool
// OutlineSummaryBelow is an outlinePr, within SheetPr option
Expand Down Expand Up @@ -131,7 +135,7 @@ func (o *FitToPage) getSheetPrOption(pr *xlsxSheetPr) {

// setSheetPrOption implements the SheetPrOption interface and specifies a
// stable name of the sheet.
func (o TabColor) setSheetPrOption(pr *xlsxSheetPr) {
func (o TabColorRGB) setSheetPrOption(pr *xlsxSheetPr) {
if pr.TabColor == nil {
if string(o) == "" {
return
Expand All @@ -143,12 +147,31 @@ func (o TabColor) setSheetPrOption(pr *xlsxSheetPr) {

// getSheetPrOption implements the SheetPrOptionPtr interface and get the
// stable name of the sheet.
func (o *TabColor) getSheetPrOption(pr *xlsxSheetPr) {
func (o *TabColorRGB) getSheetPrOption(pr *xlsxSheetPr) {
if pr == nil || pr.TabColor == nil {
*o = ""
return
}
*o = TabColor(strings.TrimPrefix(pr.TabColor.RGB, "FF"))
*o = TabColorRGB(strings.TrimPrefix(pr.TabColor.RGB, "FF"))
}

// setSheetPrOption implements the SheetPrOption interface and sets the
// TabColor Theme. Warning: it does not create a clrScheme!
func (o TabColorTheme) setSheetPrOption(pr *xlsxSheetPr) {
if pr.TabColor == nil {
pr.TabColor = new(xlsxTabColor)
}
pr.TabColor.Theme = int(o)
}

// getSheetPrOption implements the SheetPrOptionPtr interface and gets the
// TabColor Theme. Defaults to -1 if no theme has been set.
func (o *TabColorTheme) getSheetPrOption(pr *xlsxSheetPr) {
if pr == nil || pr.TabColor == nil {
*o = -1 // 0 is a valid theme value, hence using -1 here
return
}
*o = TabColorTheme(pr.TabColor.Theme)
}

// setSheetPrOption implements the SheetPrOption interface.
Expand Down
23 changes: 14 additions & 9 deletions sheetpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = []SheetPrOption{
EnableFormatConditionsCalculation(false),
Published(false),
FitToPage(true),
TabColor("#FFFF00"),
TabColorRGB("#FFFF00"),
AutoPageBreaks(true),
OutlineSummaryBelow(true),
}
Expand All @@ -23,7 +23,7 @@ var _ = []SheetPrOptionPtr{
(*EnableFormatConditionsCalculation)(nil),
(*Published)(nil),
(*FitToPage)(nil),
(*TabColor)(nil),
(*TabColorRGB)(nil),
(*AutoPageBreaks)(nil),
(*OutlineSummaryBelow)(nil),
}
Expand All @@ -37,7 +37,7 @@ func ExampleFile_SetSheetPrOptions() {
EnableFormatConditionsCalculation(false),
Published(false),
FitToPage(true),
TabColor("#FFFF00"),
TabColorRGB("#FFFF00"),
AutoPageBreaks(true),
OutlineSummaryBelow(false),
); err != nil {
Expand All @@ -55,7 +55,8 @@ func ExampleFile_GetSheetPrOptions() {
enableFormatConditionsCalculation EnableFormatConditionsCalculation
published Published
fitToPage FitToPage
tabColor TabColor
tabColorRGB TabColorRGB
tabColorTheme TabColorTheme
autoPageBreaks AutoPageBreaks
outlineSummaryBelow OutlineSummaryBelow
)
Expand All @@ -65,7 +66,8 @@ func ExampleFile_GetSheetPrOptions() {
&enableFormatConditionsCalculation,
&published,
&fitToPage,
&tabColor,
&tabColorRGB,
&tabColorTheme,
&autoPageBreaks,
&outlineSummaryBelow,
); err != nil {
Expand All @@ -76,7 +78,8 @@ func ExampleFile_GetSheetPrOptions() {
fmt.Println("- enableFormatConditionsCalculation:", enableFormatConditionsCalculation)
fmt.Println("- published:", published)
fmt.Println("- fitToPage:", fitToPage)
fmt.Printf("- tabColor: %q\n", tabColor)
fmt.Printf("- tabColorRGB: %q\n", tabColorRGB)
fmt.Printf("- tabColorTheme: %d\n", tabColorTheme)
fmt.Println("- autoPageBreaks:", autoPageBreaks)
fmt.Println("- outlineSummaryBelow:", outlineSummaryBelow)
// Output:
Expand All @@ -85,7 +88,8 @@ func ExampleFile_GetSheetPrOptions() {
// - enableFormatConditionsCalculation: true
// - published: true
// - fitToPage: false
// - tabColor: ""
// - tabColorRGB: ""
// - tabColorTheme: -1
// - autoPageBreaks: false
// - outlineSummaryBelow: true
}
Expand All @@ -101,7 +105,8 @@ func TestSheetPrOptions(t *testing.T) {
{new(EnableFormatConditionsCalculation), EnableFormatConditionsCalculation(false)},
{new(Published), Published(false)},
{new(FitToPage), FitToPage(true)},
{new(TabColor), TabColor("FFFF00")},
{new(TabColorRGB), TabColorRGB("FFFF00")},
{new(TabColorTheme), TabColorTheme(2)},
{new(AutoPageBreaks), AutoPageBreaks(true)},
{new(OutlineSummaryBelow), OutlineSummaryBelow(false)},
}
Expand Down Expand Up @@ -154,7 +159,7 @@ func TestSheetPrOptions(t *testing.T) {

func TestSetSheetPrOptions(t *testing.T) {
f := NewFile()
assert.NoError(t, f.SetSheetPrOptions("Sheet1", TabColor("")))
assert.NoError(t, f.SetSheetPrOptions("Sheet1", TabColorRGB("")))
// Test SetSheetPrOptions on not exists worksheet.
assert.EqualError(t, f.SetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
}
Expand Down

0 comments on commit 2e9ea92

Please sign in to comment.