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
|
2019-08-11 00:36:14 +08:00
|
|
|
// charts of XLSX. This library needs Go version 1.10 or later.
|
2018-09-14 00:58:48 +08:00
|
|
|
|
2017-11-14 22:09:50 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
// SheetPrOption is an option of a view of a worksheet. See SetSheetPrOptions().
|
|
|
|
type SheetPrOption interface {
|
|
|
|
setSheetPrOption(view *xlsxSheetPr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SheetPrOptionPtr is a writable SheetPrOption. See GetSheetPrOptions().
|
|
|
|
type SheetPrOptionPtr interface {
|
|
|
|
SheetPrOption
|
|
|
|
getSheetPrOption(view *xlsxSheetPr)
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
// CodeName is a SheetPrOption
|
|
|
|
CodeName string
|
|
|
|
// EnableFormatConditionsCalculation is a SheetPrOption
|
|
|
|
EnableFormatConditionsCalculation bool
|
|
|
|
// Published is a SheetPrOption
|
|
|
|
Published bool
|
|
|
|
// FitToPage is a SheetPrOption
|
|
|
|
FitToPage bool
|
|
|
|
// AutoPageBreaks is a SheetPrOption
|
|
|
|
AutoPageBreaks bool
|
2018-12-14 03:01:36 +08:00
|
|
|
// OutlineSummaryBelow is an outlinePr, within SheetPr option
|
|
|
|
OutlineSummaryBelow bool
|
2017-11-14 22:09:50 +08:00
|
|
|
)
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// setSheetPrOption implements the SheetPrOption interface.
|
2018-12-14 03:01:36 +08:00
|
|
|
func (o OutlineSummaryBelow) setSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
if pr.OutlinePr == nil {
|
|
|
|
pr.OutlinePr = new(xlsxOutlinePr)
|
|
|
|
}
|
|
|
|
pr.OutlinePr.SummaryBelow = bool(o)
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// getSheetPrOption implements the SheetPrOptionPtr interface.
|
2018-12-14 03:01:36 +08:00
|
|
|
func (o *OutlineSummaryBelow) getSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
// Excel default: true
|
|
|
|
if pr == nil || pr.OutlinePr == nil {
|
|
|
|
*o = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*o = OutlineSummaryBelow(defaultTrue(&pr.OutlinePr.SummaryBelow))
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// setSheetPrOption implements the SheetPrOption interface and specifies a
|
|
|
|
// stable name of the sheet.
|
2017-11-14 22:09:50 +08:00
|
|
|
func (o CodeName) setSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
pr.CodeName = string(o)
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// getSheetPrOption implements the SheetPrOptionPtr interface and get the
|
|
|
|
// stable name of the sheet.
|
2017-11-14 22:09:50 +08:00
|
|
|
func (o *CodeName) getSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
if pr == nil {
|
|
|
|
*o = ""
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*o = CodeName(pr.CodeName)
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// setSheetPrOption implements the SheetPrOption interface and flag indicating
|
|
|
|
// whether the conditional formatting calculations shall be evaluated.
|
2017-11-14 22:09:50 +08:00
|
|
|
func (o EnableFormatConditionsCalculation) setSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
pr.EnableFormatConditionsCalculation = boolPtr(bool(o))
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// getSheetPrOption implements the SheetPrOptionPtr interface and get the
|
|
|
|
// settings of whether the conditional formatting calculations shall be
|
|
|
|
// evaluated.
|
2017-11-14 22:09:50 +08:00
|
|
|
func (o *EnableFormatConditionsCalculation) getSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
if pr == nil {
|
|
|
|
*o = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*o = EnableFormatConditionsCalculation(defaultTrue(pr.EnableFormatConditionsCalculation))
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// setSheetPrOption implements the SheetPrOption interface and flag indicating
|
|
|
|
// whether the worksheet is published.
|
2017-11-14 22:09:50 +08:00
|
|
|
func (o Published) setSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
pr.Published = boolPtr(bool(o))
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// getSheetPrOption implements the SheetPrOptionPtr interface and get the
|
|
|
|
// settings of whether the worksheet is published.
|
2017-11-14 22:09:50 +08:00
|
|
|
func (o *Published) getSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
if pr == nil {
|
|
|
|
*o = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*o = Published(defaultTrue(pr.Published))
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// setSheetPrOption implements the SheetPrOption interface.
|
2017-11-14 22:09:50 +08:00
|
|
|
func (o FitToPage) setSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
if pr.PageSetUpPr == nil {
|
|
|
|
if !o {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pr.PageSetUpPr = new(xlsxPageSetUpPr)
|
|
|
|
}
|
|
|
|
pr.PageSetUpPr.FitToPage = bool(o)
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// getSheetPrOption implements the SheetPrOptionPtr interface.
|
2017-11-14 22:09:50 +08:00
|
|
|
func (o *FitToPage) getSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
// Excel default: false
|
|
|
|
if pr == nil || pr.PageSetUpPr == nil {
|
|
|
|
*o = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*o = FitToPage(pr.PageSetUpPr.FitToPage)
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// setSheetPrOption implements the SheetPrOption interface.
|
2017-11-14 22:09:50 +08:00
|
|
|
func (o AutoPageBreaks) setSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
if pr.PageSetUpPr == nil {
|
|
|
|
if !o {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pr.PageSetUpPr = new(xlsxPageSetUpPr)
|
|
|
|
}
|
|
|
|
pr.PageSetUpPr.AutoPageBreaks = bool(o)
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
// getSheetPrOption implements the SheetPrOptionPtr interface.
|
2017-11-14 22:09:50 +08:00
|
|
|
func (o *AutoPageBreaks) getSheetPrOption(pr *xlsxSheetPr) {
|
|
|
|
// Excel default: false
|
|
|
|
if pr == nil || pr.PageSetUpPr == nil {
|
|
|
|
*o = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*o = AutoPageBreaks(pr.PageSetUpPr.AutoPageBreaks)
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// SetSheetPrOptions provides a function to sets worksheet properties.
|
2017-11-14 22:09:50 +08:00
|
|
|
//
|
|
|
|
// Available options:
|
|
|
|
// CodeName(string)
|
|
|
|
// EnableFormatConditionsCalculation(bool)
|
|
|
|
// Published(bool)
|
|
|
|
// FitToPage(bool)
|
|
|
|
// AutoPageBreaks(bool)
|
2018-12-14 03:01:36 +08:00
|
|
|
// OutlineSummaryBelow(bool)
|
2017-11-14 22:09:50 +08:00
|
|
|
func (f *File) SetSheetPrOptions(name string, opts ...SheetPrOption) error {
|
2019-04-15 11:22:57 +08:00
|
|
|
sheet, err := f.workSheetReader(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-14 22:09:50 +08:00
|
|
|
pr := sheet.SheetPr
|
|
|
|
if pr == nil {
|
|
|
|
pr = new(xlsxSheetPr)
|
|
|
|
sheet.SheetPr = pr
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.setSheetPrOption(pr)
|
|
|
|
}
|
2019-04-15 11:22:57 +08:00
|
|
|
return err
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// GetSheetPrOptions provides a function to gets worksheet properties.
|
2017-11-14 22:09:50 +08:00
|
|
|
//
|
|
|
|
// Available options:
|
|
|
|
// CodeName(string)
|
|
|
|
// EnableFormatConditionsCalculation(bool)
|
|
|
|
// Published(bool)
|
|
|
|
// FitToPage(bool)
|
|
|
|
// AutoPageBreaks(bool)
|
2018-12-14 03:01:36 +08:00
|
|
|
// OutlineSummaryBelow(bool)
|
2017-11-14 22:09:50 +08:00
|
|
|
func (f *File) GetSheetPrOptions(name string, opts ...SheetPrOptionPtr) error {
|
2019-04-15 11:22:57 +08:00
|
|
|
sheet, err := f.workSheetReader(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-14 22:09:50 +08:00
|
|
|
pr := sheet.SheetPr
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.getSheetPrOption(pr)
|
|
|
|
}
|
2019-04-15 11:22:57 +08:00
|
|
|
return err
|
2017-11-14 22:09:50 +08:00
|
|
|
}
|