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),
|
|
|
|
ShowFormulas(false),
|
|
|
|
ShowGridLines(true),
|
|
|
|
ShowRowColHeaders(true),
|
2022-02-08 00:08:06 +08:00
|
|
|
ShowZeros(true),
|
|
|
|
RightToLeft(false),
|
2022-02-06 21:52:28 +08:00
|
|
|
ShowRuler(false),
|
2022-02-08 00:08:06 +08:00
|
|
|
View("pageLayout"),
|
|
|
|
TopLeftCell("B2"),
|
|
|
|
ZoomScale(100),
|
2017-11-18 02:43:32 +08:00
|
|
|
// SheetViewOptionPtr are also SheetViewOption
|
2020-10-04 21:07:39 +08:00
|
|
|
new(DefaultGridColor),
|
|
|
|
new(ShowFormulas),
|
|
|
|
new(ShowGridLines),
|
|
|
|
new(ShowRowColHeaders),
|
2022-02-08 00:08:06 +08:00
|
|
|
new(ShowZeros),
|
|
|
|
new(RightToLeft),
|
|
|
|
new(ShowRuler),
|
|
|
|
new(View),
|
2020-10-04 21:07:39 +08:00
|
|
|
new(TopLeftCell),
|
2022-02-08 00:08:06 +08:00
|
|
|
new(ZoomScale),
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
var _ = []SheetViewOptionPtr{
|
|
|
|
(*DefaultGridColor)(nil),
|
|
|
|
(*ShowFormulas)(nil),
|
|
|
|
(*ShowGridLines)(nil),
|
|
|
|
(*ShowRowColHeaders)(nil),
|
2022-02-08 00:08:06 +08:00
|
|
|
(*ShowZeros)(nil),
|
|
|
|
(*RightToLeft)(nil),
|
2022-02-06 21:52:28 +08:00
|
|
|
(*ShowRuler)(nil),
|
2022-02-08 00:08:06 +08:00
|
|
|
(*View)(nil),
|
|
|
|
(*TopLeftCell)(nil),
|
|
|
|
(*ZoomScale)(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),
|
|
|
|
ShowFormulas(true),
|
|
|
|
ShowGridLines(true),
|
|
|
|
ShowRowColHeaders(true),
|
2022-02-08 00:08:06 +08:00
|
|
|
RightToLeft(false),
|
2022-02-06 21:52:28 +08:00
|
|
|
ShowRuler(false),
|
2022-02-08 00:08:06 +08:00
|
|
|
View("pageLayout"),
|
|
|
|
TopLeftCell("C3"),
|
|
|
|
ZoomScale(80),
|
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
|
|
|
|
showFormulas ShowFormulas
|
|
|
|
showGridLines ShowGridLines
|
|
|
|
showRowColHeaders ShowRowColHeaders
|
2022-02-08 00:08:06 +08:00
|
|
|
showZeros ShowZeros
|
|
|
|
rightToLeft RightToLeft
|
|
|
|
showRuler ShowRuler
|
|
|
|
view View
|
2020-10-04 21:07:39 +08:00
|
|
|
topLeftCell TopLeftCell
|
2022-02-08 00:08:06 +08:00
|
|
|
zoomScale ZoomScale
|
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,
|
|
|
|
&showFormulas,
|
|
|
|
&showGridLines,
|
|
|
|
&showRowColHeaders,
|
2022-02-08 00:08:06 +08:00
|
|
|
&showZeros,
|
|
|
|
&rightToLeft,
|
|
|
|
&showRuler,
|
|
|
|
&view,
|
2018-12-18 21:50:07 +08:00
|
|
|
&topLeftCell,
|
2022-02-08 00:08:06 +08:00
|
|
|
&zoomScale,
|
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("- showFormulas:", showFormulas)
|
|
|
|
fmt.Println("- showGridLines:", showGridLines)
|
|
|
|
fmt.Println("- showRowColHeaders:", showRowColHeaders)
|
2022-02-08 00:08:06 +08:00
|
|
|
fmt.Println("- showZeros:", showZeros)
|
|
|
|
fmt.Println("- rightToLeft:", rightToLeft)
|
|
|
|
fmt.Println("- showRuler:", showRuler)
|
|
|
|
fmt.Println("- view:", view)
|
2018-12-18 21:50:07 +08:00
|
|
|
fmt.Println("- topLeftCell:", `"`+topLeftCell+`"`)
|
2022-02-08 00:08:06 +08:00
|
|
|
fmt.Println("- zoomScale:", zoomScale)
|
2018-12-18 21:50:07 +08:00
|
|
|
|
2022-02-08 00:08:06 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, ShowGridLines(false)); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2018-12-18 21:50:07 +08:00
|
|
|
}
|
|
|
|
|
2022-02-08 00:08:06 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &showGridLines); 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
|
|
|
|
2022-02-08 00:08:06 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, ShowZeros(false)); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
2022-02-08 00:08:06 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &showZeros); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
2022-02-08 00:08:06 +08:00
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, View("pageLayout")); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2019-09-30 20:37:52 +08:00
|
|
|
}
|
|
|
|
|
2022-02-08 00:08:06 +08:00
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &view); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.SetSheetViewOptions(sheet, 0, TopLeftCell("B2")); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.GetSheetViewOptions(sheet, 0, &topLeftCell); 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)
|
2022-02-08 00:08:06 +08:00
|
|
|
fmt.Println("- view:", view)
|
2018-12-18 21:50:07 +08:00
|
|
|
fmt.Println("- topLeftCell:", topLeftCell)
|
2017-11-05 09:16:41 +08:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Default:
|
|
|
|
// - defaultGridColor: true
|
|
|
|
// - showFormulas: false
|
|
|
|
// - showGridLines: true
|
|
|
|
// - showRowColHeaders: true
|
2022-02-08 00:08:06 +08:00
|
|
|
// - showZeros: true
|
|
|
|
// - rightToLeft: false
|
|
|
|
// - showRuler: true
|
|
|
|
// - view: normal
|
2018-12-18 21:50:07 +08:00
|
|
|
// - topLeftCell: ""
|
2022-02-08 00:08:06 +08:00
|
|
|
// - zoomScale: 0
|
2017-11-05 09:16:41 +08:00
|
|
|
// After change:
|
|
|
|
// - showGridLines: false
|
2019-09-30 20:37:52 +08:00
|
|
|
// - showZeros: false
|
2022-02-08 00:08:06 +08:00
|
|
|
// - view: pageLayout
|
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
|
|
|
}
|