2017-11-14 22:09:50 +08:00
|
|
|
package excelize_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mohae/deepcopy"
|
2018-12-27 18:51:44 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2019-09-13 18:38:44 +08:00
|
|
|
"github.com/360EntSecGroup-Skylar/excelize/v2"
|
2017-11-14 22:09:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = []excelize.SheetPrOption{
|
|
|
|
excelize.CodeName("hello"),
|
|
|
|
excelize.EnableFormatConditionsCalculation(false),
|
|
|
|
excelize.Published(false),
|
|
|
|
excelize.FitToPage(true),
|
|
|
|
excelize.AutoPageBreaks(true),
|
2018-12-14 03:01:36 +08:00
|
|
|
excelize.OutlineSummaryBelow(true),
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ = []excelize.SheetPrOptionPtr{
|
|
|
|
(*excelize.CodeName)(nil),
|
|
|
|
(*excelize.EnableFormatConditionsCalculation)(nil),
|
|
|
|
(*excelize.Published)(nil),
|
|
|
|
(*excelize.FitToPage)(nil),
|
|
|
|
(*excelize.AutoPageBreaks)(nil),
|
2018-12-14 03:01:36 +08:00
|
|
|
(*excelize.OutlineSummaryBelow)(nil),
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_SetSheetPrOptions() {
|
2019-04-30 18:39:27 +08:00
|
|
|
f := excelize.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,
|
2017-11-14 22:09:50 +08:00
|
|
|
excelize.CodeName("code"),
|
|
|
|
excelize.EnableFormatConditionsCalculation(false),
|
|
|
|
excelize.Published(false),
|
|
|
|
excelize.FitToPage(true),
|
|
|
|
excelize.AutoPageBreaks(true),
|
2018-12-14 03:01:36 +08:00
|
|
|
excelize.OutlineSummaryBelow(false),
|
2017-11-14 22:09:50 +08:00
|
|
|
); err != nil {
|
2020-01-03 23:57:25 +08:00
|
|
|
println(err.Error())
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|
|
|
|
// Output:
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_GetSheetPrOptions() {
|
2019-04-30 18:39:27 +08:00
|
|
|
f := excelize.NewFile()
|
2017-11-14 22:09:50 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
var (
|
|
|
|
codeName excelize.CodeName
|
|
|
|
enableFormatConditionsCalculation excelize.EnableFormatConditionsCalculation
|
|
|
|
published excelize.Published
|
|
|
|
fitToPage excelize.FitToPage
|
|
|
|
autoPageBreaks excelize.AutoPageBreaks
|
2018-12-14 03:01:36 +08:00
|
|
|
outlineSummaryBelow excelize.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,
|
|
|
|
&autoPageBreaks,
|
2018-12-14 03:01:36 +08:00
|
|
|
&outlineSummaryBelow,
|
2017-11-14 22:09:50 +08:00
|
|
|
); err != nil {
|
2020-01-03 23:57:25 +08:00
|
|
|
println(err.Error())
|
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)
|
|
|
|
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
|
|
|
|
// - 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 {
|
2017-11-14 22:09:50 +08:00
|
|
|
container excelize.SheetPrOptionPtr
|
|
|
|
nonDefault excelize.SheetPrOption
|
|
|
|
}{
|
|
|
|
{new(excelize.CodeName), excelize.CodeName("xx")},
|
|
|
|
{new(excelize.EnableFormatConditionsCalculation), excelize.EnableFormatConditionsCalculation(false)},
|
|
|
|
{new(excelize.Published), excelize.Published(false)},
|
|
|
|
{new(excelize.FitToPage), excelize.FitToPage(true)},
|
|
|
|
{new(excelize.AutoPageBreaks), excelize.AutoPageBreaks(true)},
|
2018-12-14 03:01:36 +08:00
|
|
|
{new(excelize.OutlineSummaryBelow), excelize.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) {
|
|
|
|
|
|
|
|
opt := test.nonDefault
|
|
|
|
t.Logf("option %T", opt)
|
|
|
|
|
|
|
|
def := deepcopy.Copy(test.container).(excelize.SheetPrOptionPtr)
|
|
|
|
val1 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
|
|
|
|
val2 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
f := excelize.NewFile()
|
2018-12-27 18:51:44 +08:00
|
|
|
// Get the default value
|
2019-04-30 18:39:27 +08:00
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, def), opt)
|
2018-12-27 18:51:44 +08:00
|
|
|
// Get again and check
|
2019-04-30 18:39:27 +08:00
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
|
2018-12-27 18:51:44 +08:00
|
|
|
if !assert.Equal(t, val1, def, opt) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set the same value
|
2019-04-30 18:39:27 +08:00
|
|
|
assert.NoError(t, f.SetSheetPrOptions(sheet, val1), opt)
|
2018-12-27 18:51:44 +08:00
|
|
|
// Get again and check
|
2019-04-30 18:39:27 +08:00
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
|
2018-12-27 18:51:44 +08:00
|
|
|
if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set a different value
|
2019-04-30 18:39:27 +08:00
|
|
|
assert.NoError(t, f.SetSheetPrOptions(sheet, test.nonDefault), opt)
|
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
|
2018-12-27 18:51:44 +08:00
|
|
|
// Get again and compare
|
2019-04-30 18:39:27 +08:00
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, val2), opt)
|
2018-12-27 18:51:44 +08:00
|
|
|
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
|
2019-04-30 18:39:27 +08:00
|
|
|
assert.NoError(t, f.SetSheetPrOptions(sheet, def), opt)
|
|
|
|
assert.NoError(t, f.GetSheetPrOptions(sheet, val1), opt)
|
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
|
|
|
|
|
|
|
func TestSetSheetrOptions(t *testing.T) {
|
|
|
|
f := excelize.NewFile()
|
|
|
|
// Test SetSheetrOptions on not exists worksheet.
|
|
|
|
assert.EqualError(t, f.SetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSheetPrOptions(t *testing.T) {
|
|
|
|
f := excelize.NewFile()
|
|
|
|
// Test GetSheetPrOptions on not exists worksheet.
|
|
|
|
assert.EqualError(t, f.GetSheetPrOptions("SheetN"), "sheet SheetN is not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = []excelize.PageMarginsOptions{
|
|
|
|
excelize.PageMarginBottom(1.0),
|
|
|
|
excelize.PageMarginFooter(1.0),
|
|
|
|
excelize.PageMarginHeader(1.0),
|
|
|
|
excelize.PageMarginLeft(1.0),
|
|
|
|
excelize.PageMarginRight(1.0),
|
|
|
|
excelize.PageMarginTop(1.0),
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = []excelize.PageMarginsOptionsPtr{
|
|
|
|
(*excelize.PageMarginBottom)(nil),
|
|
|
|
(*excelize.PageMarginFooter)(nil),
|
|
|
|
(*excelize.PageMarginHeader)(nil),
|
|
|
|
(*excelize.PageMarginLeft)(nil),
|
|
|
|
(*excelize.PageMarginRight)(nil),
|
|
|
|
(*excelize.PageMarginTop)(nil),
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_SetPageMargins() {
|
|
|
|
f := excelize.NewFile()
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
if err := f.SetPageMargins(sheet,
|
|
|
|
excelize.PageMarginBottom(1.0),
|
|
|
|
excelize.PageMarginFooter(1.0),
|
|
|
|
excelize.PageMarginHeader(1.0),
|
|
|
|
excelize.PageMarginLeft(1.0),
|
|
|
|
excelize.PageMarginRight(1.0),
|
|
|
|
excelize.PageMarginTop(1.0),
|
|
|
|
); err != nil {
|
2020-01-03 23:57:25 +08:00
|
|
|
println(err.Error())
|
2019-10-16 01:03:29 +08:00
|
|
|
}
|
|
|
|
// Output:
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_GetPageMargins() {
|
|
|
|
f := excelize.NewFile()
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
var (
|
|
|
|
marginBottom excelize.PageMarginBottom
|
|
|
|
marginFooter excelize.PageMarginFooter
|
|
|
|
marginHeader excelize.PageMarginHeader
|
|
|
|
marginLeft excelize.PageMarginLeft
|
|
|
|
marginRight excelize.PageMarginRight
|
|
|
|
marginTop excelize.PageMarginTop
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := f.GetPageMargins(sheet,
|
|
|
|
&marginBottom,
|
|
|
|
&marginFooter,
|
|
|
|
&marginHeader,
|
|
|
|
&marginLeft,
|
|
|
|
&marginRight,
|
|
|
|
&marginTop,
|
|
|
|
); err != nil {
|
2020-01-03 23:57:25 +08:00
|
|
|
println(err.Error())
|
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 {
|
|
|
|
container excelize.PageMarginsOptionsPtr
|
|
|
|
nonDefault excelize.PageMarginsOptions
|
|
|
|
}{
|
|
|
|
{new(excelize.PageMarginTop), excelize.PageMarginTop(1.0)},
|
|
|
|
{new(excelize.PageMarginBottom), excelize.PageMarginBottom(1.0)},
|
|
|
|
{new(excelize.PageMarginLeft), excelize.PageMarginLeft(1.0)},
|
|
|
|
{new(excelize.PageMarginRight), excelize.PageMarginRight(1.0)},
|
|
|
|
{new(excelize.PageMarginHeader), excelize.PageMarginHeader(1.0)},
|
|
|
|
{new(excelize.PageMarginFooter), excelize.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).(excelize.PageMarginsOptionsPtr)
|
|
|
|
val1 := deepcopy.Copy(def).(excelize.PageMarginsOptionsPtr)
|
|
|
|
val2 := deepcopy.Copy(def).(excelize.PageMarginsOptionsPtr)
|
|
|
|
|
|
|
|
f := excelize.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 := excelize.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 := excelize.NewFile()
|
|
|
|
// Test get page margins on not exists worksheet.
|
|
|
|
assert.EqualError(t, f.GetPageMargins("SheetN"), "sheet SheetN is not exist")
|
|
|
|
}
|