2020-10-04 21:07:39 +08:00
|
|
|
package excelize
|
2017-11-05 09:16:41 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2018-12-27 18:51:44 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-11-05 09:16:41 +08:00
|
|
|
)
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
var _ = []SheetViewOption{
|
|
|
|
DefaultGridColor(true),
|
|
|
|
RightToLeft(false),
|
|
|
|
ShowFormulas(false),
|
|
|
|
ShowGridLines(true),
|
|
|
|
ShowRowColHeaders(true),
|
|
|
|
TopLeftCell("B2"),
|
2022-02-04 13:45:42 +08:00
|
|
|
View("pageLayout"),
|
2022-02-06 21:52:28 +08:00
|
|
|
ShowRuler(false),
|
2017-11-18 02:43:32 +08:00
|
|
|
// SheetViewOptionPtr are also SheetViewOption
|
2020-10-04 21:07:39 +08:00
|
|
|
new(DefaultGridColor),
|
|
|
|
new(RightToLeft),
|
|
|
|
new(ShowFormulas),
|
|
|
|
new(ShowGridLines),
|
|
|
|
new(ShowRowColHeaders),
|
|
|
|
new(TopLeftCell),
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
var _ = []SheetViewOptionPtr{
|
|
|
|
(*DefaultGridColor)(nil),
|
|
|
|
(*RightToLeft)(nil),
|
|
|
|
(*ShowFormulas)(nil),
|
|
|
|
(*ShowGridLines)(nil),
|
|
|
|
(*ShowRowColHeaders)(nil),
|
|
|
|
(*TopLeftCell)(nil),
|
2022-02-04 13:45:42 +08:00
|
|
|
(*View)(nil),
|
2022-02-06 21:52:28 +08:00
|
|
|
(*ShowRuler)(nil),
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_SetSheetViewOptions() {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := 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,
|
2020-10-04 21:07:39 +08:00
|
|
|
DefaultGridColor(false),
|
|
|
|
RightToLeft(false),
|
|
|
|
ShowFormulas(true),
|
|
|
|
ShowGridLines(true),
|
|
|
|
ShowRowColHeaders(true),
|
|
|
|
ZoomScale(80),
|
|
|
|
TopLeftCell("C3"),
|
2022-02-04 13:45:42 +08:00
|
|
|
View("pageLayout"),
|
2022-02-06 21:52:28 +08:00
|
|
|
ShowRuler(false),
|
2017-11-05 09:16:41 +08:00
|
|
|
); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
2018-04-16 03:56:47 +08:00
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
var zoomScale ZoomScale
|
2018-04-16 03:56:47 +08:00
|
|
|
fmt.Println("Default:")
|
|
|
|
fmt.Println("- zoomScale: 80")
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, ZoomScale(500)); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2018-04-16 03:56:47 +08:00
|
|
|
}
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2018-04-16 03:56:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Used out of range value:")
|
|
|
|
fmt.Println("- zoomScale:", zoomScale)
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, ZoomScale(123)); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2018-04-16 03:56:47 +08:00
|
|
|
}
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2018-04-16 03:56:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2017-11-05 09:16:41 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
var (
|
2020-10-04 21:07:39 +08:00
|
|
|
defaultGridColor DefaultGridColor
|
|
|
|
rightToLeft RightToLeft
|
|
|
|
showFormulas ShowFormulas
|
|
|
|
showGridLines ShowGridLines
|
|
|
|
showZeros ShowZeros
|
|
|
|
showRowColHeaders ShowRowColHeaders
|
|
|
|
zoomScale ZoomScale
|
|
|
|
topLeftCell 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,
|
2019-09-30 20:37:52 +08:00
|
|
|
&showZeros,
|
2017-11-05 09:16:41 +08:00
|
|
|
&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 {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Default:")
|
|
|
|
fmt.Println("- defaultGridColor:", defaultGridColor)
|
|
|
|
fmt.Println("- rightToLeft:", rightToLeft)
|
|
|
|
fmt.Println("- showFormulas:", showFormulas)
|
|
|
|
fmt.Println("- showGridLines:", showGridLines)
|
2019-09-30 20:37:52 +08:00
|
|
|
fmt.Println("- showZeros:", showZeros)
|
2017-11-05 09:16:41 +08:00
|
|
|
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+`"`)
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, TopLeftCell("B2")); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2018-12-18 21:50:07 +08:00
|
|
|
}
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2018-12-18 21:50:07 +08:00
|
|
|
}
|
2017-11-05 09:16:41 +08:00
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, ShowGridLines(false)); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
2019-04-30 18:39:27 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &showGridLines); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, ShowZeros(false)); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2019-09-30 20:37:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &showZeros); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2019-09-30 20:37:52 +08:00
|
|
|
}
|
|
|
|
|
2017-11-05 09:16:41 +08:00
|
|
|
fmt.Println("After change:")
|
|
|
|
fmt.Println("- showGridLines:", showGridLines)
|
2019-09-30 20:37:52 +08:00
|
|
|
fmt.Println("- showZeros:", showZeros)
|
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
|
2019-09-30 20:37:52 +08:00
|
|
|
// - showZeros: true
|
2017-11-05 09:16:41 +08:00
|
|
|
// - 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
|
2019-09-30 20:37:52 +08:00
|
|
|
// - showZeros: false
|
2018-12-18 21:50:07 +08:00
|
|
|
// - topLeftCell: B2
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSheetViewOptionsErrors(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := 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
|
|
|
}
|