123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- package excelize
- import (
- "fmt"
- "testing"
- "github.com/mohae/deepcopy"
- "github.com/stretchr/testify/assert"
- )
- var _ = []SheetPrOption{
- CodeName("hello"),
- EnableFormatConditionsCalculation(false),
- Published(false),
- FitToPage(true),
- TabColor("#FFFF00"),
- AutoPageBreaks(true),
- OutlineSummaryBelow(true),
- }
- var _ = []SheetPrOptionPtr{
- (*CodeName)(nil),
- (*EnableFormatConditionsCalculation)(nil),
- (*Published)(nil),
- (*FitToPage)(nil),
- (*TabColor)(nil),
- (*AutoPageBreaks)(nil),
- (*OutlineSummaryBelow)(nil),
- }
- func ExampleFile_SetSheetPrOptions() {
- f := NewFile()
- const sheet = "Sheet1"
- if err := f.SetSheetPrOptions(sheet,
- CodeName("code"),
- EnableFormatConditionsCalculation(false),
- Published(false),
- FitToPage(true),
- TabColor("#FFFF00"),
- AutoPageBreaks(true),
- OutlineSummaryBelow(false),
- ); err != nil {
- fmt.Println(err)
- }
- // Output:
- }
- func ExampleFile_GetSheetPrOptions() {
- f := NewFile()
- const sheet = "Sheet1"
- var (
- codeName CodeName
- enableFormatConditionsCalculation EnableFormatConditionsCalculation
- published Published
- fitToPage FitToPage
- tabColor TabColor
- autoPageBreaks AutoPageBreaks
- outlineSummaryBelow OutlineSummaryBelow
- )
- if err := f.GetSheetPrOptions(sheet,
- &codeName,
- &enableFormatConditionsCalculation,
- &published,
- &fitToPage,
- &tabColor,
- &autoPageBreaks,
- &outlineSummaryBelow,
- ); err != nil {
- fmt.Println(err)
- }
- fmt.Println("Defaults:")
- fmt.Printf("- codeName: %q\n", codeName)
- fmt.Println("- enableFormatConditionsCalculation:", enableFormatConditionsCalculation)
- fmt.Println("- published:", published)
- fmt.Println("- fitToPage:", fitToPage)
- fmt.Printf("- tabColor: %q\n", tabColor)
- fmt.Println("- autoPageBreaks:", autoPageBreaks)
- fmt.Println("- outlineSummaryBelow:", outlineSummaryBelow)
- // Output:
- // Defaults:
- // - codeName: ""
- // - enableFormatConditionsCalculation: true
- // - published: true
- // - fitToPage: false
- // - tabColor: ""
- // - autoPageBreaks: false
- // - outlineSummaryBelow: true
- }
- func TestSheetPrOptions(t *testing.T) {
- const sheet = "Sheet1"
- testData := []struct {
- container SheetPrOptionPtr
- nonDefault SheetPrOption
- }{
- {new(CodeName), CodeName("xx")},
- {new(EnableFormatConditionsCalculation), EnableFormatConditionsCalculation(false)},
- {new(Published), Published(false)},
- {new(FitToPage), FitToPage(true)},
- {new(TabColor), TabColor("FFFF00")},
- {new(AutoPageBreaks), AutoPageBreaks(true)},
- {new(OutlineSummaryBelow), OutlineSummaryBelow(false)},
- }
- for i, test := range testData {
- t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
- opt := test.nonDefault
- t.Logf("option %T", opt)
- def := deepcopy.Copy(test.container).(SheetPrOptionPtr)
- val1 := deepcopy.Copy(def).(SheetPrOptionPtr)
- val2 := deepcopy.Copy(def).(SheetPrOptionPtr)
- f := NewFile()
- // Get the default value
- assert.NoError(t, f.GetSheetPrOptions(sheet, def), opt)
- // Get again and check
- assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
- if !assert.Equal(t, val1, def, opt) {
- t.FailNow()
- }
- // Set the same value
- assert.NoError(t, f.SetSheetPrOptions(sheet, val1), opt)
- // Get again and check
- assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
- if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
- t.FailNow()
- }
- // Set a different value
- assert.NoError(t, f.SetSheetPrOptions(sheet, test.nonDefault), opt)
- assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
- // Get again and compare
- assert.NoError(t, f.GetSheetPrOptions(sheet, val2), opt)
- if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
- t.FailNow()
- }
- // Value should not be the same as the default
- if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
- t.FailNow()
- }
- // Restore the default value
- assert.NoError(t, f.SetSheetPrOptions(sheet, def), opt)
- assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
- if !assert.Equal(t, def, val1) {
- t.FailNow()
- }
- })
- }
- }
- func TestSetSheetrOptions(t *testing.T) {
- f := NewFile()
- assert.NoError(t, f.SetSheetPrOptions("Sheet1", TabColor("")))
- // Test SetSheetrOptions on not exists worksheet.
- assert.EqualError(t, f.SetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
- }
- func TestGetSheetPrOptions(t *testing.T) {
- f := NewFile()
- // Test GetSheetPrOptions on not exists worksheet.
- assert.EqualError(t, f.GetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
- }
- var _ = []PageMarginsOptions{
- PageMarginBottom(1.0),
- PageMarginFooter(1.0),
- PageMarginHeader(1.0),
- PageMarginLeft(1.0),
- PageMarginRight(1.0),
- PageMarginTop(1.0),
- }
- var _ = []PageMarginsOptionsPtr{
- (*PageMarginBottom)(nil),
- (*PageMarginFooter)(nil),
- (*PageMarginHeader)(nil),
- (*PageMarginLeft)(nil),
- (*PageMarginRight)(nil),
- (*PageMarginTop)(nil),
- }
- func ExampleFile_SetPageMargins() {
- f := NewFile()
- const sheet = "Sheet1"
- if err := f.SetPageMargins(sheet,
- PageMarginBottom(1.0),
- PageMarginFooter(1.0),
- PageMarginHeader(1.0),
- PageMarginLeft(1.0),
- PageMarginRight(1.0),
- PageMarginTop(1.0),
- ); err != nil {
- fmt.Println(err)
- }
- // Output:
- }
- func ExampleFile_GetPageMargins() {
- f := NewFile()
- const sheet = "Sheet1"
- var (
- marginBottom PageMarginBottom
- marginFooter PageMarginFooter
- marginHeader PageMarginHeader
- marginLeft PageMarginLeft
- marginRight PageMarginRight
- marginTop PageMarginTop
- )
- if err := f.GetPageMargins(sheet,
- &marginBottom,
- &marginFooter,
- &marginHeader,
- &marginLeft,
- &marginRight,
- &marginTop,
- ); err != nil {
- fmt.Println(err)
- }
- 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 {
- container PageMarginsOptionsPtr
- nonDefault PageMarginsOptions
- }{
- {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)},
- }
- for i, test := range testData {
- t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
- opt := test.nonDefault
- t.Logf("option %T", opt)
- def := deepcopy.Copy(test.container).(PageMarginsOptionsPtr)
- val1 := deepcopy.Copy(def).(PageMarginsOptionsPtr)
- val2 := deepcopy.Copy(def).(PageMarginsOptionsPtr)
- f := NewFile()
- // Get the default value
- assert.NoError(t, f.GetPageMargins(sheet, def), opt)
- // Get again and check
- assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
- if !assert.Equal(t, val1, def, opt) {
- t.FailNow()
- }
- // Set the same value
- assert.NoError(t, f.SetPageMargins(sheet, val1), opt)
- // Get again and check
- assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
- if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
- t.FailNow()
- }
- // Set a different value
- assert.NoError(t, f.SetPageMargins(sheet, test.nonDefault), opt)
- assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
- // Get again and compare
- assert.NoError(t, f.GetPageMargins(sheet, val2), opt)
- if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
- t.FailNow()
- }
- // Value should not be the same as the default
- if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
- t.FailNow()
- }
- // Restore the default value
- assert.NoError(t, f.SetPageMargins(sheet, def), opt)
- assert.NoError(t, f.GetPageMargins(sheet, val1), opt)
- if !assert.Equal(t, def, val1) {
- t.FailNow()
- }
- })
- }
- }
- func TestSetPageMargins(t *testing.T) {
- f := NewFile()
- // Test set page margins on not exists worksheet.
- assert.EqualError(t, f.SetPageMargins("SheetN"), "sheet SheetN is not exist")
- }
- func TestGetPageMargins(t *testing.T) {
- f := NewFile()
- // Test get page margins on not exists worksheet.
- assert.EqualError(t, f.GetPageMargins("SheetN"), "sheet SheetN is not exist")
- }
- func ExampleFile_SetSheetFormatPr() {
- f := NewFile()
- const sheet = "Sheet1"
- if err := f.SetSheetFormatPr(sheet,
- BaseColWidth(1.0),
- DefaultColWidth(1.0),
- DefaultRowHeight(1.0),
- CustomHeight(true),
- ZeroHeight(true),
- ThickTop(true),
- ThickBottom(true),
- ); err != nil {
- fmt.Println(err)
- }
- // Output:
- }
- func ExampleFile_GetSheetFormatPr() {
- f := NewFile()
- const sheet = "Sheet1"
- var (
- baseColWidth BaseColWidth
- defaultColWidth DefaultColWidth
- defaultRowHeight DefaultRowHeight
- customHeight CustomHeight
- zeroHeight ZeroHeight
- thickTop ThickTop
- thickBottom ThickBottom
- )
- 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 {
- container SheetFormatPrOptionsPtr
- nonDefault SheetFormatPrOptions
- }{
- {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)},
- }
- for i, test := range testData {
- t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
- opt := test.nonDefault
- t.Logf("option %T", opt)
- def := deepcopy.Copy(test.container).(SheetFormatPrOptionsPtr)
- val1 := deepcopy.Copy(def).(SheetFormatPrOptionsPtr)
- val2 := deepcopy.Copy(def).(SheetFormatPrOptionsPtr)
- f := NewFile()
- // Get the default value
- assert.NoError(t, f.GetSheetFormatPr(sheet, def), opt)
- // Get again and check
- assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opt)
- if !assert.Equal(t, val1, def, opt) {
- t.FailNow()
- }
- // Set the same value
- assert.NoError(t, f.SetSheetFormatPr(sheet, val1), opt)
- // Get again and check
- assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opt)
- if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
- t.FailNow()
- }
- // Set a different value
- assert.NoError(t, f.SetSheetFormatPr(sheet, test.nonDefault), opt)
- assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opt)
- // Get again and compare
- assert.NoError(t, f.GetSheetFormatPr(sheet, val2), opt)
- if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
- t.FailNow()
- }
- // Value should not be the same as the default
- if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
- t.FailNow()
- }
- // Restore the default value
- assert.NoError(t, f.SetSheetFormatPr(sheet, def), opt)
- assert.NoError(t, f.GetSheetFormatPr(sheet, val1), opt)
- if !assert.Equal(t, def, val1) {
- t.FailNow()
- }
- })
- }
- }
- func TestSetSheetFormatPr(t *testing.T) {
- f := NewFile()
- assert.NoError(t, f.GetSheetFormatPr("Sheet1"))
- ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
- assert.True(t, ok)
- ws.(*xlsxWorksheet).SheetFormatPr = nil
- assert.NoError(t, f.SetSheetFormatPr("Sheet1", BaseColWidth(1.0)))
- // Test set formatting properties on not exists worksheet.
- assert.EqualError(t, f.SetSheetFormatPr("SheetN"), "sheet SheetN is not exist")
- }
- func TestGetSheetFormatPr(t *testing.T) {
- f := NewFile()
- assert.NoError(t, f.GetSheetFormatPr("Sheet1"))
- ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
- assert.True(t, ok)
- ws.(*xlsxWorksheet).SheetFormatPr = nil
- var (
- baseColWidth BaseColWidth
- defaultColWidth DefaultColWidth
- defaultRowHeight DefaultRowHeight
- customHeight CustomHeight
- zeroHeight ZeroHeight
- thickTop ThickTop
- thickBottom ThickBottom
- )
- assert.NoError(t, f.GetSheetFormatPr("Sheet1",
- &baseColWidth,
- &defaultColWidth,
- &defaultRowHeight,
- &customHeight,
- &zeroHeight,
- &thickTop,
- &thickBottom,
- ))
- // Test get formatting properties on not exists worksheet.
- assert.EqualError(t, f.GetSheetFormatPr("SheetN"), "sheet SheetN is not exist")
- }
|