2020-10-04 21:07:39 +08:00
|
|
|
package excelize
|
2019-01-06 14:12:31 +08:00
|
|
|
|
|
|
|
import (
|
2022-03-05 14:48:34 +08:00
|
|
|
"encoding/xml"
|
2019-01-06 14:12:31 +08:00
|
|
|
"fmt"
|
2019-05-05 16:25:57 +08:00
|
|
|
"path/filepath"
|
2020-11-03 17:48:37 +08:00
|
|
|
"strconv"
|
2019-05-05 16:25:57 +08:00
|
|
|
"strings"
|
2019-01-06 14:12:31 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-10-24 23:18:02 +08:00
|
|
|
func TestNewSheet(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2021-07-10 23:47:35 +08:00
|
|
|
f.NewSheet("Sheet2")
|
|
|
|
sheetID := f.NewSheet("sheet2")
|
2019-10-24 23:18:02 +08:00
|
|
|
f.SetActiveSheet(sheetID)
|
|
|
|
// delete original sheet
|
|
|
|
f.DeleteSheet(f.GetSheetName(f.GetSheetIndex("Sheet1")))
|
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestNewSheet.xlsx")))
|
2020-10-19 23:55:54 +08:00
|
|
|
// create new worksheet with already exists name
|
|
|
|
assert.Equal(t, f.GetSheetIndex("Sheet2"), f.NewSheet("Sheet2"))
|
2022-09-28 00:04:17 +08:00
|
|
|
// create new worksheet with empty sheet name
|
|
|
|
assert.Equal(t, -1, f.NewSheet(":\\/?*[]"))
|
2019-10-24 23:18:02 +08:00
|
|
|
}
|
|
|
|
|
2019-12-22 00:02:09 +08:00
|
|
|
func TestSetPane(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-12-22 00:02:09 +08:00
|
|
|
assert.NoError(t, f.SetPanes("Sheet1", `{"freeze":false,"split":false}`))
|
|
|
|
f.NewSheet("Panes 2")
|
|
|
|
assert.NoError(t, f.SetPanes("Panes 2", `{"freeze":true,"split":false,"x_split":1,"y_split":0,"top_left_cell":"B1","active_pane":"topRight","panes":[{"sqref":"K16","active_cell":"K16","pane":"topRight"}]}`))
|
|
|
|
f.NewSheet("Panes 3")
|
|
|
|
assert.NoError(t, f.SetPanes("Panes 3", `{"freeze":false,"split":true,"x_split":3270,"y_split":1800,"top_left_cell":"N57","active_pane":"bottomLeft","panes":[{"sqref":"I36","active_cell":"I36"},{"sqref":"G33","active_cell":"G33","pane":"topRight"},{"sqref":"J60","active_cell":"J60","pane":"bottomLeft"},{"sqref":"O60","active_cell":"O60","pane":"bottomRight"}]}`))
|
|
|
|
f.NewSheet("Panes 4")
|
|
|
|
assert.NoError(t, f.SetPanes("Panes 4", `{"freeze":true,"split":false,"x_split":0,"y_split":9,"top_left_cell":"A34","active_pane":"bottomLeft","panes":[{"sqref":"A11:XFD11","active_cell":"A11","pane":"bottomLeft"}]}`))
|
2022-10-11 00:05:02 +08:00
|
|
|
assert.EqualError(t, f.SetPanes("Panes 4", ""), "unexpected end of JSON input")
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.SetPanes("SheetN", ""), "sheet SheetN does not exist")
|
2019-12-22 00:02:09 +08:00
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetPane.xlsx")))
|
2022-05-23 13:02:11 +08:00
|
|
|
// Test add pane on empty sheet views worksheet
|
|
|
|
f = NewFile()
|
|
|
|
f.checked = nil
|
|
|
|
f.Sheet.Delete("xl/worksheets/sheet1.xml")
|
|
|
|
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(`<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData/></worksheet>`))
|
|
|
|
assert.NoError(t, f.SetPanes("Sheet1", `{"freeze":true,"split":false,"x_split":1,"y_split":0,"top_left_cell":"B1","active_pane":"topRight","panes":[{"sqref":"K16","active_cell":"K16","pane":"topRight"}]}`))
|
2019-12-22 00:02:09 +08:00
|
|
|
}
|
|
|
|
|
2019-06-18 23:07:44 +08:00
|
|
|
func TestSearchSheet(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f, err := OpenFile(filepath.Join("test", "SharedStrings.xlsx"))
|
2019-06-18 23:07:44 +08:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Test search in a not exists worksheet.
|
|
|
|
_, err = f.SearchSheet("Sheet4", "")
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, err, "sheet Sheet4 does not exist")
|
2019-06-18 23:07:44 +08:00
|
|
|
var expected []string
|
|
|
|
// Test search a not exists value.
|
|
|
|
result, err := f.SearchSheet("Sheet1", "X")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, expected, result)
|
|
|
|
result, err = f.SearchSheet("Sheet1", "A")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, []string{"A1"}, result)
|
|
|
|
// Test search the coordinates where the numerical value in the range of
|
|
|
|
// "0-9" of Sheet1 is described by regular expression:
|
|
|
|
result, err = f.SearchSheet("Sheet1", "[0-9]", true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, expected, result)
|
2021-09-18 23:20:24 +08:00
|
|
|
assert.NoError(t, f.Close())
|
2019-12-22 00:02:09 +08:00
|
|
|
|
|
|
|
// Test search worksheet data after set cell value
|
2020-10-04 21:07:39 +08:00
|
|
|
f = NewFile()
|
2019-12-22 00:02:09 +08:00
|
|
|
assert.NoError(t, f.SetCellValue("Sheet1", "A1", true))
|
|
|
|
_, err = f.SearchSheet("Sheet1", "")
|
|
|
|
assert.NoError(t, err)
|
2021-12-03 00:19:11 +08:00
|
|
|
|
|
|
|
f = NewFile()
|
|
|
|
f.Sheet.Delete("xl/worksheets/sheet1.xml")
|
|
|
|
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(`<worksheet><sheetData><row r="A"><c r="2" t="str"><v>A</v></c></row></sheetData></worksheet>`))
|
|
|
|
f.checked = nil
|
|
|
|
result, err = f.SearchSheet("Sheet1", "A")
|
|
|
|
assert.EqualError(t, err, "strconv.Atoi: parsing \"A\": invalid syntax")
|
|
|
|
assert.Equal(t, []string(nil), result)
|
|
|
|
|
|
|
|
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(`<worksheet><sheetData><row r="2"><c r="A" t="str"><v>A</v></c></row></sheetData></worksheet>`))
|
|
|
|
result, err = f.SearchSheet("Sheet1", "A")
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, err, newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2021-12-03 00:19:11 +08:00
|
|
|
assert.Equal(t, []string(nil), result)
|
|
|
|
|
|
|
|
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(`<worksheet><sheetData><row r="0"><c r="A1" t="str"><v>A</v></c></row></sheetData></worksheet>`))
|
|
|
|
result, err = f.SearchSheet("Sheet1", "A")
|
2022-09-18 00:07:15 +08:00
|
|
|
assert.EqualError(t, err, "invalid cell reference [1, 0]")
|
2021-12-03 00:19:11 +08:00
|
|
|
assert.Equal(t, []string(nil), result)
|
2019-06-18 23:07:44 +08:00
|
|
|
}
|
|
|
|
|
2019-04-16 10:57:21 +08:00
|
|
|
func TestSetPageLayout(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
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
|
|
|
assert.NoError(t, f.SetPageLayout("Sheet1", nil))
|
|
|
|
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
ws.(*xlsxWorksheet).PageSetUp = nil
|
|
|
|
expected := PageLayoutOptions{
|
|
|
|
Size: intPtr(1),
|
|
|
|
Orientation: stringPtr("landscape"),
|
|
|
|
FirstPageNumber: uintPtr(1),
|
|
|
|
AdjustTo: uintPtr(120),
|
|
|
|
FitToHeight: intPtr(2),
|
|
|
|
FitToWidth: intPtr(2),
|
|
|
|
BlackAndWhite: boolPtr(true),
|
|
|
|
}
|
|
|
|
assert.NoError(t, f.SetPageLayout("Sheet1", &expected))
|
|
|
|
opts, err := f.GetPageLayout("Sheet1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, opts)
|
2019-04-16 10:57:21 +08:00
|
|
|
// Test set page layout on not exists worksheet.
|
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
|
|
|
assert.EqualError(t, f.SetPageLayout("SheetN", nil), "sheet SheetN does not exist")
|
2019-04-16 10:57:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPageLayout(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-04-16 10:57:21 +08:00
|
|
|
// Test get page layout on not exists worksheet.
|
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
|
|
|
_, err := f.GetPageLayout("SheetN")
|
|
|
|
assert.EqualError(t, err, "sheet SheetN does not exist")
|
2019-04-16 10:57:21 +08:00
|
|
|
}
|
2019-05-05 16:25:57 +08:00
|
|
|
|
|
|
|
func TestSetHeaderFooter(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-12-24 01:09:28 +08:00
|
|
|
assert.NoError(t, f.SetCellStr("Sheet1", "A1", "Test SetHeaderFooter"))
|
2019-05-05 16:25:57 +08:00
|
|
|
// Test set header and footer on not exists worksheet.
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.SetHeaderFooter("SheetN", nil), "sheet SheetN does not exist")
|
2019-05-05 16:25:57 +08:00
|
|
|
// Test set header and footer with illegal setting.
|
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
|
|
|
assert.EqualError(t, f.SetHeaderFooter("Sheet1", &HeaderFooterOptions{
|
2021-11-16 00:40:44 +08:00
|
|
|
OddHeader: strings.Repeat("c", MaxFieldLength+1),
|
2021-12-07 00:26:53 +08:00
|
|
|
}), newFieldLengthError("OddHeader").Error())
|
2019-05-05 16:25:57 +08:00
|
|
|
|
|
|
|
assert.NoError(t, f.SetHeaderFooter("Sheet1", nil))
|
2021-11-16 00:40:44 +08:00
|
|
|
text := strings.Repeat("一", MaxFieldLength)
|
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
|
|
|
assert.NoError(t, f.SetHeaderFooter("Sheet1", &HeaderFooterOptions{
|
2021-11-16 00:40:44 +08:00
|
|
|
OddHeader: text,
|
|
|
|
OddFooter: text,
|
|
|
|
EvenHeader: text,
|
|
|
|
EvenFooter: text,
|
|
|
|
FirstHeader: text,
|
|
|
|
}))
|
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
|
|
|
assert.NoError(t, f.SetHeaderFooter("Sheet1", &HeaderFooterOptions{
|
2019-05-05 16:25:57 +08:00
|
|
|
DifferentFirst: true,
|
|
|
|
DifferentOddEven: true,
|
|
|
|
OddHeader: "&R&P",
|
|
|
|
OddFooter: "&C&F",
|
|
|
|
EvenHeader: "&L&P",
|
|
|
|
EvenFooter: "&L&D&R&T",
|
|
|
|
FirstHeader: `&CCenter &"-,Bold"Bold&"-,Regular"HeaderU+000A&D`,
|
|
|
|
}))
|
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetHeaderFooter.xlsx")))
|
|
|
|
}
|
2019-06-18 23:07:44 +08:00
|
|
|
|
|
|
|
func TestDefinedName(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
|
|
|
assert.NoError(t, f.SetDefinedName(&DefinedName{
|
2019-06-18 23:07:44 +08:00
|
|
|
Name: "Amount",
|
|
|
|
RefersTo: "Sheet1!$A$2:$D$5",
|
|
|
|
Comment: "defined name comment",
|
|
|
|
Scope: "Sheet1",
|
|
|
|
}))
|
2020-10-04 21:07:39 +08:00
|
|
|
assert.NoError(t, f.SetDefinedName(&DefinedName{
|
2019-06-18 23:07:44 +08:00
|
|
|
Name: "Amount",
|
|
|
|
RefersTo: "Sheet1!$A$2:$D$5",
|
|
|
|
Comment: "defined name comment",
|
|
|
|
}))
|
2020-10-04 21:07:39 +08:00
|
|
|
assert.EqualError(t, f.SetDefinedName(&DefinedName{
|
2019-06-18 23:07:44 +08:00
|
|
|
Name: "Amount",
|
|
|
|
RefersTo: "Sheet1!$A$2:$D$5",
|
|
|
|
Comment: "defined name comment",
|
2022-03-24 00:19:30 +08:00
|
|
|
}), ErrDefinedNameDuplicate.Error())
|
2020-10-04 21:07:39 +08:00
|
|
|
assert.EqualError(t, f.DeleteDefinedName(&DefinedName{
|
2019-10-26 20:55:24 +08:00
|
|
|
Name: "No Exist Defined Name",
|
2021-12-07 00:26:53 +08:00
|
|
|
}), ErrDefinedNameScope.Error())
|
2019-06-18 23:07:44 +08:00
|
|
|
assert.Exactly(t, "Sheet1!$A$2:$D$5", f.GetDefinedName()[1].RefersTo)
|
2020-10-04 21:07:39 +08:00
|
|
|
assert.NoError(t, f.DeleteDefinedName(&DefinedName{
|
2019-10-26 20:55:24 +08:00
|
|
|
Name: "Amount",
|
|
|
|
}))
|
|
|
|
assert.Exactly(t, "Sheet1!$A$2:$D$5", f.GetDefinedName()[0].RefersTo)
|
|
|
|
assert.Exactly(t, 1, len(f.GetDefinedName()))
|
2019-06-18 23:07:44 +08:00
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestDefinedName.xlsx")))
|
|
|
|
}
|
2019-07-04 16:15:20 +08:00
|
|
|
|
|
|
|
func TestGroupSheets(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-07-04 16:15:20 +08:00
|
|
|
sheets := []string{"Sheet2", "Sheet3"}
|
|
|
|
for _, sheet := range sheets {
|
|
|
|
f.NewSheet(sheet)
|
|
|
|
}
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.GroupSheets([]string{"Sheet1", "SheetN"}), "sheet SheetN does not exist")
|
2019-07-04 16:15:20 +08:00
|
|
|
assert.EqualError(t, f.GroupSheets([]string{"Sheet2", "Sheet3"}), "group worksheet must contain an active worksheet")
|
|
|
|
assert.NoError(t, f.GroupSheets([]string{"Sheet1", "Sheet2"}))
|
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestGroupSheets.xlsx")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUngroupSheets(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-07-04 16:15:20 +08:00
|
|
|
sheets := []string{"Sheet2", "Sheet3", "Sheet4", "Sheet5"}
|
|
|
|
for _, sheet := range sheets {
|
|
|
|
f.NewSheet(sheet)
|
|
|
|
}
|
|
|
|
assert.NoError(t, f.UngroupSheets())
|
|
|
|
}
|
2019-08-07 21:53:37 +08:00
|
|
|
|
2020-03-01 00:34:41 +08:00
|
|
|
func TestInsertPageBreak(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2020-03-01 00:34:41 +08:00
|
|
|
assert.NoError(t, f.InsertPageBreak("Sheet1", "A1"))
|
|
|
|
assert.NoError(t, f.InsertPageBreak("Sheet1", "B2"))
|
|
|
|
assert.NoError(t, f.InsertPageBreak("Sheet1", "C3"))
|
|
|
|
assert.NoError(t, f.InsertPageBreak("Sheet1", "C3"))
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, f.InsertPageBreak("Sheet1", "A"), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.InsertPageBreak("SheetN", "C3"), "sheet SheetN does not exist")
|
2020-03-01 00:34:41 +08:00
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestInsertPageBreak.xlsx")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemovePageBreak(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2020-03-01 00:34:41 +08:00
|
|
|
assert.NoError(t, f.RemovePageBreak("Sheet1", "A2"))
|
|
|
|
|
|
|
|
assert.NoError(t, f.InsertPageBreak("Sheet1", "A2"))
|
|
|
|
assert.NoError(t, f.InsertPageBreak("Sheet1", "B2"))
|
|
|
|
assert.NoError(t, f.RemovePageBreak("Sheet1", "A1"))
|
|
|
|
assert.NoError(t, f.RemovePageBreak("Sheet1", "B2"))
|
|
|
|
|
|
|
|
assert.NoError(t, f.InsertPageBreak("Sheet1", "C3"))
|
|
|
|
assert.NoError(t, f.RemovePageBreak("Sheet1", "C3"))
|
|
|
|
|
|
|
|
assert.NoError(t, f.InsertPageBreak("Sheet1", "A3"))
|
|
|
|
assert.NoError(t, f.RemovePageBreak("Sheet1", "B3"))
|
|
|
|
assert.NoError(t, f.RemovePageBreak("Sheet1", "A3"))
|
|
|
|
|
|
|
|
f.NewSheet("Sheet2")
|
|
|
|
assert.NoError(t, f.InsertPageBreak("Sheet2", "B2"))
|
|
|
|
assert.NoError(t, f.InsertPageBreak("Sheet2", "C2"))
|
|
|
|
assert.NoError(t, f.RemovePageBreak("Sheet2", "B2"))
|
|
|
|
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, f.RemovePageBreak("Sheet1", "A"), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.RemovePageBreak("SheetN", "C3"), "sheet SheetN does not exist")
|
2020-03-01 00:34:41 +08:00
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRemovePageBreak.xlsx")))
|
|
|
|
}
|
|
|
|
|
2019-08-07 21:53:37 +08:00
|
|
|
func TestGetSheetName(t *testing.T) {
|
2020-10-18 00:01:33 +08:00
|
|
|
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
assert.NoError(t, err)
|
2020-04-23 02:01:14 +08:00
|
|
|
assert.Equal(t, "Sheet1", f.GetSheetName(0))
|
|
|
|
assert.Equal(t, "Sheet2", f.GetSheetName(1))
|
|
|
|
assert.Equal(t, "", f.GetSheetName(-1))
|
|
|
|
assert.Equal(t, "", f.GetSheetName(2))
|
2021-09-18 23:20:24 +08:00
|
|
|
assert.NoError(t, f.Close())
|
2019-08-07 21:53:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSheetMap(t *testing.T) {
|
|
|
|
expectedMap := map[int]string{
|
|
|
|
1: "Sheet1",
|
|
|
|
2: "Sheet2",
|
|
|
|
}
|
2020-10-18 00:01:33 +08:00
|
|
|
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
assert.NoError(t, err)
|
2019-08-07 21:53:37 +08:00
|
|
|
sheetMap := f.GetSheetMap()
|
|
|
|
for idx, name := range sheetMap {
|
|
|
|
assert.Equal(t, expectedMap[idx], name)
|
|
|
|
}
|
|
|
|
assert.Equal(t, len(sheetMap), 2)
|
2021-09-18 23:20:24 +08:00
|
|
|
assert.NoError(t, f.Close())
|
2019-08-07 21:53:37 +08:00
|
|
|
}
|
2020-10-18 00:01:33 +08:00
|
|
|
|
|
|
|
func TestSetActiveSheet(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
f.WorkBook.BookViews = nil
|
|
|
|
f.SetActiveSheet(1)
|
|
|
|
f.WorkBook.BookViews = &xlsxBookViews{WorkBookView: []xlsxWorkBookView{}}
|
2021-07-05 00:03:56 +08:00
|
|
|
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
ws.(*xlsxWorksheet).SheetViews = &xlsxSheetViews{SheetView: []xlsxSheetView{}}
|
2020-10-18 00:01:33 +08:00
|
|
|
f.SetActiveSheet(1)
|
2021-07-05 00:03:56 +08:00
|
|
|
ws, ok = f.Sheet.Load("xl/worksheets/sheet1.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
ws.(*xlsxWorksheet).SheetViews = nil
|
2020-10-18 00:01:33 +08:00
|
|
|
f.SetActiveSheet(1)
|
|
|
|
f = NewFile()
|
|
|
|
f.SetActiveSheet(-1)
|
|
|
|
assert.Equal(t, f.GetActiveSheetIndex(), 0)
|
2021-12-03 00:19:11 +08:00
|
|
|
|
|
|
|
f = NewFile()
|
|
|
|
f.WorkBook.BookViews = nil
|
|
|
|
idx := f.NewSheet("Sheet2")
|
|
|
|
ws, ok = f.Sheet.Load("xl/worksheets/sheet2.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
ws.(*xlsxWorksheet).SheetViews = &xlsxSheetViews{SheetView: []xlsxSheetView{}}
|
|
|
|
f.SetActiveSheet(idx)
|
2020-10-18 00:01:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetSheetName(t *testing.T) {
|
|
|
|
f := NewFile()
|
2022-01-09 00:20:42 +08:00
|
|
|
// Test set worksheet with the same name.
|
2020-10-18 00:01:33 +08:00
|
|
|
f.SetSheetName("Sheet1", "Sheet1")
|
|
|
|
assert.Equal(t, "Sheet1", f.GetSheetName(0))
|
|
|
|
}
|
2020-11-03 17:48:37 +08:00
|
|
|
|
2022-03-05 14:48:34 +08:00
|
|
|
func TestWorksheetWriter(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
// Test set cell value with alternate content
|
|
|
|
f.Sheet.Delete("xl/worksheets/sheet1.xml")
|
|
|
|
worksheet := xml.Header + `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheetData><row r="1"><c r="A1"><v>%d</v></c></row></sheetData><mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"><mc:Choice xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" Requires="a14"><xdr:twoCellAnchor editAs="oneCell"></xdr:twoCellAnchor></mc:Choice><mc:Fallback/></mc:AlternateContent></worksheet>`
|
|
|
|
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(fmt.Sprintf(worksheet, 1)))
|
|
|
|
f.checked = nil
|
|
|
|
assert.NoError(t, f.SetCellValue("Sheet1", "A1", 2))
|
|
|
|
f.workSheetWriter()
|
|
|
|
value, ok := f.Pkg.Load("xl/worksheets/sheet1.xml")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, fmt.Sprintf(worksheet, 2), string(value.([]byte)))
|
|
|
|
}
|
|
|
|
|
2020-11-06 20:03:13 +08:00
|
|
|
func TestGetWorkbookPath(t *testing.T) {
|
|
|
|
f := NewFile()
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Delete("_rels/.rels")
|
2020-11-06 20:03:13 +08:00
|
|
|
assert.Equal(t, "", f.getWorkbookPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWorkbookRelsPath(t *testing.T) {
|
|
|
|
f := NewFile()
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Delete("xl/_rels/.rels")
|
2022-03-05 14:48:34 +08:00
|
|
|
f.Pkg.Store("_rels/.rels", []byte(xml.Header+`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument" Target="/workbook.xml"/></Relationships>`))
|
2020-11-06 20:03:13 +08:00
|
|
|
assert.Equal(t, "_rels/workbook.xml.rels", f.getWorkbookRelsPath())
|
|
|
|
}
|
|
|
|
|
2020-11-23 00:01:06 +08:00
|
|
|
func TestDeleteSheet(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
f.SetActiveSheet(f.NewSheet("Sheet2"))
|
|
|
|
f.NewSheet("Sheet3")
|
|
|
|
f.DeleteSheet("Sheet1")
|
|
|
|
assert.Equal(t, "Sheet2", f.GetSheetName(f.GetActiveSheetIndex()))
|
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestDeleteSheet.xlsx")))
|
2021-02-02 22:23:16 +08:00
|
|
|
// Test with auto filter defined names
|
|
|
|
f = NewFile()
|
|
|
|
f.NewSheet("Sheet2")
|
|
|
|
f.NewSheet("Sheet3")
|
|
|
|
assert.NoError(t, f.SetCellValue("Sheet1", "A1", "A"))
|
|
|
|
assert.NoError(t, f.SetCellValue("Sheet2", "A1", "A"))
|
|
|
|
assert.NoError(t, f.SetCellValue("Sheet3", "A1", "A"))
|
|
|
|
assert.NoError(t, f.AutoFilter("Sheet1", "A1", "A1", ""))
|
|
|
|
assert.NoError(t, f.AutoFilter("Sheet2", "A1", "A1", ""))
|
|
|
|
assert.NoError(t, f.AutoFilter("Sheet3", "A1", "A1", ""))
|
|
|
|
f.DeleteSheet("Sheet2")
|
|
|
|
f.DeleteSheet("Sheet1")
|
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestDeleteSheet2.xlsx")))
|
2020-11-23 00:01:06 +08:00
|
|
|
}
|
|
|
|
|
2021-12-03 00:19:11 +08:00
|
|
|
func TestDeleteAndAdjustDefinedNames(t *testing.T) {
|
|
|
|
deleteAndAdjustDefinedNames(nil, 0)
|
|
|
|
deleteAndAdjustDefinedNames(&xlsxWorkbook{}, 0)
|
|
|
|
}
|
|
|
|
|
2022-07-06 20:39:10 +08:00
|
|
|
func TestGetSheetID(t *testing.T) {
|
|
|
|
file := NewFile()
|
|
|
|
file.NewSheet("Sheet1")
|
|
|
|
id := file.getSheetID("sheet1")
|
|
|
|
assert.NotEqual(t, -1, id)
|
|
|
|
}
|
|
|
|
|
2020-11-03 17:48:37 +08:00
|
|
|
func BenchmarkNewSheet(b *testing.B) {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
newSheetWithSet()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-01-23 00:32:34 +08:00
|
|
|
|
2020-11-03 17:48:37 +08:00
|
|
|
func newSheetWithSet() {
|
|
|
|
file := NewFile()
|
|
|
|
file.NewSheet("sheet1")
|
|
|
|
for i := 0; i < 1000; i++ {
|
2021-02-15 00:09:35 +08:00
|
|
|
_ = file.SetCellInt("sheet1", "A"+strconv.Itoa(i+1), i)
|
2020-11-03 17:48:37 +08:00
|
|
|
}
|
|
|
|
file = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFile_SaveAs(b *testing.B) {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
newSheetWithSave()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-11-23 00:01:06 +08:00
|
|
|
|
2020-11-03 17:48:37 +08:00
|
|
|
func newSheetWithSave() {
|
|
|
|
file := NewFile()
|
|
|
|
file.NewSheet("sheet1")
|
|
|
|
for i := 0; i < 1000; i++ {
|
2021-02-15 00:09:35 +08:00
|
|
|
_ = file.SetCellInt("sheet1", "A"+strconv.Itoa(i+1), i)
|
2020-11-03 17:48:37 +08:00
|
|
|
}
|
2021-02-15 00:09:35 +08:00
|
|
|
_ = file.Save()
|
2020-11-03 17:48:37 +08:00
|
|
|
}
|
2022-08-11 00:20:48 +08:00
|
|
|
|
|
|
|
func TestAttrValToBool(t *testing.T) {
|
|
|
|
_, err := attrValToBool("hidden", []xml.Attr{
|
|
|
|
{Name: xml.Name{Local: "hidden"}},
|
|
|
|
})
|
|
|
|
assert.EqualError(t, err, `strconv.ParseBool: parsing "": invalid syntax`)
|
|
|
|
|
|
|
|
got, err := attrValToBool("hidden", []xml.Attr{
|
|
|
|
{Name: xml.Name{Local: "hidden"}, Value: "1"},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttrValToFloat(t *testing.T) {
|
|
|
|
_, err := attrValToFloat("ht", []xml.Attr{
|
|
|
|
{Name: xml.Name{Local: "ht"}},
|
|
|
|
})
|
|
|
|
assert.EqualError(t, err, `strconv.ParseFloat: parsing "": invalid syntax`)
|
|
|
|
|
|
|
|
got, err := attrValToFloat("ht", []xml.Attr{
|
|
|
|
{Name: xml.Name{Local: "ht"}, Value: "42.1"},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 42.1, got)
|
|
|
|
}
|