summaryrefslogtreecommitdiff
path: root/sheetview.go
diff options
context:
space:
mode:
authorOlivier Mengué <dolmen@cpan.org>2017-11-05 02:16:41 +0100
committerOlivier Mengué <dolmen@cpan.org>2017-11-16 12:00:36 +0100
commit88e48e079a91191e05f32a232f8dec4454b25238 (patch)
treec7d42ad32a89db79be454a4017c64026207dc9b0 /sheetview.go
parenta4ffb4fbfc9048998345e979577dab2a81bf57ff (diff)
Add SetSheetViewOptions and GetSheetViewOptions (#145)
Two new methods: - SetSheetViewOptions(sheetName string, viewIndex int, opts ...SheetViewOption) error - GetSheetViewOptions(sheetName string, viewIndex int, opts ...SheetViewOptionPtr) error The option values are given by the user through types that have privates methods that implement the private SheetViewOption and SheetViewOptionPtr interfaces: - DefaultGridColor(bool) - RightToLeft(bool) - ShowFormulas(bool) - ShowGridLines(bool) - ShowRowColHeaders(bool) Examples: err := xl.SetSheetViewOptions("Sheet1", -1, excelize.ShowGridLines(true)) var showGridLines excelize.ShowGridLines err := xl.GetSheetViewOptions("Sheet1", -1, &showGridLines) Fixes #145.
Diffstat (limited to 'sheetview.go')
-rw-r--r--sheetview.go138
1 files changed, 138 insertions, 0 deletions
diff --git a/sheetview.go b/sheetview.go
new file mode 100644
index 0000000..d26b8cb
--- /dev/null
+++ b/sheetview.go
@@ -0,0 +1,138 @@
+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 {
+ 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
+ /* 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
+
+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
+}
+
+// getSheetView returns the SheetView object
+func (f *File) getSheetView(sheetName string, viewIndex int) (*xlsxSheetView, error) {
+ xlsx := f.workSheetReader(sheetName)
+ 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)
+ }
+
+ return &(xlsx.SheetViews.SheetView[viewIndex]), nil
+}
+
+// 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
+}