2019-01-01 13:20:14 +08:00
|
|
|
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
|
2018-09-14 00:44:23 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
|
|
|
// Package excelize providing a set of functions that allow you to write to
|
|
|
|
// and read from XLSX files. Support reads and writes XLSX file generated by
|
|
|
|
// Microsoft Excel™ 2007 and later. Support save file without losing original
|
|
|
|
// charts of XLSX. This library needs Go version 1.8 or later.
|
2018-09-14 00:58:48 +08:00
|
|
|
|
2017-11-05 09:16:41 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// SheetViewOption is an option of a view of a worksheet. See SetSheetViewOptions().
|
|
|
|
type SheetViewOption interface {
|
|
|
|
setSheetViewOption(view *xlsxSheetView)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SheetViewOptionPtr is a writable SheetViewOption. See GetSheetViewOptions().
|
|
|
|
type SheetViewOptionPtr interface {
|
2017-11-18 02:43:32 +08:00
|
|
|
SheetViewOption
|
2017-11-05 09:16:41 +08:00
|
|
|
getSheetViewOption(view *xlsxSheetView)
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
// DefaultGridColor is a SheetViewOption.
|
|
|
|
DefaultGridColor bool
|
|
|
|
// RightToLeft is a SheetViewOption.
|
|
|
|
RightToLeft bool
|
|
|
|
// ShowFormulas is a SheetViewOption.
|
|
|
|
ShowFormulas bool
|
|
|
|
// ShowGridLines is a SheetViewOption.
|
|
|
|
ShowGridLines bool
|
|
|
|
// ShowRowColHeaders is a SheetViewOption.
|
|
|
|
ShowRowColHeaders bool
|
2018-04-16 03:56:47 +08:00
|
|
|
// ZoomScale is a SheetViewOption.
|
|
|
|
ZoomScale float64
|
2018-12-18 21:50:07 +08:00
|
|
|
// TopLeftCell is a SheetViewOption.
|
|
|
|
TopLeftCell string
|
2017-11-05 09:16:41 +08:00
|
|
|
/* TODO
|
|
|
|
// ShowWhiteSpace is a SheetViewOption.
|
|
|
|
ShowWhiteSpace bool
|
|
|
|
// ShowZeros is a SheetViewOption.
|
|
|
|
ShowZeros bool
|
|
|
|
// WindowProtection is a SheetViewOption.
|
|
|
|
WindowProtection bool
|
|
|
|
*/
|
|
|
|
)
|
|
|
|
|
|
|
|
// Defaults for each option are described in XML schema for CT_SheetView
|
|
|
|
|
2018-12-18 21:50:07 +08:00
|
|
|
func (o TopLeftCell) setSheetViewOption(view *xlsxSheetView) {
|
|
|
|
view.TopLeftCell = string(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *TopLeftCell) getSheetViewOption(view *xlsxSheetView) {
|
|
|
|
*o = TopLeftCell(string(view.TopLeftCell))
|
|
|
|
}
|
|
|
|
|
2017-11-05 09:16:41 +08:00
|
|
|
func (o DefaultGridColor) setSheetViewOption(view *xlsxSheetView) {
|
|
|
|
view.DefaultGridColor = boolPtr(bool(o))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *DefaultGridColor) getSheetViewOption(view *xlsxSheetView) {
|
|
|
|
*o = DefaultGridColor(defaultTrue(view.DefaultGridColor)) // Excel default: true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o RightToLeft) setSheetViewOption(view *xlsxSheetView) {
|
|
|
|
view.RightToLeft = bool(o) // Excel default: false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *RightToLeft) getSheetViewOption(view *xlsxSheetView) {
|
|
|
|
*o = RightToLeft(view.RightToLeft)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o ShowFormulas) setSheetViewOption(view *xlsxSheetView) {
|
|
|
|
view.ShowFormulas = bool(o) // Excel default: false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ShowFormulas) getSheetViewOption(view *xlsxSheetView) {
|
|
|
|
*o = ShowFormulas(view.ShowFormulas) // Excel default: false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o ShowGridLines) setSheetViewOption(view *xlsxSheetView) {
|
|
|
|
view.ShowGridLines = boolPtr(bool(o))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ShowGridLines) getSheetViewOption(view *xlsxSheetView) {
|
|
|
|
*o = ShowGridLines(defaultTrue(view.ShowGridLines)) // Excel default: true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o ShowRowColHeaders) setSheetViewOption(view *xlsxSheetView) {
|
|
|
|
view.ShowRowColHeaders = boolPtr(bool(o))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ShowRowColHeaders) getSheetViewOption(view *xlsxSheetView) {
|
|
|
|
*o = ShowRowColHeaders(defaultTrue(view.ShowRowColHeaders)) // Excel default: true
|
|
|
|
}
|
|
|
|
|
2018-04-16 03:56:47 +08:00
|
|
|
func (o ZoomScale) setSheetViewOption(view *xlsxSheetView) {
|
|
|
|
//This attribute is restricted to values ranging from 10 to 400.
|
|
|
|
if float64(o) >= 10 && float64(o) <= 400 {
|
|
|
|
view.ZoomScale = float64(o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ZoomScale) getSheetViewOption(view *xlsxSheetView) {
|
|
|
|
*o = ZoomScale(view.ZoomScale)
|
|
|
|
}
|
|
|
|
|
2017-11-05 09:16:41 +08:00
|
|
|
// getSheetView returns the SheetView object
|
|
|
|
func (f *File) getSheetView(sheetName string, viewIndex int) (*xlsxSheetView, error) {
|
2019-04-15 11:22:57 +08:00
|
|
|
xlsx, err := f.workSheetReader(sheetName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-05 09:16:41 +08:00
|
|
|
if viewIndex < 0 {
|
|
|
|
if viewIndex < -len(xlsx.SheetViews.SheetView) {
|
|
|
|
return nil, fmt.Errorf("view index %d out of range", viewIndex)
|
|
|
|
}
|
|
|
|
viewIndex = len(xlsx.SheetViews.SheetView) + viewIndex
|
|
|
|
} else if viewIndex >= len(xlsx.SheetViews.SheetView) {
|
|
|
|
return nil, fmt.Errorf("view index %d out of range", viewIndex)
|
|
|
|
}
|
|
|
|
|
2019-04-15 11:22:57 +08:00
|
|
|
return &(xlsx.SheetViews.SheetView[viewIndex]), err
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSheetViewOptions sets sheet view options.
|
|
|
|
// The viewIndex may be negative and if so is counted backward (-1 is the last view).
|
|
|
|
//
|
|
|
|
// Available options:
|
|
|
|
// DefaultGridColor(bool)
|
|
|
|
// RightToLeft(bool)
|
|
|
|
// ShowFormulas(bool)
|
|
|
|
// ShowGridLines(bool)
|
|
|
|
// ShowRowColHeaders(bool)
|
|
|
|
// Example:
|
|
|
|
// err = f.SetSheetViewOptions("Sheet1", -1, ShowGridLines(false))
|
|
|
|
func (f *File) SetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOption) error {
|
|
|
|
view, err := f.getSheetView(name, viewIndex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.setSheetViewOption(view)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSheetViewOptions gets the value of sheet view options.
|
|
|
|
// The viewIndex may be negative and if so is counted backward (-1 is the last view).
|
|
|
|
//
|
|
|
|
// Available options:
|
|
|
|
// DefaultGridColor(bool)
|
|
|
|
// RightToLeft(bool)
|
|
|
|
// ShowFormulas(bool)
|
|
|
|
// ShowGridLines(bool)
|
|
|
|
// ShowRowColHeaders(bool)
|
|
|
|
// Example:
|
|
|
|
// var showGridLines excelize.ShowGridLines
|
|
|
|
// err = f.GetSheetViewOptions("Sheet1", -1, &showGridLines)
|
|
|
|
func (f *File) GetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOptionPtr) error {
|
|
|
|
view, err := f.getSheetView(name, viewIndex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.getSheetViewOption(view)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|