2022-01-09 00:20:42 +08:00
|
|
|
// Copyright 2016 - 2022 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
|
2020-06-22 00:14:56 +08:00
|
|
|
// and read from XLSX / XLSM / XLTM files. Supports reading and writing
|
2021-03-30 23:02:22 +08:00
|
|
|
// spreadsheet documents generated by Microsoft Excel™ 2007 and later. Supports
|
2020-06-22 00:14:56 +08:00
|
|
|
// complex components by high compatibility, and provided streaming API for
|
|
|
|
// generating or reading data from a worksheet with huge amounts of data. This
|
2021-04-04 15:29:43 +08:00
|
|
|
// library needs Go version 1.15 or later.
|
2018-09-14 00:58:48 +08:00
|
|
|
|
2017-11-05 09:16:41 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2019-08-06 09:50:45 +08:00
|
|
|
// SheetViewOption is an option of a view of a worksheet. See
|
|
|
|
// SetSheetViewOptions().
|
2017-11-05 09:16:41 +08:00
|
|
|
type SheetViewOption interface {
|
|
|
|
setSheetViewOption(view *xlsxSheetView)
|
|
|
|
}
|
|
|
|
|
2019-08-06 09:50:45 +08:00
|
|
|
// SheetViewOptionPtr is a writable SheetViewOption. See
|
|
|
|
// GetSheetViewOptions().
|
2017-11-05 09:16:41 +08:00
|
|
|
type SheetViewOptionPtr interface {
|
2017-11-18 02:43:32 +08:00
|
|
|
SheetViewOption
|
2017-11-05 09:16:41 +08:00
|
|
|
getSheetViewOption(view *xlsxSheetView)
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
2019-08-06 09:50:45 +08:00
|
|
|
// DefaultGridColor is a SheetViewOption. It specifies a flag indicating that
|
|
|
|
// the consuming application should use the default grid lines color (system
|
|
|
|
// dependent). Overrides any color specified in colorId.
|
2017-11-05 09:16:41 +08:00
|
|
|
DefaultGridColor bool
|
2019-08-06 09:50:45 +08:00
|
|
|
// RightToLeft is a SheetViewOption. It specifies a flag indicating whether
|
|
|
|
// the sheet is in 'right to left' display mode. When in this mode, Column A
|
|
|
|
// is on the far right, Column B ;is one column left of Column A, and so on.
|
|
|
|
// Also, information in cells is displayed in the Right to Left format.
|
2017-11-05 09:16:41 +08:00
|
|
|
RightToLeft bool
|
2019-08-06 09:50:45 +08:00
|
|
|
// ShowFormulas is a SheetViewOption. It specifies a flag indicating whether
|
|
|
|
// this sheet should display formulas.
|
2017-11-05 09:16:41 +08:00
|
|
|
ShowFormulas bool
|
2019-08-06 09:50:45 +08:00
|
|
|
// ShowGridLines is a SheetViewOption. It specifies a flag indicating whether
|
|
|
|
// this sheet should display gridlines.
|
2017-11-05 09:16:41 +08:00
|
|
|
ShowGridLines bool
|
2019-08-06 09:50:45 +08:00
|
|
|
// ShowRowColHeaders is a SheetViewOption. It specifies a flag indicating
|
|
|
|
// whether the sheet should display row and column headings.
|
2017-11-05 09:16:41 +08:00
|
|
|
ShowRowColHeaders bool
|
2019-08-06 09:50:45 +08:00
|
|
|
// ZoomScale is a SheetViewOption. It specifies a window zoom magnification
|
|
|
|
// for current view representing percent values. This attribute is restricted
|
|
|
|
// to values ranging from 10 to 400. Horizontal & Vertical scale together.
|
2018-04-16 03:56:47 +08:00
|
|
|
ZoomScale float64
|
2019-08-06 09:50:45 +08:00
|
|
|
// TopLeftCell is a SheetViewOption. It specifies a location of the top left
|
|
|
|
// visible cell Location of the top left visible cell in the bottom right
|
|
|
|
// pane (when in Left-to-Right mode).
|
2018-12-18 21:50:07 +08:00
|
|
|
TopLeftCell string
|
2019-09-30 20:37:52 +08:00
|
|
|
// ShowZeros is a SheetViewOption. It specifies a flag indicating
|
|
|
|
// whether to "show a zero in cells that have zero value".
|
|
|
|
// When using a formula to reference another cell which is empty, the referenced value becomes 0
|
|
|
|
// when the flag is true. (Default setting is true.)
|
|
|
|
ShowZeros bool
|
2022-02-04 13:45:42 +08:00
|
|
|
// View is a SheetViewOption. It specifies a flag indicating
|
|
|
|
// how sheet is displayed, by default it uses empty string
|
2022-02-06 21:52:28 +08:00
|
|
|
// available options: normal, pageLayout, pageBreakPreview
|
2022-02-04 13:45:42 +08:00
|
|
|
View string
|
2022-02-06 21:52:28 +08:00
|
|
|
// ShowRuler is a SheetViewOption. It specifies a flag indicating
|
|
|
|
// this sheet should display ruler.
|
|
|
|
ShowRuler bool
|
|
|
|
|
2017-11-05 09:16:41 +08:00
|
|
|
/* TODO
|
2019-08-06 09:50:45 +08:00
|
|
|
// ShowWhiteSpace is a SheetViewOption. It specifies a flag indicating
|
|
|
|
// whether page layout view shall display margins. False means do not display
|
|
|
|
// left, right, top (header), and bottom (footer) margins (even when there is
|
|
|
|
// data in the header or footer).
|
2017-11-05 09:16:41 +08:00
|
|
|
ShowWhiteSpace 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))
|
|
|
|
}
|
|
|
|
|
2022-02-04 13:45:42 +08:00
|
|
|
func (o View) setSheetViewOption(view *xlsxSheetView) {
|
|
|
|
view.View = string(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *View) getSheetViewOption(view *xlsxSheetView) {
|
|
|
|
*o = View(string(view.View))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-06 21:52:28 +08:00
|
|
|
func (o ShowRuler) setSheetViewOption(view *xlsxSheetView) {
|
|
|
|
view.ShowRuler = boolPtr(bool(o))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ShowRuler) getSheetViewOption(view *xlsxSheetView) {
|
|
|
|
*o = ShowRuler(defaultTrue(view.ShowRuler)) // Excel default: true
|
|
|
|
}
|
|
|
|
|
2019-09-30 20:37:52 +08:00
|
|
|
func (o ShowZeros) setSheetViewOption(view *xlsxSheetView) {
|
|
|
|
view.ShowZeros = boolPtr(bool(o))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ShowZeros) getSheetViewOption(view *xlsxSheetView) {
|
|
|
|
*o = ShowZeros(defaultTrue(view.ShowZeros)) // Excel default: true
|
|
|
|
}
|
|
|
|
|
2017-11-05 09:16:41 +08:00
|
|
|
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) {
|
2019-07-08 20:17:47 +08:00
|
|
|
// This attribute is restricted to values ranging from 10 to 400.
|
2018-04-16 03:56:47 +08:00
|
|
|
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
|
2020-11-10 23:48:09 +08:00
|
|
|
func (f *File) getSheetView(sheet string, viewIndex int) (*xlsxSheetView, error) {
|
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-04-15 11:22:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-05 09:16:41 +08:00
|
|
|
if viewIndex < 0 {
|
2020-11-10 23:48:09 +08:00
|
|
|
if viewIndex < -len(ws.SheetViews.SheetView) {
|
2017-11-05 09:16:41 +08:00
|
|
|
return nil, fmt.Errorf("view index %d out of range", viewIndex)
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
viewIndex = len(ws.SheetViews.SheetView) + viewIndex
|
|
|
|
} else if viewIndex >= len(ws.SheetViews.SheetView) {
|
2017-11-05 09:16:41 +08:00
|
|
|
return nil, fmt.Errorf("view index %d out of range", viewIndex)
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:48:09 +08:00
|
|
|
return &(ws.SheetViews.SheetView[viewIndex]), err
|
2017-11-05 09:16:41 +08:00
|
|
|
}
|
|
|
|
|
2019-08-06 09:50:45 +08:00
|
|
|
// SetSheetViewOptions sets sheet view options. The viewIndex may be negative
|
|
|
|
// and if so is counted backward (-1 is the last view).
|
2017-11-05 09:16:41 +08:00
|
|
|
//
|
|
|
|
// Available options:
|
2019-08-06 09:50:45 +08:00
|
|
|
//
|
2017-11-05 09:16:41 +08:00
|
|
|
// DefaultGridColor(bool)
|
|
|
|
// RightToLeft(bool)
|
|
|
|
// ShowFormulas(bool)
|
|
|
|
// ShowGridLines(bool)
|
|
|
|
// ShowRowColHeaders(bool)
|
2019-07-08 20:17:47 +08:00
|
|
|
// ZoomScale(float64)
|
|
|
|
// TopLeftCell(string)
|
2021-02-08 18:05:15 +08:00
|
|
|
// ShowZeros(bool)
|
2019-08-06 09:50:45 +08:00
|
|
|
//
|
2017-11-05 09:16:41 +08:00
|
|
|
// Example:
|
2019-08-06 09:50:45 +08:00
|
|
|
//
|
2017-11-05 09:16:41 +08:00
|
|
|
// err = f.SetSheetViewOptions("Sheet1", -1, ShowGridLines(false))
|
2019-08-06 09:50:45 +08:00
|
|
|
//
|
2017-11-05 09:16:41 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-06 09:50:45 +08:00
|
|
|
// GetSheetViewOptions gets the value of sheet view options. The viewIndex may
|
|
|
|
// be negative and if so is counted backward (-1 is the last view).
|
2017-11-05 09:16:41 +08:00
|
|
|
//
|
|
|
|
// Available options:
|
2019-08-06 09:50:45 +08:00
|
|
|
//
|
2017-11-05 09:16:41 +08:00
|
|
|
// DefaultGridColor(bool)
|
|
|
|
// RightToLeft(bool)
|
|
|
|
// ShowFormulas(bool)
|
|
|
|
// ShowGridLines(bool)
|
|
|
|
// ShowRowColHeaders(bool)
|
2019-07-08 20:17:47 +08:00
|
|
|
// ZoomScale(float64)
|
|
|
|
// TopLeftCell(string)
|
2021-02-08 18:05:15 +08:00
|
|
|
// ShowZeros(bool)
|
2019-08-06 09:50:45 +08:00
|
|
|
//
|
2017-11-05 09:16:41 +08:00
|
|
|
// Example:
|
2019-08-06 09:50:45 +08:00
|
|
|
//
|
2017-11-05 09:16:41 +08:00
|
|
|
// var showGridLines excelize.ShowGridLines
|
|
|
|
// err = f.GetSheetViewOptions("Sheet1", -1, &showGridLines)
|
2019-08-06 09:50:45 +08:00
|
|
|
//
|
2017-11-05 09:16:41 +08:00
|
|
|
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
|
|
|
|
}
|