diff options
author | xuri <xuri.me@gmail.com> | 2022-09-29 22:00:21 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2022-09-29 22:04:50 +0800 |
commit | 53a495563a2b9acf09ae45eae05d5f33aa242a87 (patch) | |
tree | 36ae707b1e38e755063aec7c13e878126a128c84 /table.go | |
parent | efcf599dfe2ec25f10c4d55513a5648addfe989b (diff) |
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
Diffstat (limited to 'table.go')
-rw-r--r-- | table.go | 60 |
1 files changed, 30 insertions, 30 deletions
@@ -20,12 +20,12 @@ import ( "strings" ) -// parseFormatTableSet provides a function to parse the format settings of the +// parseTableOptions provides a function to parse the format settings of the // table with default value. -func parseFormatTableSet(formatSet string) (*formatTable, error) { - format := formatTable{ShowRowStripes: true} - err := json.Unmarshal(parseFormatSet(formatSet), &format) - return &format, err +func parseTableOptions(opts string) (*tableOptions, error) { + options := tableOptions{ShowRowStripes: true} + err := json.Unmarshal(fallbackOptions(opts), &options) + return &options, err } // AddTable provides the method to add table in a worksheet by given worksheet @@ -57,8 +57,8 @@ func parseFormatTableSet(formatSet string) (*formatTable, error) { // TableStyleLight1 - TableStyleLight21 // TableStyleMedium1 - TableStyleMedium28 // TableStyleDark1 - TableStyleDark11 -func (f *File) AddTable(sheet, hCell, vCell, format string) error { - formatSet, err := parseFormatTableSet(format) +func (f *File) AddTable(sheet, hCell, vCell, opts string) error { + options, err := parseTableOptions(opts) if err != nil { return err } @@ -91,7 +91,7 @@ func (f *File) AddTable(sheet, hCell, vCell, format string) error { return err } f.addSheetNameSpace(sheet, SourceRelationship) - if err = f.addTable(sheet, tableXML, hCol, hRow, vCol, vRow, tableID, formatSet); err != nil { + if err = f.addTable(sheet, tableXML, hCol, hRow, vCol, vRow, tableID, options); err != nil { return err } f.addContentTypePart(tableID, "table") @@ -160,7 +160,7 @@ func (f *File) setTableHeader(sheet string, x1, y1, x2 int) ([]*xlsxTableColumn, // addTable provides a function to add table by given worksheet name, // range reference and format set. -func (f *File) addTable(sheet, tableXML string, x1, y1, x2, y2, i int, formatSet *formatTable) error { +func (f *File) addTable(sheet, tableXML string, x1, y1, x2, y2, i int, opts *tableOptions) error { // Correct the minimum number of rows, the table at least two lines. if y1 == y2 { y2++ @@ -172,7 +172,7 @@ func (f *File) addTable(sheet, tableXML string, x1, y1, x2, y2, i int, formatSet return err } tableColumns, _ := f.setTableHeader(sheet, x1, y1, x2) - name := formatSet.TableName + name := opts.TableName if name == "" { name = "Table" + strconv.Itoa(i) } @@ -190,11 +190,11 @@ func (f *File) addTable(sheet, tableXML string, x1, y1, x2, y2, i int, formatSet TableColumn: tableColumns, }, TableStyleInfo: &xlsxTableStyleInfo{ - Name: formatSet.TableStyle, - ShowFirstColumn: formatSet.ShowFirstColumn, - ShowLastColumn: formatSet.ShowLastColumn, - ShowRowStripes: formatSet.ShowRowStripes, - ShowColumnStripes: formatSet.ShowColumnStripes, + Name: opts.TableStyle, + ShowFirstColumn: opts.ShowFirstColumn, + ShowLastColumn: opts.ShowLastColumn, + ShowRowStripes: opts.ShowRowStripes, + ShowColumnStripes: opts.ShowColumnStripes, }, } table, _ := xml.Marshal(t) @@ -202,12 +202,12 @@ func (f *File) addTable(sheet, tableXML string, x1, y1, x2, y2, i int, formatSet return nil } -// parseAutoFilterSet provides a function to parse the settings of the auto +// parseAutoFilterOptions provides a function to parse the settings of the auto // filter. -func parseAutoFilterSet(formatSet string) (*formatAutoFilter, error) { - format := formatAutoFilter{} - err := json.Unmarshal([]byte(formatSet), &format) - return &format, err +func parseAutoFilterOptions(opts string) (*autoFilterOptions, error) { + options := autoFilterOptions{} + err := json.Unmarshal([]byte(opts), &options) + return &options, err } // AutoFilter provides the method to add auto filter in a worksheet by given @@ -279,7 +279,7 @@ func parseAutoFilterSet(formatSet string) (*formatAutoFilter, error) { // x < 2000 // col < 2000 // Price < 2000 -func (f *File) AutoFilter(sheet, hCell, vCell, format string) error { +func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error { hCol, hRow, err := CellNameToCoordinates(hCell) if err != nil { return err @@ -297,7 +297,7 @@ func (f *File) AutoFilter(sheet, hCell, vCell, format string) error { vRow, hRow = hRow, vRow } - formatSet, _ := parseAutoFilterSet(format) + options, _ := parseAutoFilterOptions(opts) cellStart, _ := CoordinatesToCellName(hCol, hRow, true) cellEnd, _ := CoordinatesToCellName(vCol, vRow, true) ref, filterDB := cellStart+":"+cellEnd, "_xlnm._FilterDatabase" @@ -328,12 +328,12 @@ func (f *File) AutoFilter(sheet, hCell, vCell, format string) error { } } refRange := vCol - hCol - return f.autoFilter(sheet, ref, refRange, hCol, formatSet) + return f.autoFilter(sheet, ref, refRange, hCol, options) } // autoFilter provides a function to extract the tokens from the filter // expression. The tokens are mainly non-whitespace groups. -func (f *File) autoFilter(sheet, ref string, refRange, col int, formatSet *formatAutoFilter) error { +func (f *File) autoFilter(sheet, ref string, refRange, col int, opts *autoFilterOptions) error { ws, err := f.workSheetReader(sheet) if err != nil { return err @@ -346,28 +346,28 @@ func (f *File) autoFilter(sheet, ref string, refRange, col int, formatSet *forma Ref: ref, } ws.AutoFilter = filter - if formatSet.Column == "" || formatSet.Expression == "" { + if opts.Column == "" || opts.Expression == "" { return nil } - fsCol, err := ColumnNameToNumber(formatSet.Column) + fsCol, err := ColumnNameToNumber(opts.Column) if err != nil { return err } offset := fsCol - col if offset < 0 || offset > refRange { - return fmt.Errorf("incorrect index of column '%s'", formatSet.Column) + return fmt.Errorf("incorrect index of column '%s'", opts.Column) } filter.FilterColumn = append(filter.FilterColumn, &xlsxFilterColumn{ ColID: offset, }) re := regexp.MustCompile(`"(?:[^"]|"")*"|\S+`) - token := re.FindAllString(formatSet.Expression, -1) + token := re.FindAllString(opts.Expression, -1) if len(token) != 3 && len(token) != 7 { - return fmt.Errorf("incorrect number of tokens in criteria '%s'", formatSet.Expression) + return fmt.Errorf("incorrect number of tokens in criteria '%s'", opts.Expression) } - expressions, tokens, err := f.parseFilterExpression(formatSet.Expression, token) + expressions, tokens, err := f.parseFilterExpression(opts.Expression, token) if err != nil { return err } |