2020-10-04 21:07:39 +08:00
|
|
|
package excelize
|
2017-11-14 22:09:50 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mohae/deepcopy"
|
2018-12-27 18:51:44 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-11-14 22:09:50 +08:00
|
|
|
)
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
var _ = []SheetPrOption{
|
|
|
|
CodeName("hello"),
|
|
|
|
EnableFormatConditionsCalculation(false),
|
|
|
|
Published(false),
|
|
|
|
FitToPage(true),
|
2022-07-26 12:36:21 +08:00
|
|
|
TabColorIndexed(42),
|
|
|
|
TabColorRGB("#FFFF00"),
|
2022-08-06 15:23:03 +08:00
|
|
|
TabColorTheme(ColorMappingTypeLight2),
|
2022-07-26 12:36:21 +08:00
|
|
|
TabColorTint(0.5),
|
2020-10-04 21:07:39 +08:00
|
|
|
AutoPageBreaks(true),
|
|
|
|
OutlineSummaryBelow(true),
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
var _ = []SheetPrOptionPtr{
|
|
|
|
(*CodeName)(nil),
|
|
|
|
(*EnableFormatConditionsCalculation)(nil),
|
|
|
|
(*Published)(nil),
|
|
|
|
(*FitToPage)(nil),
|
2022-07-26 12:36:21 +08:00
|
|
|
(*TabColorIndexed)(nil),
|
|
|
|
(*TabColorRGB)(nil),
|
|
|
|
(*TabColorTheme)(nil),
|
|
|
|
(*TabColorTint)(nil),
|
2020-10-04 21:07:39 +08:00
|
|
|
(*AutoPageBreaks)(nil),
|
|
|
|
(*OutlineSummaryBelow)(nil),
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_SetSheetPrOptions() {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2017-11-14 22:09:50 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.SetSheetPrOptions(sheet,
|
2020-10-04 21:07:39 +08:00
|
|
|
CodeName("code"),
|
|
|
|
EnableFormatConditionsCalculation(false),
|
|
|
|
Published(false),
|
|
|
|
FitToPage(true),
|
2022-07-26 12:36:21 +08:00
|
|
|
TabColorIndexed(42),
|
|
|
|
TabColorRGB("#FFFF00"),
|
2022-08-06 15:23:03 +08:00
|
|
|
TabColorTheme(ColorMappingTypeLight2),
|
2022-07-26 12:36:21 +08:00
|
|
|
TabColorTint(0.5),
|
2020-10-04 21:07:39 +08:00
|
|
|
AutoPageBreaks(true),
|
|
|
|
OutlineSummaryBelow(false),
|
2017-11-14 22:09:50 +08:00
|
|
|
); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|
|
|
|
// Output:
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_GetSheetPrOptions() {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2017-11-14 22:09:50 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
var (
|
2020-10-04 21:07:39 +08:00
|
|
|
codeName CodeName
|
|
|
|
enableFormatConditionsCalculation EnableFormatConditionsCalculation
|
|
|
|
published Published
|
|
|
|
fitToPage FitToPage
|
2022-07-26 12:36:21 +08:00
|
|
|
tabColorIndexed TabColorIndexed
|
|
|
|
tabColorRGB TabColorRGB
|
|
|
|
tabColorTheme TabColorTheme
|
|
|
|
tabColorTint TabColorTint
|
2020-10-04 21:07:39 +08:00
|
|
|
autoPageBreaks AutoPageBreaks
|
|
|
|
outlineSummaryBelow OutlineSummaryBelow
|
2017-11-14 22:09:50 +08:00
|
|
|
)
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.GetSheetPrOptions(sheet,
|
2017-11-14 22:09:50 +08:00
|
|
|
&codeName,
|
|
|
|
&enableFormatConditionsCalculation,
|
|
|
|
&published,
|
|
|
|
&fitToPage,
|
2022-07-26 12:36:21 +08:00
|
|
|
&tabColorIndexed,
|
|
|
|
&tabColorRGB,
|
|
|
|
&tabColorTheme,
|
|
|
|
&tabColorTint,
|
2017-11-14 22:09:50 +08:00
|
|
|
&autoPageBreaks,
|
2018-12-14 03:01:36 +08:00
|
|
|
&outlineSummaryBelow,
|
2017-11-14 22:09:50 +08:00
|
|
|
); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|
|
|
|
fmt.Println("Defaults:")
|
|
|
|
fmt.Printf("- codeName: %q\n", codeName)
|
|
|
|
fmt.Println("- enableFormatConditionsCalculation:", enableFormatConditionsCalculation)
|
|
|
|
fmt.Println("- published:", published)
|
|
|
|
fmt.Println("- fitToPage:", fitToPage)
|
2022-07-26 12:36:21 +08:00
|
|
|
fmt.Printf("- tabColorIndexed: %d\n", tabColorIndexed)
|
|
|
|
fmt.Printf("- tabColorRGB: %q\n", tabColorRGB)
|
|
|
|
fmt.Printf("- tabColorTheme: %d\n", tabColorTheme)
|
|
|
|
fmt.Printf("- tabColorTint: %f\n", tabColorTint)
|
2017-11-14 22:09:50 +08:00
|
|
|
fmt.Println("- autoPageBreaks:", autoPageBreaks)
|
2018-12-14 03:01:36 +08:00
|
|
|
fmt.Println("- outlineSummaryBelow:", outlineSummaryBelow)
|
2017-11-14 22:09:50 +08:00
|
|
|
// Output:
|
|
|
|
// Defaults:
|
|
|
|
// - codeName: ""
|
|
|
|
// - enableFormatConditionsCalculation: true
|
|
|
|
// - published: true
|
|
|
|
// - fitToPage: false
|
2022-07-26 12:36:21 +08:00
|
|
|
// - tabColorIndexed: -1
|
|
|
|
// - tabColorRGB: ""
|
|
|
|
// - tabColorTheme: -1
|
|
|
|
// - tabColorTint: 0.000000
|
2017-11-14 22:09:50 +08:00
|
|
|
// - autoPageBreaks: false
|
2018-12-14 03:01:36 +08:00
|
|
|
// - outlineSummaryBelow: true
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSheetPrOptions(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
2018-12-27 18:51:44 +08:00
|
|
|
|
|
|
|
testData := []struct {
|
2020-10-04 21:07:39 +08:00
|
|
|
container SheetPrOptionPtr
|
|
|
|
nonDefault SheetPrOption
|
2017-11-14 22:09:50 +08:00
|
|
|
}{
|
2020-10-04 21:07:39 +08:00
|
|
|
{new(CodeName), CodeName("xx")},
|
|
|
|
{new(EnableFormatConditionsCalculation), EnableFormatConditionsCalculation(false)},
|
|
|
|
{new(Published), Published(false)},
|
|
|
|
{new(FitToPage), FitToPage(true)},
|
2022-07-26 12:36:21 +08:00
|
|
|
{new(TabColorIndexed), TabColorIndexed(42)},
|
|
|
|
{new(TabColorRGB), TabColorRGB("FFFF00")},
|
2022-08-06 15:23:03 +08:00
|
|
|
{new(TabColorTheme), TabColorTheme(ColorMappingTypeLight2)},
|
2022-07-26 12:36:21 +08:00
|
|
|
{new(TabColorTint), TabColorTint(0.5)},
|
2020-10-04 21:07:39 +08:00
|
|
|
{new(AutoPageBreaks), AutoPageBreaks(true)},
|
|
|
|
{new(OutlineSummaryBelow), OutlineSummaryBelow(false)},
|
2018-12-27 18:51:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testData {
|
|
|
|
t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
|
2022-09-18 00:07:15 +08:00
|
|
|
opts := test.nonDefault
|
|
|
|
t.Logf("option %T", opts)
|
2018-12-27 18:51:44 +08:00
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
def := deepcopy.Copy(test.container).(SheetPrOptionPtr)
|
|
|
|
val1 := deepcopy.Copy(def).(SheetPrOptionPtr)
|
|
|
|
val2 := deepcopy.Copy(def).(SheetPrOptionPtr)
|
2018-12-27 18:51:44 +08:00
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2018-12-27 18:51:44 +08:00
|
|
|
// Get the default value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, def), opts)
|
2018-12-27 18:51:44 +08:00
|
|
|
// Get again and check
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opts)
|
|
|
|
if !assert.Equal(t, val1, def, opts) {
|
2018-12-27 18:51:44 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set the same value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.SetSheetPrOptions(sheet, val1), opts)
|
2018-12-27 18:51:44 +08:00
|
|
|
// Get again and check
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opts)
|
|
|
|
if !assert.Equal(t, val1, def, "%T: value should not have changed", opts) {
|
2018-12-27 18:51:44 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set a different value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.SetSheetPrOptions(sheet, test.nonDefault), opts)
|
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opts)
|
2018-12-27 18:51:44 +08:00
|
|
|
// Get again and compare
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, val2), opts)
|
|
|
|
if !assert.Equal(t, val1, val2, "%T: value should not have changed", opts) {
|
2018-12-27 18:51:44 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Value should not be the same as the default
|
2022-09-18 00:07:15 +08:00
|
|
|
if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opts) {
|
2018-12-27 18:51:44 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Restore the default value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.SetSheetPrOptions(sheet, def), opts)
|
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opts)
|
2018-12-27 18:51:44 +08:00
|
|
|
if !assert.Equal(t, def, val1) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-16 01:03:29 +08:00
|
|
|
|
2022-01-09 00:20:42 +08:00
|
|
|
func TestSetSheetPrOptions(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2022-07-26 12:36:21 +08:00
|
|
|
assert.NoError(t, f.SetSheetPrOptions("Sheet1", TabColorRGB("")))
|
2022-01-09 00:20:42 +08:00
|
|
|
// Test SetSheetPrOptions on not exists worksheet.
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.SetSheetPrOptions("SheetN"), "sheet SheetN does not exist")
|
2019-10-16 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSheetPrOptions(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-10-16 01:03:29 +08:00
|
|
|
// Test GetSheetPrOptions on not exists worksheet.
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.GetSheetPrOptions("SheetN"), "sheet SheetN does not exist")
|
2019-10-16 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
var _ = []PageMarginsOptions{
|
|
|
|
PageMarginBottom(1.0),
|
|
|
|
PageMarginFooter(1.0),
|
|
|
|
PageMarginHeader(1.0),
|
|
|
|
PageMarginLeft(1.0),
|
|
|
|
PageMarginRight(1.0),
|
|
|
|
PageMarginTop(1.0),
|
2019-10-16 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
var _ = []PageMarginsOptionsPtr{
|
|
|
|
(*PageMarginBottom)(nil),
|
|
|
|
(*PageMarginFooter)(nil),
|
|
|
|
(*PageMarginHeader)(nil),
|
|
|
|
(*PageMarginLeft)(nil),
|
|
|
|
(*PageMarginRight)(nil),
|
|
|
|
(*PageMarginTop)(nil),
|
2019-10-16 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_SetPageMargins() {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-10-16 01:03:29 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
if err := f.SetPageMargins(sheet,
|
2020-10-04 21:07:39 +08:00
|
|
|
PageMarginBottom(1.0),
|
|
|
|
PageMarginFooter(1.0),
|
|
|
|
PageMarginHeader(1.0),
|
|
|
|
PageMarginLeft(1.0),
|
|
|
|
PageMarginRight(1.0),
|
|
|
|
PageMarginTop(1.0),
|
2019-10-16 01:03:29 +08:00
|
|
|
); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2019-10-16 01:03:29 +08:00
|
|
|
}
|
|
|
|
// Output:
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_GetPageMargins() {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-10-16 01:03:29 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
var (
|
2020-10-04 21:07:39 +08:00
|
|
|
marginBottom PageMarginBottom
|
|
|
|
marginFooter PageMarginFooter
|
|
|
|
marginHeader PageMarginHeader
|
|
|
|
marginLeft PageMarginLeft
|
|
|
|
marginRight PageMarginRight
|
|
|
|
marginTop PageMarginTop
|
2019-10-16 01:03:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := f.GetPageMargins(sheet,
|
|
|
|
&marginBottom,
|
|
|
|
&marginFooter,
|
|
|
|
&marginHeader,
|
|
|
|
&marginLeft,
|
|
|
|
&marginRight,
|
|
|
|
&marginTop,
|
|
|
|
); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2019-10-16 01:03:29 +08:00
|
|
|
}
|
|
|
|
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 {
|
2020-10-04 21:07:39 +08:00
|
|
|
container PageMarginsOptionsPtr
|
|
|
|
nonDefault PageMarginsOptions
|
2019-10-16 01:03:29 +08:00
|
|
|
}{
|
2020-10-04 21:07:39 +08:00
|
|
|
{new(PageMarginTop), PageMarginTop(1.0)},
|
|
|
|
{new(PageMarginBottom), PageMarginBottom(1.0)},
|
|
|
|
{new(PageMarginLeft), PageMarginLeft(1.0)},
|
|
|
|
{new(PageMarginRight), PageMarginRight(1.0)},
|
|
|
|
{new(PageMarginHeader), PageMarginHeader(1.0)},
|
|
|
|
{new(PageMarginFooter), PageMarginFooter(1.0)},
|
2019-10-16 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testData {
|
|
|
|
t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
|
2022-09-18 00:07:15 +08:00
|
|
|
opts := test.nonDefault
|
|
|
|
t.Logf("option %T", opts)
|
2019-10-16 01:03:29 +08:00
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
def := deepcopy.Copy(test.container).(PageMarginsOptionsPtr)
|
|
|
|
val1 := deepcopy.Copy(def).(PageMarginsOptionsPtr)
|
|
|
|
val2 := deepcopy.Copy(def).(PageMarginsOptionsPtr)
|
2019-10-16 01:03:29 +08:00
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-10-16 01:03:29 +08:00
|
|
|
// Get the default value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetPageMargins(sheet, def), opts)
|
2019-10-16 01:03:29 +08:00
|
|
|
// Get again and check
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetPageMargins(sheet, val1), opts)
|
|
|
|
if !assert.Equal(t, val1, def, opts) {
|
2019-10-16 01:03:29 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set the same value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.SetPageMargins(sheet, val1), opts)
|
2019-10-16 01:03:29 +08:00
|
|
|
// Get again and check
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetPageMargins(sheet, val1), opts)
|
|
|
|
if !assert.Equal(t, val1, def, "%T: value should not have changed", opts) {
|
2019-10-16 01:03:29 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set a different value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.SetPageMargins(sheet, test.nonDefault), opts)
|
|
|
|
assert.NoError(t, f.GetPageMargins(sheet, val1), opts)
|
2019-10-16 01:03:29 +08:00
|
|
|
// Get again and compare
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetPageMargins(sheet, val2), opts)
|
|
|
|
if !assert.Equal(t, val1, val2, "%T: value should not have changed", opts) {
|
2019-10-16 01:03:29 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Value should not be the same as the default
|
2022-09-18 00:07:15 +08:00
|
|
|
if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opts) {
|
2019-10-16 01:03:29 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Restore the default value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.SetPageMargins(sheet, def), opts)
|
|
|
|
assert.NoError(t, f.GetPageMargins(sheet, val1), opts)
|
2019-10-16 01:03:29 +08:00
|
|
|
if !assert.Equal(t, def, val1) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetPageMargins(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-10-16 01:03:29 +08:00
|
|
|
// Test set page margins on not exists worksheet.
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.SetPageMargins("SheetN"), "sheet SheetN does not exist")
|
2019-10-16 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPageMargins(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-10-16 01:03:29 +08:00
|
|
|
// Test get page margins on not exists worksheet.
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.GetPageMargins("SheetN"), "sheet SheetN does not exist")
|
2019-10-16 01:03:29 +08:00
|
|
|
}
|
2020-05-12 23:26:26 +08:00
|
|
|
|
|
|
|
func ExampleFile_SetSheetFormatPr() {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2020-05-12 23:26:26 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
if err := f.SetSheetFormatPr(sheet,
|
2020-10-04 21:07:39 +08:00
|
|
|
BaseColWidth(1.0),
|
|
|
|
DefaultColWidth(1.0),
|
|
|
|
DefaultRowHeight(1.0),
|
|
|
|
CustomHeight(true),
|
|
|
|
ZeroHeight(true),
|
|
|
|
ThickTop(true),
|
|
|
|
ThickBottom(true),
|
2020-05-12 23:26:26 +08:00
|
|
|
); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
// Output:
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_GetSheetFormatPr() {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2020-05-12 23:26:26 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
var (
|
2020-10-04 21:07:39 +08:00
|
|
|
baseColWidth BaseColWidth
|
|
|
|
defaultColWidth DefaultColWidth
|
|
|
|
defaultRowHeight DefaultRowHeight
|
|
|
|
customHeight CustomHeight
|
|
|
|
zeroHeight ZeroHeight
|
|
|
|
thickTop ThickTop
|
|
|
|
thickBottom ThickBottom
|
2020-05-12 23:26:26 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := f.GetSheetFormatPr(sheet,
|
|
|
|
&baseColWidth,
|
|
|
|
&defaultColWidth,
|
|
|
|
&defaultRowHeight,
|
|
|
|
&customHeight,
|
|
|
|
&zeroHeight,
|
|
|
|
&thickTop,
|
|
|
|
&thickBottom,
|
|
|
|
); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Defaults:")
|
|
|
|
fmt.Println("- baseColWidth:", baseColWidth)
|
|
|
|
fmt.Println("- defaultColWidth:", defaultColWidth)
|
|
|
|
fmt.Println("- defaultRowHeight:", defaultRowHeight)
|
|
|
|
fmt.Println("- customHeight:", customHeight)
|
|
|
|
fmt.Println("- zeroHeight:", zeroHeight)
|
|
|
|
fmt.Println("- thickTop:", thickTop)
|
|
|
|
fmt.Println("- thickBottom:", thickBottom)
|
|
|
|
// Output:
|
|
|
|
// Defaults:
|
|
|
|
// - baseColWidth: 0
|
|
|
|
// - defaultColWidth: 0
|
|
|
|
// - defaultRowHeight: 15
|
|
|
|
// - customHeight: false
|
|
|
|
// - zeroHeight: false
|
|
|
|
// - thickTop: false
|
|
|
|
// - thickBottom: false
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSheetFormatPrOptions(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
testData := []struct {
|
2020-10-04 21:07:39 +08:00
|
|
|
container SheetFormatPrOptionsPtr
|
|
|
|
nonDefault SheetFormatPrOptions
|
2020-05-12 23:26:26 +08:00
|
|
|
}{
|
2020-10-04 21:07:39 +08:00
|
|
|
{new(BaseColWidth), BaseColWidth(1.0)},
|
|
|
|
{new(DefaultColWidth), DefaultColWidth(1.0)},
|
|
|
|
{new(DefaultRowHeight), DefaultRowHeight(1.0)},
|
|
|
|
{new(CustomHeight), CustomHeight(true)},
|
|
|
|
{new(ZeroHeight), ZeroHeight(true)},
|
|
|
|
{new(ThickTop), ThickTop(true)},
|
|
|
|
{new(ThickBottom), ThickBottom(true)},
|
2020-05-12 23:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testData {
|
|
|
|
t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
|
2022-09-18 00:07:15 +08:00
|
|
|
opts := test.nonDefault
|
|
|
|
t.Logf("option %T", opts)
|
2020-05-12 23:26:26 +08:00
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
def := deepcopy.Copy(test.container).(SheetFormatPrOptionsPtr)
|
|
|
|
val1 := deepcopy.Copy(def).(SheetFormatPrOptionsPtr)
|
|
|
|
val2 := deepcopy.Copy(def).(SheetFormatPrOptionsPtr)
|
2020-05-12 23:26:26 +08:00
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2020-05-12 23:26:26 +08:00
|
|
|
// Get the default value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetSheetFormatPr(sheet, def), opts)
|
2020-05-12 23:26:26 +08:00
|
|
|
// Get again and check
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opts)
|
|
|
|
if !assert.Equal(t, val1, def, opts) {
|
2020-05-12 23:26:26 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set the same value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.SetSheetFormatPr(sheet, val1), opts)
|
2020-05-12 23:26:26 +08:00
|
|
|
// Get again and check
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opts)
|
|
|
|
if !assert.Equal(t, val1, def, "%T: value should not have changed", opts) {
|
2020-05-12 23:26:26 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set a different value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.SetSheetFormatPr(sheet, test.nonDefault), opts)
|
|
|
|
assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opts)
|
2020-05-12 23:26:26 +08:00
|
|
|
// Get again and compare
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.GetSheetFormatPr(sheet, val2), opts)
|
|
|
|
if !assert.Equal(t, val1, val2, "%T: value should not have changed", opts) {
|
2020-05-12 23:26:26 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Value should not be the same as the default
|
2022-09-18 00:07:15 +08:00
|
|
|
if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opts) {
|
2020-05-12 23:26:26 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Restore the default value
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.NoError(t, f.SetSheetFormatPr(sheet, def), opts)
|
|
|
|
assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opts)
|
2020-05-12 23:26:26 +08:00
|
|
|
if !assert.Equal(t, def, val1) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetSheetFormatPr(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2020-05-12 23:26:26 +08:00
|
|
|
assert.NoError(t, f.GetSheetFormatPr("Sheet1"))
|
2021-07-05 00:03:56 +08:00
|
|
|
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
ws.(*xlsxWorksheet).SheetFormatPr = nil
|
2020-10-04 21:07:39 +08:00
|
|
|
assert.NoError(t, f.SetSheetFormatPr("Sheet1", BaseColWidth(1.0)))
|
2020-05-12 23:26:26 +08:00
|
|
|
// Test set formatting properties on not exists worksheet.
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.SetSheetFormatPr("SheetN"), "sheet SheetN does not exist")
|
2020-05-12 23:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSheetFormatPr(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2020-05-12 23:26:26 +08:00
|
|
|
assert.NoError(t, f.GetSheetFormatPr("Sheet1"))
|
2021-07-05 00:03:56 +08:00
|
|
|
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
ws.(*xlsxWorksheet).SheetFormatPr = nil
|
2020-05-12 23:26:26 +08:00
|
|
|
var (
|
2020-10-04 21:07:39 +08:00
|
|
|
baseColWidth BaseColWidth
|
|
|
|
defaultColWidth DefaultColWidth
|
|
|
|
defaultRowHeight DefaultRowHeight
|
|
|
|
customHeight CustomHeight
|
|
|
|
zeroHeight ZeroHeight
|
|
|
|
thickTop ThickTop
|
|
|
|
thickBottom ThickBottom
|
2020-05-12 23:26:26 +08:00
|
|
|
)
|
|
|
|
assert.NoError(t, f.GetSheetFormatPr("Sheet1",
|
|
|
|
&baseColWidth,
|
|
|
|
&defaultColWidth,
|
|
|
|
&defaultRowHeight,
|
|
|
|
&customHeight,
|
|
|
|
&zeroHeight,
|
|
|
|
&thickTop,
|
|
|
|
&thickBottom,
|
|
|
|
))
|
|
|
|
// Test get formatting properties on not exists worksheet.
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.GetSheetFormatPr("SheetN"), "sheet SheetN does not exist")
|
2020-05-12 23:26:26 +08:00
|
|
|
}
|