From 53a495563a2b9acf09ae45eae05d5f33aa242a87 Mon Sep 17 00:00:00 2001 From: xuri Date: Thu, 29 Sep 2022 22:00:21 +0800 Subject: 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 --- workbook_test.go | 68 +++++++++----------------------------------------------- 1 file changed, 11 insertions(+), 57 deletions(-) (limited to 'workbook_test.go') diff --git a/workbook_test.go b/workbook_test.go index 18b222c..29571fa 100644 --- a/workbook_test.go +++ b/workbook_test.go @@ -1,69 +1,23 @@ package excelize import ( - "fmt" "testing" "github.com/stretchr/testify/assert" ) -func ExampleFile_SetWorkbookPrOptions() { - f := NewFile() - if err := f.SetWorkbookPrOptions( - Date1904(false), - FilterPrivacy(false), - CodeName("code"), - ); err != nil { - fmt.Println(err) - } - // Output: -} - -func ExampleFile_GetWorkbookPrOptions() { - f := NewFile() - var ( - date1904 Date1904 - filterPrivacy FilterPrivacy - codeName CodeName - ) - if err := f.GetWorkbookPrOptions(&date1904); err != nil { - fmt.Println(err) - } - if err := f.GetWorkbookPrOptions(&filterPrivacy); err != nil { - fmt.Println(err) - } - if err := f.GetWorkbookPrOptions(&codeName); err != nil { - fmt.Println(err) - } - fmt.Println("Defaults:") - fmt.Printf("- date1904: %t\n", date1904) - fmt.Printf("- filterPrivacy: %t\n", filterPrivacy) - fmt.Printf("- codeName: %q\n", codeName) - // Output: - // Defaults: - // - date1904: false - // - filterPrivacy: true - // - codeName: "" -} - -func TestWorkbookPr(t *testing.T) { +func TestWorkbookProps(t *testing.T) { f := NewFile() + assert.NoError(t, f.SetWorkbookProps(nil)) wb := f.workbookReader() wb.WorkbookPr = nil - var date1904 Date1904 - assert.NoError(t, f.GetWorkbookPrOptions(&date1904)) - assert.Equal(t, false, bool(date1904)) - - wb.WorkbookPr = nil - var codeName CodeName - assert.NoError(t, f.GetWorkbookPrOptions(&codeName)) - assert.Equal(t, "", string(codeName)) - assert.NoError(t, f.SetWorkbookPrOptions(CodeName("code"))) - assert.NoError(t, f.GetWorkbookPrOptions(&codeName)) - assert.Equal(t, "code", string(codeName)) - - wb.WorkbookPr = nil - var filterPrivacy FilterPrivacy - assert.NoError(t, f.GetWorkbookPrOptions(&filterPrivacy)) - assert.Equal(t, false, bool(filterPrivacy)) + expected := WorkbookPropsOptions{ + Date1904: boolPtr(true), + FilterPrivacy: boolPtr(true), + CodeName: stringPtr("code"), + } + assert.NoError(t, f.SetWorkbookProps(&expected)) + opts, err := f.GetWorkbookProps() + assert.NoError(t, err) + assert.Equal(t, expected, opts) } -- cgit v1.2.1