2019-01-06 14:12:31 +08:00
|
|
|
package excelize_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-05 16:25:57 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-01-06 14:12:31 +08:00
|
|
|
"testing"
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
"github.com/360EntSecGroup-Skylar/excelize/v2"
|
2019-01-06 14:12:31 +08:00
|
|
|
"github.com/mohae/deepcopy"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleFile_SetPageLayout() {
|
2019-05-05 16:25:57 +08:00
|
|
|
f := excelize.NewFile()
|
2019-01-06 14:12:31 +08:00
|
|
|
|
2019-05-05 16:25:57 +08:00
|
|
|
if err := f.SetPageLayout(
|
2019-01-06 14:12:31 +08:00
|
|
|
"Sheet1",
|
|
|
|
excelize.PageLayoutOrientation(excelize.OrientationLandscape),
|
|
|
|
); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-05-05 16:25:57 +08:00
|
|
|
if err := f.SetPageLayout(
|
2019-01-13 21:58:50 +08:00
|
|
|
"Sheet1",
|
|
|
|
excelize.PageLayoutPaperSize(10),
|
|
|
|
); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-06 14:12:31 +08:00
|
|
|
// Output:
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_GetPageLayout() {
|
2019-05-05 16:25:57 +08:00
|
|
|
f := excelize.NewFile()
|
2019-01-06 14:12:31 +08:00
|
|
|
var (
|
|
|
|
orientation excelize.PageLayoutOrientation
|
2019-01-13 21:58:50 +08:00
|
|
|
paperSize excelize.PageLayoutPaperSize
|
2019-01-06 14:12:31 +08:00
|
|
|
)
|
2019-05-05 16:25:57 +08:00
|
|
|
if err := f.GetPageLayout("Sheet1", &orientation); err != nil {
|
2019-01-06 14:12:31 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-05-05 16:25:57 +08:00
|
|
|
if err := f.GetPageLayout("Sheet1", &paperSize); err != nil {
|
2019-01-13 21:58:50 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-06 14:12:31 +08:00
|
|
|
fmt.Println("Defaults:")
|
|
|
|
fmt.Printf("- orientation: %q\n", orientation)
|
2019-01-13 21:58:50 +08:00
|
|
|
fmt.Printf("- paper size: %d\n", paperSize)
|
2019-01-06 14:12:31 +08:00
|
|
|
// Output:
|
|
|
|
// Defaults:
|
|
|
|
// - orientation: "portrait"
|
2019-01-13 21:58:50 +08:00
|
|
|
// - paper size: 1
|
2019-01-06 14:12:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPageLayoutOption(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
testData := []struct {
|
|
|
|
container excelize.PageLayoutOptionPtr
|
|
|
|
nonDefault excelize.PageLayoutOption
|
|
|
|
}{
|
|
|
|
{new(excelize.PageLayoutOrientation), excelize.PageLayoutOrientation(excelize.OrientationLandscape)},
|
2019-01-13 21:58:50 +08:00
|
|
|
{new(excelize.PageLayoutPaperSize), excelize.PageLayoutPaperSize(10)},
|
2019-01-06 14:12:31 +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.PageLayoutOptionPtr)
|
|
|
|
val1 := deepcopy.Copy(def).(excelize.PageLayoutOptionPtr)
|
|
|
|
val2 := deepcopy.Copy(def).(excelize.PageLayoutOptionPtr)
|
|
|
|
|
2019-05-05 16:25:57 +08:00
|
|
|
f := excelize.NewFile()
|
2019-01-06 14:12:31 +08:00
|
|
|
// Get the default value
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, def), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
// Get again and check
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, val1), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
if !assert.Equal(t, val1, def, opt) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set the same value
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.SetPageLayout(sheet, val1), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
// Get again and check
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, val1), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set a different value
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.SetPageLayout(sheet, test.nonDefault), opt)
|
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, val1), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
// Get again and compare
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, val2), opt)
|
2019-01-06 14:12:31 +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-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.SetPageLayout(sheet, def), opt)
|
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, val1), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
if !assert.Equal(t, def, val1) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 10:57:21 +08:00
|
|
|
|
|
|
|
func TestSetPageLayout(t *testing.T) {
|
|
|
|
f := excelize.NewFile()
|
|
|
|
// Test set page layout on not exists worksheet.
|
|
|
|
assert.EqualError(t, f.SetPageLayout("SheetN"), "sheet SheetN is not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPageLayout(t *testing.T) {
|
|
|
|
f := excelize.NewFile()
|
|
|
|
// Test get page layout on not exists worksheet.
|
|
|
|
assert.EqualError(t, f.GetPageLayout("SheetN"), "sheet SheetN is not exist")
|
|
|
|
}
|
2019-05-05 16:25:57 +08:00
|
|
|
|
|
|
|
func TestSetHeaderFooter(t *testing.T) {
|
|
|
|
f := excelize.NewFile()
|
|
|
|
f.SetCellStr("Sheet1", "A1", "Test SetHeaderFooter")
|
|
|
|
// Test set header and footer on not exists worksheet.
|
|
|
|
assert.EqualError(t, f.SetHeaderFooter("SheetN", nil), "sheet SheetN is not exist")
|
|
|
|
// Test set header and footer with illegal setting.
|
|
|
|
assert.EqualError(t, f.SetHeaderFooter("Sheet1", &excelize.FormatHeaderFooter{
|
|
|
|
OddHeader: strings.Repeat("c", 256),
|
|
|
|
}), "field OddHeader must be less than 255 characters")
|
|
|
|
|
|
|
|
assert.NoError(t, f.SetHeaderFooter("Sheet1", nil))
|
|
|
|
assert.NoError(t, f.SetHeaderFooter("Sheet1", &excelize.FormatHeaderFooter{
|
|
|
|
DifferentFirst: true,
|
|
|
|
DifferentOddEven: true,
|
|
|
|
OddHeader: "&R&P",
|
|
|
|
OddFooter: "&C&F",
|
|
|
|
EvenHeader: "&L&P",
|
|
|
|
EvenFooter: "&L&D&R&T",
|
|
|
|
FirstHeader: `&CCenter &"-,Bold"Bold&"-,Regular"HeaderU+000A&D`,
|
|
|
|
}))
|
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetHeaderFooter.xlsx")))
|
|
|
|
}
|