summaryrefslogtreecommitdiff
path: root/excelize_test.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2022-09-29 22:00:21 +0800
committerxuri <xuri.me@gmail.com>2022-09-29 22:04:50 +0800
commit53a495563a2b9acf09ae45eae05d5f33aa242a87 (patch)
tree36ae707b1e38e755063aec7c13e878126a128c84 /excelize_test.go
parentefcf599dfe2ec25f10c4d55513a5648addfe989b (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 'excelize_test.go')
-rw-r--r--excelize_test.go26
1 files changed, 10 insertions, 16 deletions
diff --git a/excelize_test.go b/excelize_test.go
index 9bb6fa8..e685b66 100644
--- a/excelize_test.go
+++ b/excelize_test.go
@@ -319,13 +319,13 @@ func TestNewFile(t *testing.T) {
t.FailNow()
}
- // Test add picture to worksheet without formatset.
+ // Test add picture to worksheet without options.
err = f.AddPicture("Sheet1", "C2", filepath.Join("test", "images", "excel.png"), "")
if !assert.NoError(t, err) {
t.FailNow()
}
- // Test add picture to worksheet with invalid formatset.
+ // Test add picture to worksheet with invalid options.
err = f.AddPicture("Sheet1", "C2", filepath.Join("test", "images", "excel.png"), `{`)
if !assert.Error(t, err) {
t.FailNow()
@@ -1021,12 +1021,6 @@ func TestRelsWriter(t *testing.T) {
f.relsWriter()
}
-func TestGetSheetView(t *testing.T) {
- f := NewFile()
- _, err := f.getSheetView("SheetN", 0)
- assert.EqualError(t, err, "sheet SheetN does not exist")
-}
-
func TestConditionalFormat(t *testing.T) {
f := NewFile()
sheet1 := f.GetSheetName(0)
@@ -1228,7 +1222,7 @@ func TestProtectSheet(t *testing.T) {
sheetName := f.GetSheetName(0)
assert.NoError(t, f.ProtectSheet(sheetName, nil))
// Test protect worksheet with XOR hash algorithm
- assert.NoError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{
+ assert.NoError(t, f.ProtectSheet(sheetName, &SheetProtectionOptions{
Password: "password",
EditScenarios: false,
}))
@@ -1237,7 +1231,7 @@ func TestProtectSheet(t *testing.T) {
assert.Equal(t, "83AF", ws.SheetProtection.Password)
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestProtectSheet.xlsx")))
// Test protect worksheet with SHA-512 hash algorithm
- assert.NoError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{
+ assert.NoError(t, f.ProtectSheet(sheetName, &SheetProtectionOptions{
AlgorithmName: "SHA-512",
Password: "password",
}))
@@ -1251,15 +1245,15 @@ func TestProtectSheet(t *testing.T) {
// Test remove sheet protection with password verification
assert.NoError(t, f.UnprotectSheet(sheetName, "password"))
// Test protect worksheet with empty password
- assert.NoError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{}))
+ assert.NoError(t, f.ProtectSheet(sheetName, &SheetProtectionOptions{}))
assert.Equal(t, "", ws.SheetProtection.Password)
// Test protect worksheet with password exceeds the limit length
- assert.EqualError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{
+ assert.EqualError(t, f.ProtectSheet(sheetName, &SheetProtectionOptions{
AlgorithmName: "MD4",
Password: strings.Repeat("s", MaxFieldLength+1),
}), ErrPasswordLengthInvalid.Error())
// Test protect worksheet with unsupported hash algorithm
- assert.EqualError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{
+ assert.EqualError(t, f.ProtectSheet(sheetName, &SheetProtectionOptions{
AlgorithmName: "RIPEMD-160",
Password: "password",
}), ErrUnsupportedHashAlgorithm.Error())
@@ -1282,13 +1276,13 @@ func TestUnprotectSheet(t *testing.T) {
f = NewFile()
sheetName := f.GetSheetName(0)
- assert.NoError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{Password: "password"}))
+ assert.NoError(t, f.ProtectSheet(sheetName, &SheetProtectionOptions{Password: "password"}))
// Test remove sheet protection with an incorrect password
assert.EqualError(t, f.UnprotectSheet(sheetName, "wrongPassword"), ErrUnprotectSheetPassword.Error())
// Test remove sheet protection with password verification
assert.NoError(t, f.UnprotectSheet(sheetName, "password"))
// Test with invalid salt value
- assert.NoError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{
+ assert.NoError(t, f.ProtectSheet(sheetName, &SheetProtectionOptions{
AlgorithmName: "SHA-512",
Password: "password",
}))
@@ -1309,7 +1303,7 @@ func TestSetDefaultTimeStyle(t *testing.T) {
func TestAddVBAProject(t *testing.T) {
f := NewFile()
- assert.NoError(t, f.SetSheetPrOptions("Sheet1", CodeName("Sheet1")))
+ assert.NoError(t, f.SetSheetProps("Sheet1", &SheetPropsOptions{CodeName: stringPtr("Sheet1")}))
assert.EqualError(t, f.AddVBAProject("macros.bin"), "stat macros.bin: no such file or directory")
assert.EqualError(t, f.AddVBAProject(filepath.Join("test", "Book1.xlsx")), ErrAddVBAProject.Error())
assert.NoError(t, f.AddVBAProject(filepath.Join("test", "vbaProject.bin")))