2017-11-05 09:16:41 +08:00
|
|
|
package excelize_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-12-27 18:51:44 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
"github.com/360EntSecGroup-Skylar/excelize/v2"
|
2017-11-05 09:16:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = []excelize.SheetViewOption{
|
|
|
|
excelize.DefaultGridColor(true),
|
|
|
|
excelize.RightToLeft(false),
|
|
|
|
excelize.ShowFormulas(false),
|
|
|
|
excelize.ShowGridLines(true),
|
|
|
|
excelize.ShowRowColHeaders(true),
|
2018-12-18 21:50:07 +08:00
|
|
|
excelize.TopLeftCell("B2"),
|
2017-11-18 02:43:32 +08:00
|
|
|
// SheetViewOptionPtr are also SheetViewOption
|
|
|
|
new(excelize.DefaultGridColor),
|
|
|
|
new(excelize.RightToLeft),
|
|
|
|
new(excelize.ShowFormulas),
|
|
|
|
new(excelize.ShowGridLines),
|
|
|
|
new(excelize.ShowRowColHeaders),
|
2018-12-18 21:50:07 +08:00
|
|
|
new(excelize.TopLeftCell),
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ = []excelize.SheetViewOptionPtr{
|
|
|
|
(*excelize.DefaultGridColor)(nil),
|
|
|
|
(*excelize.RightToLeft)(nil),
|
|
|
|
(*excelize.ShowFormulas)(nil),
|
|
|
|
(*excelize.ShowGridLines)(nil),
|
|
|
|
(*excelize.ShowRowColHeaders)(nil),
|
2018-12-18 21:50:07 +08:00
|
|
|
(*excelize.TopLeftCell)(nil),
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_SetSheetViewOptions() {
|
2019-04-30 18:39:27 +08:00
|
|
|
f := excelize.NewFile()
|
2017-11-05 09:16:41 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0,
|
2017-11-05 09:16:41 +08:00
|
|
|
excelize.DefaultGridColor(false),
|
|
|
|
excelize.RightToLeft(false),
|
|
|
|
excelize.ShowFormulas(true),
|
|
|
|
excelize.ShowGridLines(true),
|
|
|
|
excelize.ShowRowColHeaders(true),
|
2018-04-16 03:56:47 +08:00
|
|
|
excelize.ZoomScale(80),
|
2018-12-18 21:50:07 +08:00
|
|
|
excelize.TopLeftCell("C3"),
|
2017-11-05 09:16:41 +08:00
|
|
|
); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-04-16 03:56:47 +08:00
|
|
|
|
|
|
|
var zoomScale excelize.ZoomScale
|
|
|
|
fmt.Println("Default:")
|
|
|
|
fmt.Println("- zoomScale: 80")
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(500)); err != nil {
|
2018-04-16 03:56:47 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
|
2018-04-16 03:56:47 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Used out of range value:")
|
|
|
|
fmt.Println("- zoomScale:", zoomScale)
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(123)); err != nil {
|
2018-04-16 03:56:47 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
|
2018-04-16 03:56:47 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Used correct value:")
|
|
|
|
fmt.Println("- zoomScale:", zoomScale)
|
|
|
|
|
2017-11-05 09:16:41 +08:00
|
|
|
// Output:
|
2018-04-16 03:56:47 +08:00
|
|
|
// Default:
|
|
|
|
// - zoomScale: 80
|
|
|
|
// Used out of range value:
|
|
|
|
// - zoomScale: 80
|
|
|
|
// Used correct value:
|
|
|
|
// - zoomScale: 123
|
|
|
|
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_GetSheetViewOptions() {
|
2019-04-30 18:39:27 +08:00
|
|
|
f := excelize.NewFile()
|
2017-11-05 09:16:41 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultGridColor excelize.DefaultGridColor
|
|
|
|
rightToLeft excelize.RightToLeft
|
|
|
|
showFormulas excelize.ShowFormulas
|
|
|
|
showGridLines excelize.ShowGridLines
|
|
|
|
showRowColHeaders excelize.ShowRowColHeaders
|
2018-04-16 03:56:47 +08:00
|
|
|
zoomScale excelize.ZoomScale
|
2018-12-18 21:50:07 +08:00
|
|
|
topLeftCell excelize.TopLeftCell
|
2017-11-05 09:16:41 +08:00
|
|
|
)
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0,
|
2017-11-05 09:16:41 +08:00
|
|
|
&defaultGridColor,
|
|
|
|
&rightToLeft,
|
|
|
|
&showFormulas,
|
|
|
|
&showGridLines,
|
|
|
|
&showRowColHeaders,
|
2018-04-16 03:56:47 +08:00
|
|
|
&zoomScale,
|
2018-12-18 21:50:07 +08:00
|
|
|
&topLeftCell,
|
2017-11-05 09:16:41 +08:00
|
|
|
); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Default:")
|
|
|
|
fmt.Println("- defaultGridColor:", defaultGridColor)
|
|
|
|
fmt.Println("- rightToLeft:", rightToLeft)
|
|
|
|
fmt.Println("- showFormulas:", showFormulas)
|
|
|
|
fmt.Println("- showGridLines:", showGridLines)
|
|
|
|
fmt.Println("- showRowColHeaders:", showRowColHeaders)
|
2018-04-16 03:56:47 +08:00
|
|
|
fmt.Println("- zoomScale:", zoomScale)
|
2018-12-18 21:50:07 +08:00
|
|
|
fmt.Println("- topLeftCell:", `"`+topLeftCell+`"`)
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, excelize.TopLeftCell("B2")); err != nil {
|
2018-12-18 21:50:07 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil {
|
2018-12-18 21:50:07 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2017-11-05 09:16:41 +08:00
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, excelize.ShowGridLines(false)); err != nil {
|
2017-11-05 09:16:41 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &showGridLines); err != nil {
|
2017-11-05 09:16:41 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("After change:")
|
|
|
|
fmt.Println("- showGridLines:", showGridLines)
|
2018-12-18 21:50:07 +08:00
|
|
|
fmt.Println("- topLeftCell:", topLeftCell)
|
2017-11-05 09:16:41 +08:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Default:
|
|
|
|
// - defaultGridColor: true
|
|
|
|
// - rightToLeft: false
|
|
|
|
// - showFormulas: false
|
|
|
|
// - showGridLines: true
|
|
|
|
// - showRowColHeaders: true
|
2018-04-16 03:56:47 +08:00
|
|
|
// - zoomScale: 0
|
2018-12-18 21:50:07 +08:00
|
|
|
// - topLeftCell: ""
|
2017-11-05 09:16:41 +08:00
|
|
|
// After change:
|
|
|
|
// - showGridLines: false
|
2018-12-18 21:50:07 +08:00
|
|
|
// - topLeftCell: B2
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSheetViewOptionsErrors(t *testing.T) {
|
2019-04-30 18:39:27 +08:00
|
|
|
f := excelize.NewFile()
|
2017-11-05 09:16:41 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
assert.NoError(t, f.GetSheetViewOptions(sheet, 0))
|
|
|
|
assert.NoError(t, f.GetSheetViewOptions(sheet, -1))
|
|
|
|
assert.Error(t, f.GetSheetViewOptions(sheet, 1))
|
|
|
|
assert.Error(t, f.GetSheetViewOptions(sheet, -2))
|
|
|
|
assert.NoError(t, f.SetSheetViewOptions(sheet, 0))
|
|
|
|
assert.NoError(t, f.SetSheetViewOptions(sheet, -1))
|
|
|
|
assert.Error(t, f.SetSheetViewOptions(sheet, 1))
|
|
|
|
assert.Error(t, f.SetSheetViewOptions(sheet, -2))
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|