2020-10-04 21:07:39 +08:00
|
|
|
package excelize
|
2019-01-06 14:12:31 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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/mohae/deepcopy"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleFile_SetPageLayout() {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-05-05 16:25:57 +08:00
|
|
|
if err := f.SetPageLayout(
|
2019-01-06 14:12:31 +08:00
|
|
|
"Sheet1",
|
2021-01-17 01:06:08 +08:00
|
|
|
BlackAndWhite(true),
|
|
|
|
FirstPageNumber(2),
|
2020-10-04 21:07:39 +08:00
|
|
|
PageLayoutOrientation(OrientationLandscape),
|
|
|
|
PageLayoutPaperSize(10),
|
|
|
|
FitToHeight(2),
|
|
|
|
FitToWidth(2),
|
2021-01-16 21:51:23 +08:00
|
|
|
PageLayoutScale(50),
|
2019-01-13 21:58:50 +08:00
|
|
|
); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2019-01-13 21:58:50 +08:00
|
|
|
}
|
2019-01-06 14:12:31 +08:00
|
|
|
// Output:
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleFile_GetPageLayout() {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-01-06 14:12:31 +08:00
|
|
|
var (
|
2021-01-17 01:06:08 +08:00
|
|
|
blackAndWhite BlackAndWhite
|
|
|
|
firstPageNumber FirstPageNumber
|
|
|
|
orientation PageLayoutOrientation
|
|
|
|
paperSize PageLayoutPaperSize
|
|
|
|
fitToHeight FitToHeight
|
|
|
|
fitToWidth FitToWidth
|
|
|
|
scale PageLayoutScale
|
2019-01-06 14:12:31 +08:00
|
|
|
)
|
2021-01-17 01:06:08 +08:00
|
|
|
if err := f.GetPageLayout("Sheet1", &blackAndWhite); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
if err := f.GetPageLayout("Sheet1", &firstPageNumber); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2019-05-05 16:25:57 +08:00
|
|
|
if err := f.GetPageLayout("Sheet1", &orientation); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2019-01-06 14:12:31 +08:00
|
|
|
}
|
2019-05-05 16:25:57 +08:00
|
|
|
if err := f.GetPageLayout("Sheet1", &paperSize); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2019-01-13 21:58:50 +08:00
|
|
|
}
|
2019-07-07 00:17:15 +08:00
|
|
|
if err := f.GetPageLayout("Sheet1", &fitToHeight); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2019-07-07 00:17:15 +08:00
|
|
|
}
|
|
|
|
if err := f.GetPageLayout("Sheet1", &fitToWidth); err != nil {
|
2020-02-19 00:08:10 +08:00
|
|
|
fmt.Println(err)
|
2019-07-07 00:17:15 +08:00
|
|
|
}
|
2021-01-16 21:51:23 +08:00
|
|
|
if err := f.GetPageLayout("Sheet1", &scale); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2019-01-06 14:12:31 +08:00
|
|
|
fmt.Println("Defaults:")
|
2021-01-17 01:06:08 +08:00
|
|
|
fmt.Printf("- print black and white: %t\n", blackAndWhite)
|
|
|
|
fmt.Printf("- page number for first printed page: %d\n", firstPageNumber)
|
2019-01-06 14:12:31 +08:00
|
|
|
fmt.Printf("- orientation: %q\n", orientation)
|
2019-01-13 21:58:50 +08:00
|
|
|
fmt.Printf("- paper size: %d\n", paperSize)
|
2019-07-07 00:17:15 +08:00
|
|
|
fmt.Printf("- fit to height: %d\n", fitToHeight)
|
|
|
|
fmt.Printf("- fit to width: %d\n", fitToWidth)
|
2021-01-16 21:51:23 +08:00
|
|
|
fmt.Printf("- scale: %d\n", scale)
|
2019-01-06 14:12:31 +08:00
|
|
|
// Output:
|
|
|
|
// Defaults:
|
2021-01-17 01:06:08 +08:00
|
|
|
// - print black and white: false
|
|
|
|
// - page number for first printed page: 1
|
2019-01-06 14:12:31 +08:00
|
|
|
// - orientation: "portrait"
|
2019-01-13 21:58:50 +08:00
|
|
|
// - paper size: 1
|
2019-07-07 00:17:15 +08:00
|
|
|
// - fit to height: 1
|
|
|
|
// - fit to width: 1
|
2021-01-16 21:51:23 +08:00
|
|
|
// - scale: 100
|
2019-01-06 14:12:31 +08:00
|
|
|
}
|
|
|
|
|
2019-10-24 23:18:02 +08:00
|
|
|
func TestNewSheet(t *testing.T) {
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-10-24 23:18:02 +08:00
|
|
|
sheetID := f.NewSheet("Sheet2")
|
|
|
|
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"))
|
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"}]}`))
|
|
|
|
assert.NoError(t, f.SetPanes("Panes 4", ""))
|
|
|
|
assert.EqualError(t, f.SetPanes("SheetN", ""), "sheet SheetN is not exist")
|
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetPane.xlsx")))
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:12:31 +08:00
|
|
|
func TestPageLayoutOption(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
|
|
|
|
testData := []struct {
|
2020-10-04 21:07:39 +08:00
|
|
|
container PageLayoutOptionPtr
|
|
|
|
nonDefault PageLayoutOption
|
2019-01-06 14:12:31 +08:00
|
|
|
}{
|
2021-01-17 01:06:08 +08:00
|
|
|
{new(BlackAndWhite), BlackAndWhite(true)},
|
|
|
|
{new(FirstPageNumber), FirstPageNumber(2)},
|
2020-10-04 21:07:39 +08:00
|
|
|
{new(PageLayoutOrientation), PageLayoutOrientation(OrientationLandscape)},
|
|
|
|
{new(PageLayoutPaperSize), PageLayoutPaperSize(10)},
|
|
|
|
{new(FitToHeight), FitToHeight(2)},
|
|
|
|
{new(FitToWidth), FitToWidth(2)},
|
2021-01-16 21:51:23 +08:00
|
|
|
{new(PageLayoutScale), PageLayoutScale(50)},
|
2019-01-06 14:12:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testData {
|
|
|
|
t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
|
|
|
|
|
|
|
|
opt := test.nonDefault
|
|
|
|
t.Logf("option %T", opt)
|
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
def := deepcopy.Copy(test.container).(PageLayoutOptionPtr)
|
|
|
|
val1 := deepcopy.Copy(def).(PageLayoutOptionPtr)
|
|
|
|
val2 := deepcopy.Copy(def).(PageLayoutOptionPtr)
|
2019-01-06 14:12:31 +08:00
|
|
|
|
2020-10-04 21:07:39 +08:00
|
|
|
f := NewFile()
|
2019-01-06 14:12:31 +08:00
|
|
|
// Get the default value
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, def), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
// Get again and check
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, val1), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
if !assert.Equal(t, val1, def, opt) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set the same value
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.SetPageLayout(sheet, val1), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
// Get again and check
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, val1), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Set a different value
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.SetPageLayout(sheet, test.nonDefault), opt)
|
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, val1), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
// Get again and compare
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, val2), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Value should not be the same as the default
|
|
|
|
if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Restore the default value
|
2019-05-05 16:25:57 +08:00
|
|
|
assert.NoError(t, f.SetPageLayout(sheet, def), opt)
|
|
|
|
assert.NoError(t, f.GetPageLayout(sheet, val1), opt)
|
2019-01-06 14:12:31 +08:00
|
|
|
if !assert.Equal(t, def, val1) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 10:57:21 +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", "")
|
|
|
|
assert.EqualError(t, err, "sheet Sheet4 is not exist")
|
|
|
|
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)
|
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)
|
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()
|
2019-04-16 10:57:21 +08:00
|
|
|
// Test set page layout on not exists worksheet.
|
|
|
|
assert.EqualError(t, f.SetPageLayout("SheetN"), "sheet SheetN is not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
assert.EqualError(t, f.GetPageLayout("SheetN"), "sheet SheetN is not exist")
|
|
|
|
}
|
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.
|
|
|
|
assert.EqualError(t, f.SetHeaderFooter("SheetN", nil), "sheet SheetN is not exist")
|
|
|
|
// Test set header and footer with illegal setting.
|
2020-10-04 21:07:39 +08:00
|
|
|
assert.EqualError(t, f.SetHeaderFooter("Sheet1", &FormatHeaderFooter{
|
2019-05-05 16:25:57 +08:00
|
|
|
OddHeader: strings.Repeat("c", 256),
|
|
|
|
}), "field OddHeader must be less than 255 characters")
|
|
|
|
|
|
|
|
assert.NoError(t, f.SetHeaderFooter("Sheet1", nil))
|
2020-10-04 21:07:39 +08:00
|
|
|
assert.NoError(t, f.SetHeaderFooter("Sheet1", &FormatHeaderFooter{
|
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",
|
2019-10-26 20:55:24 +08:00
|
|
|
}), "the same name already exists on the scope")
|
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",
|
|
|
|
}), "no defined name on the scope")
|
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)
|
|
|
|
}
|
|
|
|
assert.EqualError(t, f.GroupSheets([]string{"Sheet1", "SheetN"}), "sheet SheetN is not exist")
|
|
|
|
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"))
|
|
|
|
assert.EqualError(t, f.InsertPageBreak("Sheet1", "A"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
|
|
|
assert.EqualError(t, f.InsertPageBreak("SheetN", "C3"), "sheet SheetN is not exist")
|
|
|
|
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"))
|
|
|
|
|
|
|
|
assert.EqualError(t, f.RemovePageBreak("Sheet1", "A"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
|
|
|
assert.EqualError(t, f.RemovePageBreak("SheetN", "C3"), "sheet SheetN is not exist")
|
|
|
|
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))
|
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)
|
|
|
|
}
|
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{}}
|
|
|
|
f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = &xlsxSheetViews{SheetView: []xlsxSheetView{}}
|
|
|
|
f.SetActiveSheet(1)
|
|
|
|
f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = nil
|
|
|
|
f.SetActiveSheet(1)
|
|
|
|
f = NewFile()
|
|
|
|
f.SetActiveSheet(-1)
|
|
|
|
assert.Equal(t, f.GetActiveSheetIndex(), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetSheetName(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
// Test set workksheet with the same name.
|
|
|
|
f.SetSheetName("Sheet1", "Sheet1")
|
|
|
|
assert.Equal(t, "Sheet1", f.GetSheetName(0))
|
|
|
|
}
|
2020-11-03 17:48:37 +08:00
|
|
|
|
2020-11-06 20:03:13 +08:00
|
|
|
func TestGetWorkbookPath(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
delete(f.XLSX, "_rels/.rels")
|
|
|
|
assert.Equal(t, "", f.getWorkbookPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWorkbookRelsPath(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
delete(f.XLSX, "xl/_rels/.rels")
|
|
|
|
f.XLSX["_rels/.rels"] = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><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>`)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-03 17:48:37 +08:00
|
|
|
func BenchmarkNewSheet(b *testing.B) {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
newSheetWithSet()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
func newSheetWithSet() {
|
|
|
|
file := NewFile()
|
|
|
|
file.NewSheet("sheet1")
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
file.SetCellInt("sheet1", "A"+strconv.Itoa(i+1), i)
|
|
|
|
}
|
|
|
|
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++ {
|
|
|
|
file.SetCellInt("sheet1", "A"+strconv.Itoa(i+1), i)
|
|
|
|
}
|
|
|
|
file.Save()
|
|
|
|
}
|