2024-01-09 20:56:20 +08:00
|
|
|
// Copyright 2016 - 2024 The excelize Authors. All rights reserved. Use of
|
2019-09-20 00:20:30 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
2022-02-17 00:09:11 +08:00
|
|
|
// Package excelize providing a set of functions that allow you to write to and
|
|
|
|
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
|
|
|
|
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
|
|
|
|
// Supports complex components by high compatibility, and provided streaming
|
|
|
|
// API for generating or reading data from a worksheet with huge amounts of
|
2024-01-18 15:31:43 +08:00
|
|
|
// data. This library needs Go version 1.18 or later.
|
2019-09-20 00:20:30 +08:00
|
|
|
|
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
2023-09-08 00:09:41 +08:00
|
|
|
"bytes"
|
2019-09-20 00:20:30 +08:00
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
2023-09-08 00:09:41 +08:00
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2019-09-20 00:20:30 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-09-08 00:09:41 +08:00
|
|
|
|
|
|
|
"golang.org/x/text/cases"
|
|
|
|
"golang.org/x/text/language"
|
2019-09-20 00:20:30 +08:00
|
|
|
)
|
|
|
|
|
This closes #1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes:
Motivation and Context
When I decided to add set horizontal centered support for this library to resolve #1358, the reason I made this huge breaking change was:
- There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure.
- Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories
- Nested properties cannot proceed modify easily
Introduce 5 new export data types:
`HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions`
Rename 4 exported data types:
- Rename `PivotTableOption` to `PivotTableOptions`
- Rename `FormatHeaderFooter` to `HeaderFooterOptions`
- Rename `FormatSheetProtection` to `SheetProtectionOptions`
- Rename `SparklineOption` to `SparklineOptions`
Remove 54 exported types:
`AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale`
Remove 2 exported constants:
`OrientationPortrait` and `OrientationLandscape`
Change 8 functions:
- Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error`
- Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)`
- Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error`
- Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)`
- Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error`
- Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)`
- Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error`
- Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)`
Introduce new function to instead of existing functions:
- New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
2022-09-29 22:00:21 +08:00
|
|
|
// PivotTableOptions directly maps the format settings of the pivot table.
|
2021-10-11 00:08:45 +08:00
|
|
|
//
|
|
|
|
// PivotTableStyleName: The built-in pivot table style names
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// PivotStyleLight1 - PivotStyleLight28
|
|
|
|
// PivotStyleMedium1 - PivotStyleMedium28
|
|
|
|
// PivotStyleDark1 - PivotStyleDark28
|
This closes #1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes:
Motivation and Context
When I decided to add set horizontal centered support for this library to resolve #1358, the reason I made this huge breaking change was:
- There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure.
- Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories
- Nested properties cannot proceed modify easily
Introduce 5 new export data types:
`HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions`
Rename 4 exported data types:
- Rename `PivotTableOption` to `PivotTableOptions`
- Rename `FormatHeaderFooter` to `HeaderFooterOptions`
- Rename `FormatSheetProtection` to `SheetProtectionOptions`
- Rename `SparklineOption` to `SparklineOptions`
Remove 54 exported types:
`AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale`
Remove 2 exported constants:
`OrientationPortrait` and `OrientationLandscape`
Change 8 functions:
- Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error`
- Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)`
- Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error`
- Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)`
- Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error`
- Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)`
- Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error`
- Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)`
Introduce new function to instead of existing functions:
- New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
2022-09-29 22:00:21 +08:00
|
|
|
type PivotTableOptions struct {
|
2023-09-27 00:05:59 +08:00
|
|
|
pivotTableXML string
|
|
|
|
pivotCacheXML string
|
2023-10-05 00:08:31 +08:00
|
|
|
pivotSheetName string
|
|
|
|
pivotDataRange string
|
|
|
|
namedDataRange bool
|
Breaking change: changed the function signature for 11 exported functions
* Change
`func (f *File) NewConditionalStyle(style string) (int, error)`
to
`func (f *File) NewConditionalStyle(style *Style) (int, error)`
* Change
`func (f *File) NewStyle(style interface{}) (int, error)`
to
`func (f *File) NewStyle(style *Style) (int, error)`
* Change
`func (f *File) AddChart(sheet, cell, opts string, combo ...string) error`
to
`func (f *File) AddChart(sheet, cell string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddChartSheet(sheet, opts string, combo ...string) error`
to
`func (f *File) AddChartSheet(sheet string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddShape(sheet, cell, opts string) error`
to
`func (f *File) AddShape(sheet, cell string, opts *Shape) error`
* Change
`func (f *File) AddPictureFromBytes(sheet, cell, opts, name, extension string, file []byte) error`
to
`func (f *File) AddPictureFromBytes(sheet, cell, name, extension string, file []byte, opts *PictureOptions) error`
* Change
`func (f *File) AddTable(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AddTable(sheet, reference string, opts *TableOptions) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AutoFilter(sheet, reference string, opts *AutoFilterOptions) error`
* Change
`func (f *File) SetPanes(sheet, panes string) error`
to
`func (f *File) SetPanes(sheet string, panes *Panes) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) SetConditionalFormat(sheet, reference, opts string) error`
to
`func (f *File) SetConditionalFormat(sheet, reference string, opts []ConditionalFormatOptions) error`
* Add exported types:
* AutoFilterListOptions
* AutoFilterOptions
* Chart
* ChartAxis
* ChartDimension
* ChartLegend
* ChartLine
* ChartMarker
* ChartPlotArea
* ChartSeries
* ChartTitle
* ConditionalFormatOptions
* PaneOptions
* Panes
* PictureOptions
* Shape
* ShapeColor
* ShapeLine
* ShapeParagraph
* TableOptions
* This added support for set sheet visible as very hidden
* Return error when missing required parameters for set defined name
* Update unit test and comments
2022-12-30 00:50:08 +08:00
|
|
|
DataRange string
|
|
|
|
PivotTableRange string
|
2023-09-08 00:09:41 +08:00
|
|
|
Name string
|
Breaking change: changed the function signature for 11 exported functions
* Change
`func (f *File) NewConditionalStyle(style string) (int, error)`
to
`func (f *File) NewConditionalStyle(style *Style) (int, error)`
* Change
`func (f *File) NewStyle(style interface{}) (int, error)`
to
`func (f *File) NewStyle(style *Style) (int, error)`
* Change
`func (f *File) AddChart(sheet, cell, opts string, combo ...string) error`
to
`func (f *File) AddChart(sheet, cell string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddChartSheet(sheet, opts string, combo ...string) error`
to
`func (f *File) AddChartSheet(sheet string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddShape(sheet, cell, opts string) error`
to
`func (f *File) AddShape(sheet, cell string, opts *Shape) error`
* Change
`func (f *File) AddPictureFromBytes(sheet, cell, opts, name, extension string, file []byte) error`
to
`func (f *File) AddPictureFromBytes(sheet, cell, name, extension string, file []byte, opts *PictureOptions) error`
* Change
`func (f *File) AddTable(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AddTable(sheet, reference string, opts *TableOptions) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AutoFilter(sheet, reference string, opts *AutoFilterOptions) error`
* Change
`func (f *File) SetPanes(sheet, panes string) error`
to
`func (f *File) SetPanes(sheet string, panes *Panes) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) SetConditionalFormat(sheet, reference, opts string) error`
to
`func (f *File) SetConditionalFormat(sheet, reference string, opts []ConditionalFormatOptions) error`
* Add exported types:
* AutoFilterListOptions
* AutoFilterOptions
* Chart
* ChartAxis
* ChartDimension
* ChartLegend
* ChartLine
* ChartMarker
* ChartPlotArea
* ChartSeries
* ChartTitle
* ConditionalFormatOptions
* PaneOptions
* Panes
* PictureOptions
* Shape
* ShapeColor
* ShapeLine
* ShapeParagraph
* TableOptions
* This added support for set sheet visible as very hidden
* Return error when missing required parameters for set defined name
* Update unit test and comments
2022-12-30 00:50:08 +08:00
|
|
|
Rows []PivotTableField
|
|
|
|
Columns []PivotTableField
|
|
|
|
Data []PivotTableField
|
|
|
|
Filter []PivotTableField
|
|
|
|
RowGrandTotals bool
|
|
|
|
ColGrandTotals bool
|
|
|
|
ShowDrill bool
|
|
|
|
UseAutoFormatting bool
|
|
|
|
PageOverThenDown bool
|
|
|
|
MergeItem bool
|
2024-09-21 15:39:36 +08:00
|
|
|
ClassicLayout bool
|
Breaking change: changed the function signature for 11 exported functions
* Change
`func (f *File) NewConditionalStyle(style string) (int, error)`
to
`func (f *File) NewConditionalStyle(style *Style) (int, error)`
* Change
`func (f *File) NewStyle(style interface{}) (int, error)`
to
`func (f *File) NewStyle(style *Style) (int, error)`
* Change
`func (f *File) AddChart(sheet, cell, opts string, combo ...string) error`
to
`func (f *File) AddChart(sheet, cell string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddChartSheet(sheet, opts string, combo ...string) error`
to
`func (f *File) AddChartSheet(sheet string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddShape(sheet, cell, opts string) error`
to
`func (f *File) AddShape(sheet, cell string, opts *Shape) error`
* Change
`func (f *File) AddPictureFromBytes(sheet, cell, opts, name, extension string, file []byte) error`
to
`func (f *File) AddPictureFromBytes(sheet, cell, name, extension string, file []byte, opts *PictureOptions) error`
* Change
`func (f *File) AddTable(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AddTable(sheet, reference string, opts *TableOptions) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AutoFilter(sheet, reference string, opts *AutoFilterOptions) error`
* Change
`func (f *File) SetPanes(sheet, panes string) error`
to
`func (f *File) SetPanes(sheet string, panes *Panes) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) SetConditionalFormat(sheet, reference, opts string) error`
to
`func (f *File) SetConditionalFormat(sheet, reference string, opts []ConditionalFormatOptions) error`
* Add exported types:
* AutoFilterListOptions
* AutoFilterOptions
* Chart
* ChartAxis
* ChartDimension
* ChartLegend
* ChartLine
* ChartMarker
* ChartPlotArea
* ChartSeries
* ChartTitle
* ConditionalFormatOptions
* PaneOptions
* Panes
* PictureOptions
* Shape
* ShapeColor
* ShapeLine
* ShapeParagraph
* TableOptions
* This added support for set sheet visible as very hidden
* Return error when missing required parameters for set defined name
* Update unit test and comments
2022-12-30 00:50:08 +08:00
|
|
|
CompactData bool
|
|
|
|
ShowError bool
|
|
|
|
ShowRowHeaders bool
|
|
|
|
ShowColHeaders bool
|
|
|
|
ShowRowStripes bool
|
|
|
|
ShowColStripes bool
|
|
|
|
ShowLastColumn bool
|
2024-09-01 12:10:01 +08:00
|
|
|
FieldPrintTitles bool
|
|
|
|
ItemPrintTitles bool
|
Breaking change: changed the function signature for 11 exported functions
* Change
`func (f *File) NewConditionalStyle(style string) (int, error)`
to
`func (f *File) NewConditionalStyle(style *Style) (int, error)`
* Change
`func (f *File) NewStyle(style interface{}) (int, error)`
to
`func (f *File) NewStyle(style *Style) (int, error)`
* Change
`func (f *File) AddChart(sheet, cell, opts string, combo ...string) error`
to
`func (f *File) AddChart(sheet, cell string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddChartSheet(sheet, opts string, combo ...string) error`
to
`func (f *File) AddChartSheet(sheet string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddShape(sheet, cell, opts string) error`
to
`func (f *File) AddShape(sheet, cell string, opts *Shape) error`
* Change
`func (f *File) AddPictureFromBytes(sheet, cell, opts, name, extension string, file []byte) error`
to
`func (f *File) AddPictureFromBytes(sheet, cell, name, extension string, file []byte, opts *PictureOptions) error`
* Change
`func (f *File) AddTable(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AddTable(sheet, reference string, opts *TableOptions) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AutoFilter(sheet, reference string, opts *AutoFilterOptions) error`
* Change
`func (f *File) SetPanes(sheet, panes string) error`
to
`func (f *File) SetPanes(sheet string, panes *Panes) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) SetConditionalFormat(sheet, reference, opts string) error`
to
`func (f *File) SetConditionalFormat(sheet, reference string, opts []ConditionalFormatOptions) error`
* Add exported types:
* AutoFilterListOptions
* AutoFilterOptions
* Chart
* ChartAxis
* ChartDimension
* ChartLegend
* ChartLine
* ChartMarker
* ChartPlotArea
* ChartSeries
* ChartTitle
* ConditionalFormatOptions
* PaneOptions
* Panes
* PictureOptions
* Shape
* ShapeColor
* ShapeLine
* ShapeParagraph
* TableOptions
* This added support for set sheet visible as very hidden
* Return error when missing required parameters for set defined name
* Update unit test and comments
2022-12-30 00:50:08 +08:00
|
|
|
PivotTableStyleName string
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
|
2020-02-26 18:53:50 +08:00
|
|
|
// PivotTableField directly maps the field settings of the pivot table.
|
2024-08-18 00:18:02 +08:00
|
|
|
//
|
|
|
|
// Name specifies the name of the data field. Maximum 255 characters
|
|
|
|
// are allowed in data field name, excess characters will be truncated.
|
|
|
|
//
|
2020-02-26 18:53:50 +08:00
|
|
|
// Subtotal specifies the aggregation function that applies to this data
|
2020-02-21 23:07:43 +08:00
|
|
|
// field. The default value is sum. The possible values for this attribute
|
|
|
|
// are:
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// Average
|
|
|
|
// Count
|
|
|
|
// CountNums
|
|
|
|
// Max
|
|
|
|
// Min
|
|
|
|
// Product
|
|
|
|
// StdDev
|
|
|
|
// StdDevp
|
|
|
|
// Sum
|
|
|
|
// Var
|
|
|
|
// Varp
|
2020-02-21 23:07:43 +08:00
|
|
|
//
|
2024-08-18 00:18:02 +08:00
|
|
|
// NumFmt specifies the number format ID of the data field, this filed only
|
|
|
|
// accepts built-in number format ID and does not support custom number format
|
|
|
|
// expression currently.
|
2020-02-26 18:53:50 +08:00
|
|
|
type PivotTableField struct {
|
Breaking change: changed the function signature for 11 exported functions
* Change
`func (f *File) NewConditionalStyle(style string) (int, error)`
to
`func (f *File) NewConditionalStyle(style *Style) (int, error)`
* Change
`func (f *File) NewStyle(style interface{}) (int, error)`
to
`func (f *File) NewStyle(style *Style) (int, error)`
* Change
`func (f *File) AddChart(sheet, cell, opts string, combo ...string) error`
to
`func (f *File) AddChart(sheet, cell string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddChartSheet(sheet, opts string, combo ...string) error`
to
`func (f *File) AddChartSheet(sheet string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddShape(sheet, cell, opts string) error`
to
`func (f *File) AddShape(sheet, cell string, opts *Shape) error`
* Change
`func (f *File) AddPictureFromBytes(sheet, cell, opts, name, extension string, file []byte) error`
to
`func (f *File) AddPictureFromBytes(sheet, cell, name, extension string, file []byte, opts *PictureOptions) error`
* Change
`func (f *File) AddTable(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AddTable(sheet, reference string, opts *TableOptions) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AutoFilter(sheet, reference string, opts *AutoFilterOptions) error`
* Change
`func (f *File) SetPanes(sheet, panes string) error`
to
`func (f *File) SetPanes(sheet string, panes *Panes) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) SetConditionalFormat(sheet, reference, opts string) error`
to
`func (f *File) SetConditionalFormat(sheet, reference string, opts []ConditionalFormatOptions) error`
* Add exported types:
* AutoFilterListOptions
* AutoFilterOptions
* Chart
* ChartAxis
* ChartDimension
* ChartLegend
* ChartLine
* ChartMarker
* ChartPlotArea
* ChartSeries
* ChartTitle
* ConditionalFormatOptions
* PaneOptions
* Panes
* PictureOptions
* Shape
* ShapeColor
* ShapeLine
* ShapeParagraph
* TableOptions
* This added support for set sheet visible as very hidden
* Return error when missing required parameters for set defined name
* Update unit test and comments
2022-12-30 00:50:08 +08:00
|
|
|
Compact bool
|
|
|
|
Data string
|
|
|
|
Name string
|
|
|
|
Outline bool
|
2024-09-01 12:10:01 +08:00
|
|
|
ShowAll bool
|
|
|
|
InsertBlankRow bool
|
Breaking change: changed the function signature for 11 exported functions
* Change
`func (f *File) NewConditionalStyle(style string) (int, error)`
to
`func (f *File) NewConditionalStyle(style *Style) (int, error)`
* Change
`func (f *File) NewStyle(style interface{}) (int, error)`
to
`func (f *File) NewStyle(style *Style) (int, error)`
* Change
`func (f *File) AddChart(sheet, cell, opts string, combo ...string) error`
to
`func (f *File) AddChart(sheet, cell string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddChartSheet(sheet, opts string, combo ...string) error`
to
`func (f *File) AddChartSheet(sheet string, chart *ChartOptions, combo ...*ChartOptions) error`
* Change
`func (f *File) AddShape(sheet, cell, opts string) error`
to
`func (f *File) AddShape(sheet, cell string, opts *Shape) error`
* Change
`func (f *File) AddPictureFromBytes(sheet, cell, opts, name, extension string, file []byte) error`
to
`func (f *File) AddPictureFromBytes(sheet, cell, name, extension string, file []byte, opts *PictureOptions) error`
* Change
`func (f *File) AddTable(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AddTable(sheet, reference string, opts *TableOptions) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error`
to
`func (f *File) AutoFilter(sheet, reference string, opts *AutoFilterOptions) error`
* Change
`func (f *File) SetPanes(sheet, panes string) error`
to
`func (f *File) SetPanes(sheet string, panes *Panes) error`
* Change
`func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error`
to
`func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error`
* Change
`func (f *File) SetConditionalFormat(sheet, reference, opts string) error`
to
`func (f *File) SetConditionalFormat(sheet, reference string, opts []ConditionalFormatOptions) error`
* Add exported types:
* AutoFilterListOptions
* AutoFilterOptions
* Chart
* ChartAxis
* ChartDimension
* ChartLegend
* ChartLine
* ChartMarker
* ChartPlotArea
* ChartSeries
* ChartTitle
* ConditionalFormatOptions
* PaneOptions
* Panes
* PictureOptions
* Shape
* ShapeColor
* ShapeLine
* ShapeParagraph
* TableOptions
* This added support for set sheet visible as very hidden
* Return error when missing required parameters for set defined name
* Update unit test and comments
2022-12-30 00:50:08 +08:00
|
|
|
Subtotal string
|
|
|
|
DefaultSubtotal bool
|
2024-08-18 00:18:02 +08:00
|
|
|
NumFmt int
|
2020-02-26 18:53:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddPivotTable provides the method to add pivot table by given pivot table
|
2021-03-15 23:56:36 +08:00
|
|
|
// options. Note that the same fields can not in Columns, Rows and Filter
|
|
|
|
// fields at the same time.
|
2020-02-21 23:07:43 +08:00
|
|
|
//
|
2023-09-16 12:21:11 +08:00
|
|
|
// For example, create a pivot table on the range reference Sheet1!G2:M34 with
|
|
|
|
// the range reference Sheet1!A1:E31 as the data source, summarize by sum for
|
|
|
|
// sales:
|
2019-09-20 00:20:30 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// package main
|
2019-09-20 00:20:30 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// import (
|
|
|
|
// "fmt"
|
|
|
|
// "math/rand"
|
2019-09-20 00:20:30 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// "github.com/xuri/excelize/v2"
|
|
|
|
// )
|
2019-09-20 00:20:30 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// func main() {
|
|
|
|
// f := excelize.NewFile()
|
2023-01-02 11:47:31 +08:00
|
|
|
// defer func() {
|
|
|
|
// if err := f.Close(); err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// }
|
|
|
|
// }()
|
2022-08-13 11:21:59 +08:00
|
|
|
// // Create some data in a sheet
|
|
|
|
// month := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
|
|
|
|
// year := []int{2017, 2018, 2019}
|
|
|
|
// types := []string{"Meat", "Dairy", "Beverages", "Produce"}
|
|
|
|
// region := []string{"East", "West", "North", "South"}
|
|
|
|
// f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "Year", "Type", "Sales", "Region"})
|
|
|
|
// for row := 2; row < 32; row++ {
|
|
|
|
// f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), month[rand.Intn(12)])
|
|
|
|
// f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), year[rand.Intn(3)])
|
|
|
|
// f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), types[rand.Intn(4)])
|
|
|
|
// f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), rand.Intn(5000))
|
|
|
|
// f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), region[rand.Intn(4)])
|
|
|
|
// }
|
2022-10-08 22:08:06 +08:00
|
|
|
// if err := f.AddPivotTable(&excelize.PivotTableOptions{
|
2023-09-08 00:09:41 +08:00
|
|
|
// DataRange: "Sheet1!A1:E31",
|
|
|
|
// PivotTableRange: "Sheet1!G2:M34",
|
2022-08-13 11:21:59 +08:00
|
|
|
// Rows: []excelize.PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},
|
|
|
|
// Filter: []excelize.PivotTableField{{Data: "Region"}},
|
|
|
|
// Columns: []excelize.PivotTableField{{Data: "Type", DefaultSubtotal: true}},
|
|
|
|
// Data: []excelize.PivotTableField{{Data: "Sales", Name: "Summarize", Subtotal: "Sum"}},
|
|
|
|
// RowGrandTotals: true,
|
|
|
|
// ColGrandTotals: true,
|
|
|
|
// ShowDrill: true,
|
|
|
|
// ShowRowHeaders: true,
|
|
|
|
// ShowColHeaders: true,
|
|
|
|
// ShowLastColumn: true,
|
|
|
|
// }); err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// }
|
|
|
|
// if err := f.SaveAs("Book1.xlsx"); err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// }
|
|
|
|
// }
|
This closes #1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes:
Motivation and Context
When I decided to add set horizontal centered support for this library to resolve #1358, the reason I made this huge breaking change was:
- There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure.
- Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories
- Nested properties cannot proceed modify easily
Introduce 5 new export data types:
`HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions`
Rename 4 exported data types:
- Rename `PivotTableOption` to `PivotTableOptions`
- Rename `FormatHeaderFooter` to `HeaderFooterOptions`
- Rename `FormatSheetProtection` to `SheetProtectionOptions`
- Rename `SparklineOption` to `SparklineOptions`
Remove 54 exported types:
`AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale`
Remove 2 exported constants:
`OrientationPortrait` and `OrientationLandscape`
Change 8 functions:
- Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error`
- Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)`
- Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error`
- Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)`
- Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error`
- Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)`
- Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error`
- Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)`
Introduce new function to instead of existing functions:
- New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
2022-09-29 22:00:21 +08:00
|
|
|
func (f *File) AddPivotTable(opts *PivotTableOptions) error {
|
2019-09-20 00:20:30 +08:00
|
|
|
// parameter validation
|
2022-09-18 00:07:15 +08:00
|
|
|
_, pivotTableSheetPath, err := f.parseFormatPivotTableSet(opts)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pivotTableID := f.countPivotTables() + 1
|
|
|
|
pivotCacheID := f.countPivotCache() + 1
|
|
|
|
|
|
|
|
sheetRelationshipsPivotTableXML := "../pivotTables/pivotTable" + strconv.Itoa(pivotTableID) + ".xml"
|
2023-10-05 00:08:31 +08:00
|
|
|
opts.pivotTableXML = strings.ReplaceAll(sheetRelationshipsPivotTableXML, "..", "xl")
|
|
|
|
opts.pivotCacheXML = "xl/pivotCache/pivotCacheDefinition" + strconv.Itoa(pivotCacheID) + ".xml"
|
|
|
|
if err = f.addPivotCache(opts); err != nil {
|
2019-09-20 00:20:30 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// workbook pivot cache
|
2023-10-05 00:08:31 +08:00
|
|
|
workBookPivotCacheRID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipPivotCache, strings.TrimPrefix(opts.pivotCacheXML, "xl/"), "")
|
2019-09-20 00:20:30 +08:00
|
|
|
cacheID := f.addWorkbookPivotCache(workBookPivotCacheRID)
|
|
|
|
|
|
|
|
pivotCacheRels := "xl/pivotTables/_rels/pivotTable" + strconv.Itoa(pivotTableID) + ".xml.rels"
|
|
|
|
// rId not used
|
|
|
|
_ = f.addRels(pivotCacheRels, SourceRelationshipPivotCache, fmt.Sprintf("../pivotCache/pivotCacheDefinition%d.xml", pivotCacheID), "")
|
2023-10-05 00:08:31 +08:00
|
|
|
if err = f.addPivotTable(cacheID, pivotTableID, opts); err != nil {
|
2019-09-20 00:20:30 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
pivotTableSheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(pivotTableSheetPath, "xl/worksheets/") + ".rels"
|
|
|
|
f.addRels(pivotTableSheetRels, SourceRelationshipPivotTable, sheetRelationshipsPivotTableXML, "")
|
2022-11-13 00:40:04 +08:00
|
|
|
if err = f.addContentTypePart(pivotTableID, "pivotTable"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return f.addContentTypePart(pivotCacheID, "pivotCache")
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseFormatPivotTableSet provides a function to validate pivot table
|
|
|
|
// properties.
|
This closes #1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes:
Motivation and Context
When I decided to add set horizontal centered support for this library to resolve #1358, the reason I made this huge breaking change was:
- There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure.
- Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories
- Nested properties cannot proceed modify easily
Introduce 5 new export data types:
`HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions`
Rename 4 exported data types:
- Rename `PivotTableOption` to `PivotTableOptions`
- Rename `FormatHeaderFooter` to `HeaderFooterOptions`
- Rename `FormatSheetProtection` to `SheetProtectionOptions`
- Rename `SparklineOption` to `SparklineOptions`
Remove 54 exported types:
`AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale`
Remove 2 exported constants:
`OrientationPortrait` and `OrientationLandscape`
Change 8 functions:
- Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error`
- Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)`
- Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error`
- Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)`
- Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error`
- Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)`
- Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error`
- Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)`
Introduce new function to instead of existing functions:
- New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
2022-09-29 22:00:21 +08:00
|
|
|
func (f *File) parseFormatPivotTableSet(opts *PivotTableOptions) (*xlsxWorksheet, string, error) {
|
2022-09-18 00:07:15 +08:00
|
|
|
if opts == nil {
|
2021-07-05 00:03:56 +08:00
|
|
|
return nil, "", ErrParameterRequired
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
2022-09-18 00:07:15 +08:00
|
|
|
pivotTableSheetName, _, err := f.adjustRange(opts.PivotTableRange)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
2023-09-28 08:53:54 +08:00
|
|
|
return nil, "", newPivotTableRangeError(err.Error())
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
2023-09-08 00:09:41 +08:00
|
|
|
if len(opts.Name) > MaxFieldLength {
|
|
|
|
return nil, "", ErrNameLength
|
|
|
|
}
|
2023-10-05 00:08:31 +08:00
|
|
|
opts.pivotSheetName = pivotTableSheetName
|
|
|
|
if err = f.getPivotTableDataRange(opts); err != nil {
|
2023-10-03 00:59:31 +08:00
|
|
|
return nil, "", err
|
2021-06-29 22:26:55 +08:00
|
|
|
}
|
2023-10-05 00:08:31 +08:00
|
|
|
dataSheetName, _, err := f.adjustRange(opts.pivotDataRange)
|
2021-06-29 22:26:55 +08:00
|
|
|
if err != nil {
|
2023-09-28 08:53:54 +08:00
|
|
|
return nil, "", newPivotTableDataRangeError(err.Error())
|
2021-06-29 22:26:55 +08:00
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
dataSheet, err := f.workSheetReader(dataSheetName)
|
|
|
|
if err != nil {
|
|
|
|
return dataSheet, "", err
|
|
|
|
}
|
2022-07-18 00:21:34 +08:00
|
|
|
pivotTableSheetPath, ok := f.getSheetXMLPath(pivotTableSheetName)
|
2019-09-20 00:20:30 +08:00
|
|
|
if !ok {
|
2023-09-28 08:53:54 +08:00
|
|
|
return dataSheet, pivotTableSheetPath, ErrSheetNotExist{pivotTableSheetName}
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
2024-09-21 15:39:36 +08:00
|
|
|
if opts.CompactData && opts.ClassicLayout {
|
|
|
|
return nil, "", ErrPivotTableClassicLayout
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
return dataSheet, pivotTableSheetPath, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjustRange adjust range, for example: adjust Sheet1!$E$31:$A$1 to Sheet1!$A$1:$E$31
|
|
|
|
func (f *File) adjustRange(rangeStr string) (string, []int, error) {
|
|
|
|
if len(rangeStr) < 1 {
|
2021-07-05 00:03:56 +08:00
|
|
|
return "", []int{}, ErrParameterRequired
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
rng := strings.Split(rangeStr, "!")
|
|
|
|
if len(rng) != 2 {
|
2021-07-05 00:03:56 +08:00
|
|
|
return "", []int{}, ErrParameterInvalid
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
2022-06-12 00:19:12 +08:00
|
|
|
trimRng := strings.ReplaceAll(rng[1], "$", "")
|
2022-09-28 00:04:17 +08:00
|
|
|
coordinates, err := rangeRefToCoordinates(trimRng)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return rng[0], []int{}, err
|
|
|
|
}
|
|
|
|
x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
|
|
|
|
if x1 == x2 && y1 == y2 {
|
2021-07-05 00:03:56 +08:00
|
|
|
return rng[0], []int{}, ErrParameterInvalid
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
|
2022-09-18 00:07:15 +08:00
|
|
|
// Correct the range, such correct C1:B3 to B1:C3.
|
2019-09-20 00:20:30 +08:00
|
|
|
if x2 < x1 {
|
|
|
|
x1, x2 = x2, x1
|
|
|
|
}
|
|
|
|
|
|
|
|
if y2 < y1 {
|
|
|
|
y1, y2 = y2, y1
|
|
|
|
}
|
|
|
|
return rng[0], []int{x1, y1, x2, y2}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-16 12:21:11 +08:00
|
|
|
// getTableFieldsOrder provides a function to get order list of pivot table
|
2020-02-26 18:53:50 +08:00
|
|
|
// fields.
|
2023-10-05 00:08:31 +08:00
|
|
|
func (f *File) getTableFieldsOrder(opts *PivotTableOptions) ([]string, error) {
|
2022-03-24 00:19:30 +08:00
|
|
|
var order []string
|
2023-10-05 00:08:31 +08:00
|
|
|
if err := f.getPivotTableDataRange(opts); err != nil {
|
2023-10-03 00:59:31 +08:00
|
|
|
return order, err
|
|
|
|
}
|
2023-10-05 00:08:31 +08:00
|
|
|
dataSheet, coordinates, err := f.adjustRange(opts.pivotDataRange)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
2023-09-28 08:53:54 +08:00
|
|
|
return order, newPivotTableDataRangeError(err.Error())
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
for col := coordinates[0]; col <= coordinates[2]; col++ {
|
|
|
|
coordinate, _ := CoordinatesToCellName(col, coordinates[1])
|
|
|
|
name, err := f.GetCellValue(dataSheet, coordinate)
|
|
|
|
if err != nil {
|
|
|
|
return order, err
|
|
|
|
}
|
2024-07-13 10:41:57 +08:00
|
|
|
if name == "" {
|
|
|
|
return order, ErrParameterInvalid
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
order = append(order, name)
|
|
|
|
}
|
|
|
|
return order, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addPivotCache provides a function to create a pivot cache by given properties.
|
2023-10-05 00:08:31 +08:00
|
|
|
func (f *File) addPivotCache(opts *PivotTableOptions) error {
|
2019-09-20 00:20:30 +08:00
|
|
|
// validate data range
|
2023-10-05 00:08:31 +08:00
|
|
|
dataSheet, coordinates, err := f.adjustRange(opts.pivotDataRange)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
2023-09-28 08:53:54 +08:00
|
|
|
return newPivotTableDataRangeError(err.Error())
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
2024-07-13 10:41:57 +08:00
|
|
|
order, err := f.getTableFieldsOrder(opts)
|
|
|
|
if err != nil {
|
|
|
|
return newPivotTableDataRangeError(err.Error())
|
|
|
|
}
|
2023-12-30 14:41:16 +08:00
|
|
|
topLeftCell, _ := CoordinatesToCellName(coordinates[0], coordinates[1])
|
|
|
|
bottomRightCell, _ := CoordinatesToCellName(coordinates[2], coordinates[3])
|
2019-09-20 00:20:30 +08:00
|
|
|
pc := xlsxPivotCacheDefinition{
|
2021-03-15 23:56:36 +08:00
|
|
|
SaveData: false,
|
|
|
|
RefreshOnLoad: true,
|
|
|
|
CreatedVersion: pivotTableVersion,
|
2023-09-27 00:05:59 +08:00
|
|
|
RefreshedVersion: pivotTableRefreshedVersion,
|
2021-03-15 23:56:36 +08:00
|
|
|
MinRefreshableVersion: pivotTableVersion,
|
2019-09-20 00:20:30 +08:00
|
|
|
CacheSource: &xlsxCacheSource{
|
|
|
|
Type: "worksheet",
|
|
|
|
WorksheetSource: &xlsxWorksheetSource{
|
2023-12-30 14:41:16 +08:00
|
|
|
Ref: topLeftCell + ":" + bottomRightCell,
|
2019-09-20 00:20:30 +08:00
|
|
|
Sheet: dataSheet,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CacheFields: &xlsxCacheFields{},
|
|
|
|
}
|
2023-10-05 00:08:31 +08:00
|
|
|
if opts.namedDataRange {
|
2022-09-18 00:07:15 +08:00
|
|
|
pc.CacheSource.WorksheetSource = &xlsxWorksheetSource{Name: opts.DataRange}
|
2021-06-29 22:26:55 +08:00
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
for _, name := range order {
|
|
|
|
pc.CacheFields.CacheField = append(pc.CacheFields.CacheField, &xlsxCacheField{
|
2020-09-11 00:45:52 +08:00
|
|
|
Name: name,
|
2023-09-27 00:05:59 +08:00
|
|
|
SharedItems: &xlsxSharedItems{ContainsBlank: true, M: []xlsxMissing{{}}},
|
2019-09-20 00:20:30 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
pc.CacheFields.Count = len(pc.CacheFields.CacheField)
|
|
|
|
pivotCache, err := xml.Marshal(pc)
|
2023-10-05 00:08:31 +08:00
|
|
|
f.saveFileList(opts.pivotCacheXML, pivotCache)
|
2019-09-20 00:20:30 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// addPivotTable provides a function to create a pivot table by given pivot
|
|
|
|
// table ID and properties.
|
2023-10-05 00:08:31 +08:00
|
|
|
func (f *File) addPivotTable(cacheID, pivotTableID int, opts *PivotTableOptions) error {
|
2019-09-20 00:20:30 +08:00
|
|
|
// validate pivot table range
|
2022-09-18 00:07:15 +08:00
|
|
|
_, coordinates, err := f.adjustRange(opts.PivotTableRange)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
2023-09-28 08:53:54 +08:00
|
|
|
return newPivotTableRangeError(err.Error())
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
|
2023-12-30 14:41:16 +08:00
|
|
|
topLeftCell, _ := CoordinatesToCellName(coordinates[0], coordinates[1])
|
|
|
|
bottomRightCell, _ := CoordinatesToCellName(coordinates[2], coordinates[3])
|
2019-09-20 00:20:30 +08:00
|
|
|
|
2020-09-11 00:45:52 +08:00
|
|
|
pivotTableStyle := func() string {
|
2022-09-18 00:07:15 +08:00
|
|
|
if opts.PivotTableStyleName == "" {
|
2020-09-11 00:45:52 +08:00
|
|
|
return "PivotStyleLight16"
|
|
|
|
}
|
2022-09-18 00:07:15 +08:00
|
|
|
return opts.PivotTableStyleName
|
2020-09-11 00:45:52 +08:00
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
pt := xlsxPivotTableDefinition{
|
2023-09-08 00:09:41 +08:00
|
|
|
Name: opts.Name,
|
2021-03-15 23:56:36 +08:00
|
|
|
CacheID: cacheID,
|
2022-09-18 00:07:15 +08:00
|
|
|
RowGrandTotals: &opts.RowGrandTotals,
|
|
|
|
ColGrandTotals: &opts.ColGrandTotals,
|
2023-09-27 00:05:59 +08:00
|
|
|
UpdatedVersion: pivotTableRefreshedVersion,
|
2021-03-15 23:56:36 +08:00
|
|
|
MinRefreshableVersion: pivotTableVersion,
|
2022-09-18 00:07:15 +08:00
|
|
|
ShowDrill: &opts.ShowDrill,
|
|
|
|
UseAutoFormatting: &opts.UseAutoFormatting,
|
|
|
|
PageOverThenDown: &opts.PageOverThenDown,
|
|
|
|
MergeItem: &opts.MergeItem,
|
2023-10-01 13:37:47 +08:00
|
|
|
CreatedVersion: pivotTableVersion,
|
2022-09-18 00:07:15 +08:00
|
|
|
CompactData: &opts.CompactData,
|
2024-09-21 15:39:36 +08:00
|
|
|
GridDropZones: opts.ClassicLayout,
|
2022-09-18 00:07:15 +08:00
|
|
|
ShowError: &opts.ShowError,
|
2024-09-01 12:10:01 +08:00
|
|
|
FieldPrintTitles: opts.FieldPrintTitles,
|
|
|
|
ItemPrintTitles: opts.ItemPrintTitles,
|
2021-03-15 23:56:36 +08:00
|
|
|
DataCaption: "Values",
|
2019-09-20 00:20:30 +08:00
|
|
|
Location: &xlsxLocation{
|
2023-12-30 14:41:16 +08:00
|
|
|
Ref: topLeftCell + ":" + bottomRightCell,
|
2019-09-20 00:20:30 +08:00
|
|
|
FirstDataCol: 1,
|
|
|
|
FirstDataRow: 1,
|
|
|
|
FirstHeaderRow: 1,
|
|
|
|
},
|
|
|
|
PivotFields: &xlsxPivotFields{},
|
|
|
|
RowItems: &xlsxRowItems{
|
|
|
|
Count: 1,
|
|
|
|
I: []*xlsxI{
|
|
|
|
{
|
|
|
|
[]*xlsxX{{}, {}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-10-24 23:18:02 +08:00
|
|
|
ColItems: &xlsxColItems{
|
|
|
|
Count: 1,
|
|
|
|
I: []*xlsxI{{}},
|
|
|
|
},
|
2019-09-20 00:20:30 +08:00
|
|
|
PivotTableStyleInfo: &xlsxPivotTableStyleInfo{
|
2020-09-11 00:45:52 +08:00
|
|
|
Name: pivotTableStyle(),
|
2022-09-18 00:07:15 +08:00
|
|
|
ShowRowHeaders: opts.ShowRowHeaders,
|
|
|
|
ShowColHeaders: opts.ShowColHeaders,
|
|
|
|
ShowRowStripes: opts.ShowRowStripes,
|
|
|
|
ShowColStripes: opts.ShowColStripes,
|
|
|
|
ShowLastColumn: opts.ShowLastColumn,
|
2019-09-20 00:20:30 +08:00
|
|
|
},
|
|
|
|
}
|
2023-09-08 00:09:41 +08:00
|
|
|
if pt.Name == "" {
|
|
|
|
pt.Name = fmt.Sprintf("PivotTable%d", pivotTableID)
|
|
|
|
}
|
2024-09-21 15:39:36 +08:00
|
|
|
|
|
|
|
// set classic layout
|
|
|
|
if opts.ClassicLayout {
|
|
|
|
pt.Compact, pt.CompactData = boolPtr(false), boolPtr(false)
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:20:30 +08:00
|
|
|
// pivot fields
|
2022-09-18 00:07:15 +08:00
|
|
|
_ = f.addPivotFields(&pt, opts)
|
2019-09-20 00:20:30 +08:00
|
|
|
|
|
|
|
// count pivot fields
|
|
|
|
pt.PivotFields.Count = len(pt.PivotFields.PivotField)
|
|
|
|
|
2020-04-10 00:04:23 +08:00
|
|
|
// data range has been checked
|
2022-09-18 00:07:15 +08:00
|
|
|
_ = f.addPivotRowFields(&pt, opts)
|
|
|
|
_ = f.addPivotColFields(&pt, opts)
|
|
|
|
_ = f.addPivotPageFields(&pt, opts)
|
|
|
|
_ = f.addPivotDataFields(&pt, opts)
|
2020-04-10 00:04:23 +08:00
|
|
|
|
|
|
|
pivotTable, err := xml.Marshal(pt)
|
2023-10-05 00:08:31 +08:00
|
|
|
f.saveFileList(opts.pivotTableXML, pivotTable)
|
2020-04-10 00:04:23 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// addPivotRowFields provides a method to add row fields for pivot table by
|
|
|
|
// given pivot table options.
|
This closes #1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes:
Motivation and Context
When I decided to add set horizontal centered support for this library to resolve #1358, the reason I made this huge breaking change was:
- There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure.
- Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories
- Nested properties cannot proceed modify easily
Introduce 5 new export data types:
`HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions`
Rename 4 exported data types:
- Rename `PivotTableOption` to `PivotTableOptions`
- Rename `FormatHeaderFooter` to `HeaderFooterOptions`
- Rename `FormatSheetProtection` to `SheetProtectionOptions`
- Rename `SparklineOption` to `SparklineOptions`
Remove 54 exported types:
`AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale`
Remove 2 exported constants:
`OrientationPortrait` and `OrientationLandscape`
Change 8 functions:
- Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error`
- Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)`
- Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error`
- Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)`
- Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error`
- Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)`
- Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error`
- Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)`
Introduce new function to instead of existing functions:
- New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
2022-09-29 22:00:21 +08:00
|
|
|
func (f *File) addPivotRowFields(pt *xlsxPivotTableDefinition, opts *PivotTableOptions) error {
|
2019-09-20 00:20:30 +08:00
|
|
|
// row fields
|
2022-09-18 00:07:15 +08:00
|
|
|
rowFieldsIndex, err := f.getPivotFieldsIndex(opts.Rows, opts)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-21 23:07:43 +08:00
|
|
|
for _, fieldIdx := range rowFieldsIndex {
|
2020-04-10 00:04:23 +08:00
|
|
|
if pt.RowFields == nil {
|
|
|
|
pt.RowFields = &xlsxRowFields{}
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
pt.RowFields.Field = append(pt.RowFields.Field, &xlsxField{
|
2020-02-21 23:07:43 +08:00
|
|
|
X: fieldIdx,
|
2019-09-20 00:20:30 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// count row fields
|
2020-04-10 00:04:23 +08:00
|
|
|
if pt.RowFields != nil {
|
|
|
|
pt.RowFields.Count = len(pt.RowFields.Field)
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
2020-04-10 00:04:23 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
|
2020-04-10 00:04:23 +08:00
|
|
|
// addPivotPageFields provides a method to add page fields for pivot table by
|
|
|
|
// given pivot table options.
|
This closes #1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes:
Motivation and Context
When I decided to add set horizontal centered support for this library to resolve #1358, the reason I made this huge breaking change was:
- There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure.
- Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories
- Nested properties cannot proceed modify easily
Introduce 5 new export data types:
`HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions`
Rename 4 exported data types:
- Rename `PivotTableOption` to `PivotTableOptions`
- Rename `FormatHeaderFooter` to `HeaderFooterOptions`
- Rename `FormatSheetProtection` to `SheetProtectionOptions`
- Rename `SparklineOption` to `SparklineOptions`
Remove 54 exported types:
`AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale`
Remove 2 exported constants:
`OrientationPortrait` and `OrientationLandscape`
Change 8 functions:
- Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error`
- Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)`
- Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error`
- Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)`
- Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error`
- Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)`
- Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error`
- Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)`
Introduce new function to instead of existing functions:
- New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
2022-09-29 22:00:21 +08:00
|
|
|
func (f *File) addPivotPageFields(pt *xlsxPivotTableDefinition, opts *PivotTableOptions) error {
|
2020-04-09 01:00:14 +08:00
|
|
|
// page fields
|
2022-09-18 00:07:15 +08:00
|
|
|
pageFieldsIndex, err := f.getPivotFieldsIndex(opts.Filter, opts)
|
2020-04-09 01:00:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-18 00:07:15 +08:00
|
|
|
pageFieldsName := f.getPivotTableFieldsName(opts.Filter)
|
2020-04-09 01:00:14 +08:00
|
|
|
for idx, pageField := range pageFieldsIndex {
|
2020-04-10 00:04:23 +08:00
|
|
|
if pt.PageFields == nil {
|
|
|
|
pt.PageFields = &xlsxPageFields{}
|
|
|
|
}
|
2020-04-09 01:00:14 +08:00
|
|
|
pt.PageFields.PageField = append(pt.PageFields.PageField, &xlsxPageField{
|
|
|
|
Name: pageFieldsName[idx],
|
|
|
|
Fld: pageField,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-10 00:04:23 +08:00
|
|
|
// count page fields
|
|
|
|
if pt.PageFields != nil {
|
|
|
|
pt.PageFields.Count = len(pt.PageFields.PageField)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// addPivotDataFields provides a method to add data fields for pivot table by
|
|
|
|
// given pivot table options.
|
This closes #1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes:
Motivation and Context
When I decided to add set horizontal centered support for this library to resolve #1358, the reason I made this huge breaking change was:
- There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure.
- Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories
- Nested properties cannot proceed modify easily
Introduce 5 new export data types:
`HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions`
Rename 4 exported data types:
- Rename `PivotTableOption` to `PivotTableOptions`
- Rename `FormatHeaderFooter` to `HeaderFooterOptions`
- Rename `FormatSheetProtection` to `SheetProtectionOptions`
- Rename `SparklineOption` to `SparklineOptions`
Remove 54 exported types:
`AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale`
Remove 2 exported constants:
`OrientationPortrait` and `OrientationLandscape`
Change 8 functions:
- Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error`
- Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)`
- Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error`
- Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)`
- Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error`
- Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)`
- Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error`
- Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)`
Introduce new function to instead of existing functions:
- New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
2022-09-29 22:00:21 +08:00
|
|
|
func (f *File) addPivotDataFields(pt *xlsxPivotTableDefinition, opts *PivotTableOptions) error {
|
2019-09-20 00:20:30 +08:00
|
|
|
// data fields
|
2022-09-18 00:07:15 +08:00
|
|
|
dataFieldsIndex, err := f.getPivotFieldsIndex(opts.Data, opts)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-18 00:07:15 +08:00
|
|
|
dataFieldsSubtotals := f.getPivotTableFieldsSubtotal(opts.Data)
|
|
|
|
dataFieldsName := f.getPivotTableFieldsName(opts.Data)
|
2024-08-18 00:18:02 +08:00
|
|
|
dataFieldsNumFmtID := f.getPivotTableFieldsNumFmtID(opts.Data)
|
2020-02-26 18:53:50 +08:00
|
|
|
for idx, dataField := range dataFieldsIndex {
|
2020-04-10 00:04:23 +08:00
|
|
|
if pt.DataFields == nil {
|
|
|
|
pt.DataFields = &xlsxDataFields{}
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
pt.DataFields.DataField = append(pt.DataFields.DataField, &xlsxDataField{
|
2020-02-26 18:53:50 +08:00
|
|
|
Name: dataFieldsName[idx],
|
2020-02-21 23:07:43 +08:00
|
|
|
Fld: dataField,
|
2020-02-26 18:53:50 +08:00
|
|
|
Subtotal: dataFieldsSubtotals[idx],
|
2024-08-18 00:18:02 +08:00
|
|
|
NumFmtID: dataFieldsNumFmtID[idx],
|
2019-09-20 00:20:30 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// count data fields
|
2020-04-10 00:04:23 +08:00
|
|
|
if pt.DataFields != nil {
|
|
|
|
pt.DataFields.Count = len(pt.DataFields.DataField)
|
|
|
|
}
|
2019-09-20 00:20:30 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-26 18:53:50 +08:00
|
|
|
// inPivotTableField provides a method to check if an element is present in
|
|
|
|
// pivot table fields list, and return the index of its location, otherwise
|
|
|
|
// return -1.
|
|
|
|
func inPivotTableField(a []PivotTableField, x string) int {
|
|
|
|
for idx, n := range a {
|
|
|
|
if x == n.Data {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2019-10-24 23:18:02 +08:00
|
|
|
// addPivotColFields create pivot column fields by given pivot table
|
|
|
|
// definition and option.
|
This closes #1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes:
Motivation and Context
When I decided to add set horizontal centered support for this library to resolve #1358, the reason I made this huge breaking change was:
- There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure.
- Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories
- Nested properties cannot proceed modify easily
Introduce 5 new export data types:
`HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions`
Rename 4 exported data types:
- Rename `PivotTableOption` to `PivotTableOptions`
- Rename `FormatHeaderFooter` to `HeaderFooterOptions`
- Rename `FormatSheetProtection` to `SheetProtectionOptions`
- Rename `SparklineOption` to `SparklineOptions`
Remove 54 exported types:
`AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale`
Remove 2 exported constants:
`OrientationPortrait` and `OrientationLandscape`
Change 8 functions:
- Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error`
- Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)`
- Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error`
- Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)`
- Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error`
- Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)`
- Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error`
- Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)`
Introduce new function to instead of existing functions:
- New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
2022-09-29 22:00:21 +08:00
|
|
|
func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opts *PivotTableOptions) error {
|
2022-09-18 00:07:15 +08:00
|
|
|
if len(opts.Columns) == 0 {
|
|
|
|
if len(opts.Data) <= 1 {
|
2020-09-27 13:34:39 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pt.ColFields = &xlsxColFields{}
|
|
|
|
// in order to create pivot table in case there is no input from Columns
|
|
|
|
pt.ColFields.Count = 1
|
|
|
|
pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
|
|
|
|
X: -2,
|
|
|
|
})
|
2019-10-24 23:18:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pt.ColFields = &xlsxColFields{}
|
|
|
|
|
|
|
|
// col fields
|
2022-09-18 00:07:15 +08:00
|
|
|
colFieldsIndex, err := f.getPivotFieldsIndex(opts.Columns, opts)
|
2019-10-24 23:18:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-21 23:07:43 +08:00
|
|
|
for _, fieldIdx := range colFieldsIndex {
|
2019-10-24 23:18:02 +08:00
|
|
|
pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
|
2020-02-21 23:07:43 +08:00
|
|
|
X: fieldIdx,
|
2019-10-24 23:18:02 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-09 00:20:42 +08:00
|
|
|
// in order to create pivot in case there is many Columns and Data
|
2022-09-18 00:07:15 +08:00
|
|
|
if len(opts.Data) > 1 {
|
2020-10-01 00:20:11 +08:00
|
|
|
pt.ColFields.Field = append(pt.ColFields.Field, &xlsxField{
|
|
|
|
X: -2,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-24 23:18:02 +08:00
|
|
|
// count col fields
|
|
|
|
pt.ColFields.Count = len(pt.ColFields.Field)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-21 15:39:36 +08:00
|
|
|
// setClassicLayout provides a method to set classic layout for pivot table by
|
|
|
|
// setting Compact and Outline to false.
|
|
|
|
func (fld *xlsxPivotField) setClassicLayout(classicLayout bool) {
|
|
|
|
if classicLayout {
|
|
|
|
fld.Compact, fld.Outline = boolPtr(false), boolPtr(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:20:30 +08:00
|
|
|
// addPivotFields create pivot fields based on the column order of the first
|
|
|
|
// row in the data region by given pivot table definition and option.
|
This closes #1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes:
Motivation and Context
When I decided to add set horizontal centered support for this library to resolve #1358, the reason I made this huge breaking change was:
- There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure.
- Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories
- Nested properties cannot proceed modify easily
Introduce 5 new export data types:
`HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions`
Rename 4 exported data types:
- Rename `PivotTableOption` to `PivotTableOptions`
- Rename `FormatHeaderFooter` to `HeaderFooterOptions`
- Rename `FormatSheetProtection` to `SheetProtectionOptions`
- Rename `SparklineOption` to `SparklineOptions`
Remove 54 exported types:
`AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale`
Remove 2 exported constants:
`OrientationPortrait` and `OrientationLandscape`
Change 8 functions:
- Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error`
- Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)`
- Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error`
- Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)`
- Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error`
- Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)`
- Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error`
- Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)`
Introduce new function to instead of existing functions:
- New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
2022-09-29 22:00:21 +08:00
|
|
|
func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOptions) error {
|
2023-10-05 00:08:31 +08:00
|
|
|
order, err := f.getTableFieldsOrder(opts)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-11 00:45:52 +08:00
|
|
|
x := 0
|
2019-09-20 00:20:30 +08:00
|
|
|
for _, name := range order {
|
2022-09-18 00:07:15 +08:00
|
|
|
if inPivotTableField(opts.Rows, name) != -1 {
|
|
|
|
rowOptions, ok := f.getPivotTableFieldOptions(name, opts.Rows)
|
2020-09-11 00:45:52 +08:00
|
|
|
var items []*xlsxItem
|
2021-10-11 00:08:45 +08:00
|
|
|
if !ok || !rowOptions.DefaultSubtotal {
|
2020-09-11 00:45:52 +08:00
|
|
|
items = append(items, &xlsxItem{X: &x})
|
|
|
|
} else {
|
|
|
|
items = append(items, &xlsxItem{T: "default"})
|
|
|
|
}
|
2024-09-21 15:39:36 +08:00
|
|
|
fld := &xlsxPivotField{
|
2022-09-18 00:07:15 +08:00
|
|
|
Name: f.getPivotTableFieldName(name, opts.Rows),
|
2021-10-11 00:08:45 +08:00
|
|
|
Axis: "axisRow",
|
2022-09-18 00:07:15 +08:00
|
|
|
DataField: inPivotTableField(opts.Data, name) != -1,
|
2021-10-11 00:08:45 +08:00
|
|
|
Compact: &rowOptions.Compact,
|
|
|
|
Outline: &rowOptions.Outline,
|
2024-09-01 12:10:01 +08:00
|
|
|
ShowAll: rowOptions.ShowAll,
|
|
|
|
InsertBlankRow: rowOptions.InsertBlankRow,
|
2021-10-11 00:08:45 +08:00
|
|
|
DefaultSubtotal: &rowOptions.DefaultSubtotal,
|
2019-09-20 00:20:30 +08:00
|
|
|
Items: &xlsxItems{
|
2020-09-11 00:45:52 +08:00
|
|
|
Count: len(items),
|
|
|
|
Item: items,
|
2019-09-20 00:20:30 +08:00
|
|
|
},
|
2024-09-21 15:39:36 +08:00
|
|
|
}
|
|
|
|
fld.setClassicLayout(opts.ClassicLayout)
|
|
|
|
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, fld)
|
2019-09-20 00:20:30 +08:00
|
|
|
continue
|
|
|
|
}
|
2022-09-18 00:07:15 +08:00
|
|
|
if inPivotTableField(opts.Filter, name) != -1 {
|
2024-09-21 15:39:36 +08:00
|
|
|
fld := &xlsxPivotField{
|
2022-04-19 20:54:05 +08:00
|
|
|
Axis: "axisPage",
|
2022-09-18 00:07:15 +08:00
|
|
|
DataField: inPivotTableField(opts.Data, name) != -1,
|
|
|
|
Name: f.getPivotTableFieldName(name, opts.Columns),
|
2020-04-09 01:00:14 +08:00
|
|
|
Items: &xlsxItems{
|
|
|
|
Count: 1,
|
|
|
|
Item: []*xlsxItem{
|
|
|
|
{T: "default"},
|
|
|
|
},
|
|
|
|
},
|
2024-09-21 15:39:36 +08:00
|
|
|
}
|
|
|
|
fld.setClassicLayout(opts.ClassicLayout)
|
|
|
|
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, fld)
|
2020-04-09 01:00:14 +08:00
|
|
|
continue
|
|
|
|
}
|
2022-09-18 00:07:15 +08:00
|
|
|
if inPivotTableField(opts.Columns, name) != -1 {
|
|
|
|
columnOptions, ok := f.getPivotTableFieldOptions(name, opts.Columns)
|
2020-09-11 00:45:52 +08:00
|
|
|
var items []*xlsxItem
|
2021-10-11 00:08:45 +08:00
|
|
|
if !ok || !columnOptions.DefaultSubtotal {
|
2020-09-11 00:45:52 +08:00
|
|
|
items = append(items, &xlsxItem{X: &x})
|
|
|
|
} else {
|
|
|
|
items = append(items, &xlsxItem{T: "default"})
|
|
|
|
}
|
2024-09-21 15:39:36 +08:00
|
|
|
fld := &xlsxPivotField{
|
2022-09-18 00:07:15 +08:00
|
|
|
Name: f.getPivotTableFieldName(name, opts.Columns),
|
2021-10-11 00:08:45 +08:00
|
|
|
Axis: "axisCol",
|
2022-09-18 00:07:15 +08:00
|
|
|
DataField: inPivotTableField(opts.Data, name) != -1,
|
2021-10-11 00:08:45 +08:00
|
|
|
Compact: &columnOptions.Compact,
|
|
|
|
Outline: &columnOptions.Outline,
|
2024-09-01 12:10:01 +08:00
|
|
|
ShowAll: columnOptions.ShowAll,
|
|
|
|
InsertBlankRow: columnOptions.InsertBlankRow,
|
2021-10-11 00:08:45 +08:00
|
|
|
DefaultSubtotal: &columnOptions.DefaultSubtotal,
|
2019-09-20 00:20:30 +08:00
|
|
|
Items: &xlsxItems{
|
2020-09-11 00:45:52 +08:00
|
|
|
Count: len(items),
|
|
|
|
Item: items,
|
2019-09-20 00:20:30 +08:00
|
|
|
},
|
2024-09-21 15:39:36 +08:00
|
|
|
}
|
|
|
|
fld.setClassicLayout(opts.ClassicLayout)
|
|
|
|
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, fld)
|
2019-09-20 00:20:30 +08:00
|
|
|
continue
|
|
|
|
}
|
2022-09-18 00:07:15 +08:00
|
|
|
if inPivotTableField(opts.Data, name) != -1 {
|
2024-09-21 15:39:36 +08:00
|
|
|
fld := &xlsxPivotField{
|
2019-09-20 00:20:30 +08:00
|
|
|
DataField: true,
|
2024-09-21 15:39:36 +08:00
|
|
|
}
|
|
|
|
fld.setClassicLayout(opts.ClassicLayout)
|
|
|
|
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, fld)
|
2019-09-20 00:20:30 +08:00
|
|
|
continue
|
|
|
|
}
|
2024-09-21 15:39:36 +08:00
|
|
|
fld := &xlsxPivotField{}
|
|
|
|
fld.setClassicLayout(opts.ClassicLayout)
|
|
|
|
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, fld)
|
2019-09-20 00:20:30 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-08 00:09:41 +08:00
|
|
|
// countPivotTables provides a function to get pivot table files count storage
|
|
|
|
// in the folder xl/pivotTables.
|
2019-09-20 00:20:30 +08:00
|
|
|
func (f *File) countPivotTables() int {
|
|
|
|
count := 0
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Range(func(k, v interface{}) bool {
|
|
|
|
if strings.Contains(k.(string), "xl/pivotTables/pivotTable") {
|
2019-09-20 00:20:30 +08:00
|
|
|
count++
|
|
|
|
}
|
2021-07-05 00:03:56 +08:00
|
|
|
return true
|
|
|
|
})
|
2019-09-20 00:20:30 +08:00
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2023-09-08 00:09:41 +08:00
|
|
|
// countPivotCache provides a function to get pivot table cache definition files
|
|
|
|
// count storage in the folder xl/pivotCache.
|
2019-09-20 00:20:30 +08:00
|
|
|
func (f *File) countPivotCache() int {
|
|
|
|
count := 0
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Range(func(k, v interface{}) bool {
|
|
|
|
if strings.Contains(k.(string), "xl/pivotCache/pivotCacheDefinition") {
|
2019-09-20 00:20:30 +08:00
|
|
|
count++
|
|
|
|
}
|
2021-07-05 00:03:56 +08:00
|
|
|
return true
|
|
|
|
})
|
2019-09-20 00:20:30 +08:00
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPivotFieldsIndex convert the column of the first row in the data region
|
|
|
|
// to a sequential index by given fields and pivot option.
|
This closes #1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes:
Motivation and Context
When I decided to add set horizontal centered support for this library to resolve #1358, the reason I made this huge breaking change was:
- There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure.
- Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories
- Nested properties cannot proceed modify easily
Introduce 5 new export data types:
`HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions`
Rename 4 exported data types:
- Rename `PivotTableOption` to `PivotTableOptions`
- Rename `FormatHeaderFooter` to `HeaderFooterOptions`
- Rename `FormatSheetProtection` to `SheetProtectionOptions`
- Rename `SparklineOption` to `SparklineOptions`
Remove 54 exported types:
`AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale`
Remove 2 exported constants:
`OrientationPortrait` and `OrientationLandscape`
Change 8 functions:
- Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error`
- Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)`
- Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error`
- Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)`
- Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error`
- Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)`
- Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error`
- Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)`
Introduce new function to instead of existing functions:
- New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
2022-09-29 22:00:21 +08:00
|
|
|
func (f *File) getPivotFieldsIndex(fields []PivotTableField, opts *PivotTableOptions) ([]int, error) {
|
2022-03-24 00:19:30 +08:00
|
|
|
var pivotFieldsIndex []int
|
2023-10-05 00:08:31 +08:00
|
|
|
orders, err := f.getTableFieldsOrder(opts)
|
2019-09-20 00:20:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return pivotFieldsIndex, err
|
|
|
|
}
|
|
|
|
for _, field := range fields {
|
2022-02-13 00:06:30 +08:00
|
|
|
if pos := inStrSlice(orders, field.Data, true); pos != -1 {
|
2019-09-20 00:20:30 +08:00
|
|
|
pivotFieldsIndex = append(pivotFieldsIndex, pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pivotFieldsIndex, nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 18:53:50 +08:00
|
|
|
// getPivotTableFieldsSubtotal prepare fields subtotal by given pivot table fields.
|
|
|
|
func (f *File) getPivotTableFieldsSubtotal(fields []PivotTableField) []string {
|
|
|
|
field := make([]string, len(fields))
|
|
|
|
enums := []string{"average", "count", "countNums", "max", "min", "product", "stdDev", "stdDevp", "sum", "var", "varp"}
|
|
|
|
inEnums := func(enums []string, val string) string {
|
|
|
|
for _, enum := range enums {
|
2021-02-15 00:09:35 +08:00
|
|
|
if strings.EqualFold(enum, val) {
|
2020-02-26 18:53:50 +08:00
|
|
|
return enum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "sum"
|
|
|
|
}
|
|
|
|
for idx, fld := range fields {
|
|
|
|
field[idx] = inEnums(enums, fld.Subtotal)
|
|
|
|
}
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPivotTableFieldsName prepare fields name list by given pivot table
|
|
|
|
// fields.
|
|
|
|
func (f *File) getPivotTableFieldsName(fields []PivotTableField) []string {
|
|
|
|
field := make([]string, len(fields))
|
|
|
|
for idx, fld := range fields {
|
2021-11-16 00:40:44 +08:00
|
|
|
if len(fld.Name) > MaxFieldLength {
|
|
|
|
field[idx] = fld.Name[:MaxFieldLength]
|
2020-02-26 18:53:50 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
field[idx] = fld.Name
|
|
|
|
}
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPivotTableFieldName prepare field name by given pivot table fields.
|
|
|
|
func (f *File) getPivotTableFieldName(name string, fields []PivotTableField) string {
|
|
|
|
fieldsName := f.getPivotTableFieldsName(fields)
|
|
|
|
for idx, field := range fields {
|
|
|
|
if field.Data == name {
|
|
|
|
return fieldsName[idx]
|
2020-02-21 23:07:43 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 18:53:50 +08:00
|
|
|
return ""
|
2020-02-21 23:07:43 +08:00
|
|
|
}
|
|
|
|
|
2024-08-18 00:18:02 +08:00
|
|
|
// getPivotTableFieldsNumFmtID prepare fields number format ID by given pivot
|
|
|
|
// table fields.
|
|
|
|
func (f *File) getPivotTableFieldsNumFmtID(fields []PivotTableField) []int {
|
|
|
|
field := make([]int, len(fields))
|
|
|
|
for idx, fld := range fields {
|
|
|
|
if _, ok := builtInNumFmt[fld.NumFmt]; ok {
|
|
|
|
field[idx] = fld.NumFmt
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (27 <= fld.NumFmt && fld.NumFmt <= 36) || (50 <= fld.NumFmt && fld.NumFmt <= 81) {
|
|
|
|
field[idx] = fld.NumFmt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
|
2021-10-11 00:08:45 +08:00
|
|
|
// getPivotTableFieldOptions return options for specific field by given field name.
|
|
|
|
func (f *File) getPivotTableFieldOptions(name string, fields []PivotTableField) (options PivotTableField, ok bool) {
|
2020-09-11 00:45:52 +08:00
|
|
|
for _, field := range fields {
|
|
|
|
if field.Data == name {
|
2021-10-11 00:08:45 +08:00
|
|
|
options, ok = field, true
|
|
|
|
return
|
2020-09-11 00:45:52 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-11 00:08:45 +08:00
|
|
|
return
|
2020-09-11 00:45:52 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 00:28:20 +08:00
|
|
|
// addWorkbookPivotCache add the association ID of the pivot cache in workbook.xml.
|
2019-09-20 00:20:30 +08:00
|
|
|
func (f *File) addWorkbookPivotCache(RID int) int {
|
2022-11-13 00:40:04 +08:00
|
|
|
wb, _ := f.workbookReader()
|
2019-09-20 00:20:30 +08:00
|
|
|
if wb.PivotCaches == nil {
|
|
|
|
wb.PivotCaches = &xlsxPivotCaches{}
|
|
|
|
}
|
|
|
|
cacheID := 1
|
|
|
|
for _, pivotCache := range wb.PivotCaches.PivotCache {
|
|
|
|
if pivotCache.CacheID > cacheID {
|
|
|
|
cacheID = pivotCache.CacheID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cacheID++
|
|
|
|
wb.PivotCaches.PivotCache = append(wb.PivotCaches.PivotCache, xlsxPivotCache{
|
|
|
|
CacheID: cacheID,
|
|
|
|
RID: fmt.Sprintf("rId%d", RID),
|
|
|
|
})
|
|
|
|
return cacheID
|
|
|
|
}
|
2023-09-08 00:09:41 +08:00
|
|
|
|
|
|
|
// GetPivotTables returns all pivot table definitions in a worksheet by given
|
|
|
|
// worksheet name.
|
|
|
|
func (f *File) GetPivotTables(sheet string) ([]PivotTableOptions, error) {
|
|
|
|
var pivotTables []PivotTableOptions
|
|
|
|
name, ok := f.getSheetXMLPath(sheet)
|
|
|
|
if !ok {
|
2023-09-28 08:53:54 +08:00
|
|
|
return pivotTables, ErrSheetNotExist{sheet}
|
2023-09-08 00:09:41 +08:00
|
|
|
}
|
|
|
|
rels := "xl/worksheets/_rels/" + strings.TrimPrefix(name, "xl/worksheets/") + ".rels"
|
|
|
|
sheetRels, err := f.relsReader(rels)
|
|
|
|
if err != nil {
|
|
|
|
return pivotTables, err
|
|
|
|
}
|
|
|
|
if sheetRels == nil {
|
|
|
|
sheetRels = &xlsxRelationships{}
|
|
|
|
}
|
|
|
|
for _, v := range sheetRels.Relationships {
|
|
|
|
if v.Type == SourceRelationshipPivotTable {
|
|
|
|
pivotTableXML := strings.ReplaceAll(v.Target, "..", "xl")
|
|
|
|
pivotCacheRels := "xl/pivotTables/_rels/" + filepath.Base(v.Target) + ".rels"
|
|
|
|
pivotTable, err := f.getPivotTable(sheet, pivotTableXML, pivotCacheRels)
|
|
|
|
if err != nil {
|
|
|
|
return pivotTables, err
|
|
|
|
}
|
|
|
|
pivotTables = append(pivotTables, pivotTable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pivotTables, nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 00:08:31 +08:00
|
|
|
// getPivotTableDataRange checking given if data range is a cell reference or
|
|
|
|
// named reference (defined name or table name), and set pivot table data range.
|
|
|
|
func (f *File) getPivotTableDataRange(opts *PivotTableOptions) error {
|
|
|
|
if opts.DataRange == "" {
|
|
|
|
return newPivotTableDataRangeError(ErrParameterRequired.Error())
|
|
|
|
}
|
|
|
|
if opts.pivotDataRange != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if strings.Contains(opts.DataRange, "!") {
|
|
|
|
opts.pivotDataRange = opts.DataRange
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-23 10:47:47 +08:00
|
|
|
tbls, err := f.getTables()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for sheetName, tables := range tbls {
|
2023-10-05 00:08:31 +08:00
|
|
|
for _, table := range tables {
|
|
|
|
if table.Name == opts.DataRange {
|
|
|
|
opts.pivotDataRange, opts.namedDataRange = fmt.Sprintf("%s!%s", sheetName, table.Range), true
|
|
|
|
return err
|
2023-10-03 00:59:31 +08:00
|
|
|
}
|
|
|
|
}
|
2023-10-05 00:08:31 +08:00
|
|
|
}
|
|
|
|
if !opts.namedDataRange {
|
|
|
|
opts.pivotDataRange = f.getDefinedNameRefTo(opts.DataRange, opts.pivotSheetName)
|
|
|
|
if opts.pivotDataRange != "" {
|
|
|
|
opts.namedDataRange = true
|
|
|
|
return nil
|
2023-10-03 00:59:31 +08:00
|
|
|
}
|
|
|
|
}
|
2023-10-05 00:08:31 +08:00
|
|
|
return newPivotTableDataRangeError(ErrParameterInvalid.Error())
|
2023-10-03 00:59:31 +08:00
|
|
|
}
|
|
|
|
|
2023-09-08 00:09:41 +08:00
|
|
|
// getPivotTable provides a function to get a pivot table definition by given
|
|
|
|
// worksheet name, pivot table XML path and pivot cache relationship XML path.
|
|
|
|
func (f *File) getPivotTable(sheet, pivotTableXML, pivotCacheRels string) (PivotTableOptions, error) {
|
|
|
|
var opts PivotTableOptions
|
|
|
|
rels, err := f.relsReader(pivotCacheRels)
|
|
|
|
if err != nil {
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
var pivotCacheXML string
|
|
|
|
for _, v := range rels.Relationships {
|
|
|
|
if v.Type == SourceRelationshipPivotCache {
|
|
|
|
pivotCacheXML = strings.ReplaceAll(v.Target, "..", "xl")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pc, err := f.pivotCacheReader(pivotCacheXML)
|
|
|
|
if err != nil {
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
pt, err := f.pivotTableReader(pivotTableXML)
|
|
|
|
if err != nil {
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
opts = PivotTableOptions{
|
2024-09-01 12:10:01 +08:00
|
|
|
pivotTableXML: pivotTableXML,
|
|
|
|
pivotCacheXML: pivotCacheXML,
|
|
|
|
pivotSheetName: sheet,
|
|
|
|
DataRange: fmt.Sprintf("%s!%s", pc.CacheSource.WorksheetSource.Sheet, pc.CacheSource.WorksheetSource.Ref),
|
|
|
|
PivotTableRange: fmt.Sprintf("%s!%s", sheet, pt.Location.Ref),
|
|
|
|
Name: pt.Name,
|
2024-09-21 15:39:36 +08:00
|
|
|
ClassicLayout: pt.GridDropZones,
|
2024-09-01 12:10:01 +08:00
|
|
|
FieldPrintTitles: pt.FieldPrintTitles,
|
|
|
|
ItemPrintTitles: pt.ItemPrintTitles,
|
2023-10-05 00:08:31 +08:00
|
|
|
}
|
|
|
|
if pc.CacheSource.WorksheetSource.Name != "" {
|
|
|
|
opts.DataRange = pc.CacheSource.WorksheetSource.Name
|
|
|
|
_ = f.getPivotTableDataRange(&opts)
|
2023-09-08 00:09:41 +08:00
|
|
|
}
|
|
|
|
fields := []string{"RowGrandTotals", "ColGrandTotals", "ShowDrill", "UseAutoFormatting", "PageOverThenDown", "MergeItem", "CompactData", "ShowError"}
|
|
|
|
immutable, mutable := reflect.ValueOf(*pt), reflect.ValueOf(&opts).Elem()
|
|
|
|
for _, field := range fields {
|
|
|
|
immutableField := immutable.FieldByName(field)
|
|
|
|
if immutableField.Kind() == reflect.Ptr && !immutableField.IsNil() && immutableField.Elem().Kind() == reflect.Bool {
|
|
|
|
mutable.FieldByName(field).SetBool(immutableField.Elem().Bool())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if si := pt.PivotTableStyleInfo; si != nil {
|
|
|
|
opts.ShowRowHeaders = si.ShowRowHeaders
|
|
|
|
opts.ShowColHeaders = si.ShowColHeaders
|
|
|
|
opts.ShowRowStripes = si.ShowRowStripes
|
|
|
|
opts.ShowColStripes = si.ShowColStripes
|
|
|
|
opts.ShowLastColumn = si.ShowLastColumn
|
|
|
|
opts.PivotTableStyleName = si.Name
|
|
|
|
}
|
2023-10-05 00:08:31 +08:00
|
|
|
order, err := f.getTableFieldsOrder(&opts)
|
|
|
|
if err != nil {
|
|
|
|
return opts, err
|
|
|
|
}
|
2023-09-08 00:09:41 +08:00
|
|
|
f.extractPivotTableFields(order, pt, &opts)
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// pivotTableReader provides a function to get the pointer to the structure
|
|
|
|
// after deserialization of xl/pivotTables/pivotTable%d.xml.
|
|
|
|
func (f *File) pivotTableReader(path string) (*xlsxPivotTableDefinition, error) {
|
|
|
|
content, ok := f.Pkg.Load(path)
|
|
|
|
pivotTable := &xlsxPivotTableDefinition{}
|
|
|
|
if ok && content != nil {
|
|
|
|
if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(content.([]byte)))).
|
|
|
|
Decode(pivotTable); err != nil && err != io.EOF {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pivotTable, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pivotCacheReader provides a function to get the pointer to the structure
|
|
|
|
// after deserialization of xl/pivotCache/pivotCacheDefinition%d.xml.
|
|
|
|
func (f *File) pivotCacheReader(path string) (*xlsxPivotCacheDefinition, error) {
|
|
|
|
content, ok := f.Pkg.Load(path)
|
|
|
|
pivotCache := &xlsxPivotCacheDefinition{}
|
|
|
|
if ok && content != nil {
|
|
|
|
if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(content.([]byte)))).
|
|
|
|
Decode(pivotCache); err != nil && err != io.EOF {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pivotCache, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractPivotTableFields provides a function to extract all pivot table fields
|
|
|
|
// settings by given pivot table fields.
|
|
|
|
func (f *File) extractPivotTableFields(order []string, pt *xlsxPivotTableDefinition, opts *PivotTableOptions) {
|
|
|
|
for fieldIdx, field := range pt.PivotFields.PivotField {
|
|
|
|
if field.Axis == "axisRow" {
|
|
|
|
opts.Rows = append(opts.Rows, extractPivotTableField(order[fieldIdx], field))
|
|
|
|
}
|
|
|
|
if field.Axis == "axisCol" {
|
|
|
|
opts.Columns = append(opts.Columns, extractPivotTableField(order[fieldIdx], field))
|
|
|
|
}
|
|
|
|
if field.Axis == "axisPage" {
|
|
|
|
opts.Filter = append(opts.Filter, extractPivotTableField(order[fieldIdx], field))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pt.DataFields != nil {
|
|
|
|
for _, field := range pt.DataFields.DataField {
|
|
|
|
opts.Data = append(opts.Data, PivotTableField{
|
|
|
|
Data: order[field.Fld],
|
|
|
|
Name: field.Name,
|
|
|
|
Subtotal: cases.Title(language.English).String(field.Subtotal),
|
2024-08-18 00:18:02 +08:00
|
|
|
NumFmt: field.NumFmtID,
|
2023-09-08 00:09:41 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractPivotTableField provides a function to extract pivot table field
|
|
|
|
// settings by given pivot table fields.
|
|
|
|
func extractPivotTableField(data string, fld *xlsxPivotField) PivotTableField {
|
|
|
|
pivotTableField := PivotTableField{
|
2024-09-01 12:10:01 +08:00
|
|
|
Data: data,
|
|
|
|
ShowAll: fld.ShowAll,
|
|
|
|
InsertBlankRow: fld.InsertBlankRow,
|
2023-09-08 00:09:41 +08:00
|
|
|
}
|
|
|
|
fields := []string{"Compact", "Name", "Outline", "Subtotal", "DefaultSubtotal"}
|
|
|
|
immutable, mutable := reflect.ValueOf(*fld), reflect.ValueOf(&pivotTableField).Elem()
|
|
|
|
for _, field := range fields {
|
|
|
|
immutableField := immutable.FieldByName(field)
|
|
|
|
if immutableField.Kind() == reflect.String {
|
|
|
|
mutable.FieldByName(field).SetString(immutableField.String())
|
|
|
|
}
|
|
|
|
if immutableField.Kind() == reflect.Ptr && !immutableField.IsNil() && immutableField.Elem().Kind() == reflect.Bool {
|
|
|
|
mutable.FieldByName(field).SetBool(immutableField.Elem().Bool())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pivotTableField
|
|
|
|
}
|
2023-09-27 00:05:59 +08:00
|
|
|
|
|
|
|
// genPivotCacheDefinitionID generates a unique pivot table cache definition ID.
|
|
|
|
func (f *File) genPivotCacheDefinitionID() int {
|
|
|
|
var (
|
|
|
|
ID int
|
|
|
|
decodeExtLst = new(decodeExtLst)
|
|
|
|
decodeX14PivotCacheDefinition = new(decodeX14PivotCacheDefinition)
|
|
|
|
)
|
|
|
|
f.Pkg.Range(func(k, v interface{}) bool {
|
|
|
|
if strings.Contains(k.(string), "xl/pivotCache/pivotCacheDefinition") {
|
|
|
|
pc, err := f.pivotCacheReader(k.(string))
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if pc.ExtLst != nil {
|
|
|
|
_ = f.xmlNewDecoder(strings.NewReader("<extLst>" + pc.ExtLst.Ext + "</extLst>")).Decode(decodeExtLst)
|
|
|
|
for _, ext := range decodeExtLst.Ext {
|
|
|
|
if ext.URI == ExtURIPivotCacheDefinition {
|
|
|
|
_ = f.xmlNewDecoder(strings.NewReader(ext.Content)).Decode(decodeX14PivotCacheDefinition)
|
|
|
|
if ID < decodeX14PivotCacheDefinition.PivotCacheID {
|
|
|
|
ID = decodeX14PivotCacheDefinition.PivotCacheID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return ID + 1
|
|
|
|
}
|
2023-10-03 00:59:31 +08:00
|
|
|
|
|
|
|
// deleteWorkbookPivotCache remove workbook pivot cache and pivot cache
|
|
|
|
// relationships.
|
|
|
|
func (f *File) deleteWorkbookPivotCache(opt PivotTableOptions) error {
|
|
|
|
rID, err := f.deleteWorkbookRels(SourceRelationshipPivotCache, strings.TrimPrefix(strings.TrimPrefix(opt.pivotCacheXML, "/"), "xl/"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wb, err := f.workbookReader()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if wb.PivotCaches != nil {
|
|
|
|
for i, pivotCache := range wb.PivotCaches.PivotCache {
|
|
|
|
if pivotCache.RID == rID {
|
|
|
|
wb.PivotCaches.PivotCache = append(wb.PivotCaches.PivotCache[:i], wb.PivotCaches.PivotCache[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(wb.PivotCaches.PivotCache) == 0 {
|
|
|
|
wb.PivotCaches = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePivotTable delete a pivot table by giving the worksheet name and pivot
|
|
|
|
// table name. Note that this function does not clean cell values in the pivot
|
|
|
|
// table range.
|
|
|
|
func (f *File) DeletePivotTable(sheet, name string) error {
|
|
|
|
sheetXML, ok := f.getSheetXMLPath(sheet)
|
|
|
|
if !ok {
|
|
|
|
return ErrSheetNotExist{sheet}
|
|
|
|
}
|
|
|
|
rels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetXML, "xl/worksheets/") + ".rels"
|
|
|
|
sheetRels, err := f.relsReader(rels)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sheetRels == nil {
|
|
|
|
sheetRels = &xlsxRelationships{}
|
|
|
|
}
|
|
|
|
opts, err := f.GetPivotTables(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pivotTableCaches := map[string]int{}
|
2024-08-23 10:47:47 +08:00
|
|
|
pivotTables, _ := f.getPivotTables()
|
|
|
|
for _, sheetPivotTables := range pivotTables {
|
2023-10-03 00:59:31 +08:00
|
|
|
for _, sheetPivotTable := range sheetPivotTables {
|
|
|
|
pivotTableCaches[sheetPivotTable.pivotCacheXML]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range sheetRels.Relationships {
|
|
|
|
for _, opt := range opts {
|
|
|
|
if v.Type == SourceRelationshipPivotTable {
|
|
|
|
pivotTableXML := strings.ReplaceAll(v.Target, "..", "xl")
|
|
|
|
if opt.Name == name && opt.pivotTableXML == pivotTableXML {
|
|
|
|
if pivotTableCaches[opt.pivotCacheXML] == 1 {
|
|
|
|
err = f.deleteWorkbookPivotCache(opt)
|
|
|
|
}
|
|
|
|
f.deleteSheetRelationships(sheet, v.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newNoExistTableError(name)
|
|
|
|
}
|
2024-08-23 10:47:47 +08:00
|
|
|
|
|
|
|
// getPivotTables provides a function to get all pivot tables in a workbook.
|
|
|
|
func (f *File) getPivotTables() (map[string][]PivotTableOptions, error) {
|
|
|
|
pivotTables := map[string][]PivotTableOptions{}
|
|
|
|
for _, sheetName := range f.GetSheetList() {
|
|
|
|
pts, err := f.GetPivotTables(sheetName)
|
|
|
|
e := ErrSheetNotExist{sheetName}
|
|
|
|
if err != nil && err.Error() != newNotWorksheetError(sheetName).Error() && err.Error() != e.Error() {
|
|
|
|
return pivotTables, err
|
|
|
|
}
|
|
|
|
pivotTables[sheetName] = append(pivotTables[sheetName], pts...)
|
|
|
|
}
|
|
|
|
return pivotTables, nil
|
|
|
|
}
|