From ac91ca0ded4111ed9f22578d4a0570a9084c97b0 Mon Sep 17 00:00:00 2001 From: Harris Date: Sun, 4 Aug 2019 17:23:42 -0500 Subject: Only parse xml once when reading We were parsing the whole sheet twice since the sheet reader already reads in all the rows. getTotalRowsCols function is unused after these changes so it has been deleted as well. Closes #439 --- rows_test.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index f7d49b4..d52c635 100644 --- a/rows_test.go +++ b/rows_test.go @@ -39,9 +39,6 @@ func TestRows(t *testing.T) { if !assert.Equal(t, collectedRows, returnedRows) { t.FailNow() } - - r := Rows{} - r.Columns() } func TestRowsError(t *testing.T) { @@ -672,6 +669,21 @@ func TestDuplicateRowInvalidRownum(t *testing.T) { } } +func BenchmarkRows(b *testing.B) { + for i := 0; i < b.N; i++ { + f, _ := OpenFile(filepath.Join("test", "Book1.xlsx")) + rows, _ := f.Rows("Sheet2") + for rows.Next() { + row, _ := rows.Columns() + for i := range row { + if i >= 0 { + continue + } + } + } + } +} + func trimSliceSpace(s []string) []string { for { if len(s) > 0 && s[len(s)-1] == "" { -- cgit v1.2.1 From 2e791fa433def282ee2e7a5049a46fc4a76796cf Mon Sep 17 00:00:00 2001 From: xuri Date: Wed, 16 Oct 2019 01:03:29 +0800 Subject: Optimize code of Getting/Setting Page Margins --- rows_test.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index d52c635..a99a594 100644 --- a/rows_test.go +++ b/rows_test.go @@ -669,6 +669,11 @@ func TestDuplicateRowInvalidRownum(t *testing.T) { } } +func TestErrSheetNotExistError(t *testing.T) { + err := ErrSheetNotExist{SheetName: "Sheet1"} + assert.EqualValues(t, err.Error(), "Sheet Sheet1 is not exist") +} + func BenchmarkRows(b *testing.B) { for i := 0; i < b.N; i++ { f, _ := OpenFile(filepath.Join("test", "Book1.xlsx")) -- cgit v1.2.1 From 866fda230028a3a9e6ff1c5234e432ad850d3c6b Mon Sep 17 00:00:00 2001 From: ducquangkstn Date: Fri, 18 Oct 2019 13:57:35 +0700 Subject: fix #503 rows next issue --- rows_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index a99a594..ba81f9f 100644 --- a/rows_test.go +++ b/rows_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestRows(t *testing.T) { @@ -41,6 +42,25 @@ func TestRows(t *testing.T) { } } +// test bug https://github.com/360EntSecGroup-Skylar/excelize/issues/502 +func TestRowsIterator(t *testing.T) { + const ( + sheet2 = "Sheet2" + expectedNumRow = 11 + ) + xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx")) + require.NoError(t, err) + + rows, err := xlsx.Rows(sheet2) + require.NoError(t, err) + var rowCount int + for rows.Next() { + rowCount++ + require.True(t, rowCount <= expectedNumRow, "rowCount is greater than expected") + } + assert.Equal(t, expectedNumRow, rowCount) +} + func TestRowsError(t *testing.T) { xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx")) if !assert.NoError(t, err) { -- cgit v1.2.1 From 9fe267ffcfa06545223160cdb8c35cd91163730e Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 24 Oct 2019 09:14:33 -0500 Subject: Pre-allocate some memory when reading files (#510) --- rows_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index ba81f9f..f0fbe03 100644 --- a/rows_test.go +++ b/rows_test.go @@ -695,8 +695,8 @@ func TestErrSheetNotExistError(t *testing.T) { } func BenchmarkRows(b *testing.B) { + f, _ := OpenFile(filepath.Join("test", "Book1.xlsx")) for i := 0; i < b.N; i++ { - f, _ := OpenFile(filepath.Join("test", "Book1.xlsx")) rows, _ := f.Rows("Sheet2") for rows.Next() { row, _ := rows.Columns() -- cgit v1.2.1 From 87390cdd99b3afbe07daeef9abe96f57d03cb352 Mon Sep 17 00:00:00 2001 From: xuri Date: Thu, 24 Oct 2019 23:18:02 +0800 Subject: Resolve #511, allow empty columns in the pivot table --- rows_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index f0fbe03..a443e89 100644 --- a/rows_test.go +++ b/rows_test.go @@ -22,7 +22,7 @@ func TestRows(t *testing.T) { t.FailNow() } - collectedRows := make([][]string, 0) + var collectedRows [][]string for rows.Next() { columns, err := rows.Columns() assert.NoError(t, err) -- cgit v1.2.1 From 5e418ebd665f38d1211b27d7157ec7e5868451bc Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 26 Oct 2019 20:55:24 +0800 Subject: Resolve #507, add the new function `DeleteDefinedName` --- rows_test.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index a443e89..ff70118 100644 --- a/rows_test.go +++ b/rows_test.go @@ -42,7 +42,6 @@ func TestRows(t *testing.T) { } } -// test bug https://github.com/360EntSecGroup-Skylar/excelize/issues/502 func TestRowsIterator(t *testing.T) { const ( sheet2 = "Sheet2" @@ -59,6 +58,10 @@ func TestRowsIterator(t *testing.T) { require.True(t, rowCount <= expectedNumRow, "rowCount is greater than expected") } assert.Equal(t, expectedNumRow, rowCount) + + rows = &Rows{f: xlsx, rows: []xlsxRow{{C: []xlsxC{{R: "A"}}}}, curRow: 1} + _, err = rows.Columns() + assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`) } func TestRowsError(t *testing.T) { @@ -113,22 +116,25 @@ func TestRowHeight(t *testing.T) { } func TestRowVisibility(t *testing.T) { - xlsx, err := prepareTestBook1() + f, err := prepareTestBook1() if !assert.NoError(t, err) { t.FailNow() } - xlsx.NewSheet("Sheet3") - assert.NoError(t, xlsx.SetRowVisible("Sheet3", 2, false)) - assert.NoError(t, xlsx.SetRowVisible("Sheet3", 2, true)) - xlsx.GetRowVisible("Sheet3", 2) - xlsx.GetRowVisible("Sheet3", 25) - assert.EqualError(t, xlsx.SetRowVisible("Sheet3", 0, true), "invalid row number 0") + f.NewSheet("Sheet3") + assert.NoError(t, f.SetRowVisible("Sheet3", 2, false)) + assert.NoError(t, f.SetRowVisible("Sheet3", 2, true)) + f.GetRowVisible("Sheet3", 2) + f.GetRowVisible("Sheet3", 25) + assert.EqualError(t, f.SetRowVisible("Sheet3", 0, true), "invalid row number 0") + assert.EqualError(t, f.SetRowVisible("SheetN", 2, false), "sheet SheetN is not exist") - visible, err := xlsx.GetRowVisible("Sheet3", 0) + visible, err := f.GetRowVisible("Sheet3", 0) assert.Equal(t, false, visible) assert.EqualError(t, err, "invalid row number 0") + _, err = f.GetRowVisible("SheetN", 1) + assert.EqualError(t, err, "sheet SheetN is not exist") - assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestRowVisibility.xlsx"))) + assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRowVisibility.xlsx"))) } func TestRemoveRow(t *testing.T) { -- cgit v1.2.1 From 7965e1231b736f8507f93f6383b76332eb15ff5f Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 23 Nov 2019 04:13:59 +0800 Subject: Resolve #146, make the GetRow function read data as streaming. Ref: #382, #515 --- rows_test.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index ff70118..47c9d96 100644 --- a/rows_test.go +++ b/rows_test.go @@ -47,10 +47,10 @@ func TestRowsIterator(t *testing.T) { sheet2 = "Sheet2" expectedNumRow = 11 ) - xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx")) + f, err := OpenFile(filepath.Join("test", "Book1.xlsx")) require.NoError(t, err) - rows, err := xlsx.Rows(sheet2) + rows, err := f.Rows(sheet2) require.NoError(t, err) var rowCount int for rows.Next() { @@ -59,9 +59,20 @@ func TestRowsIterator(t *testing.T) { } assert.Equal(t, expectedNumRow, rowCount) - rows = &Rows{f: xlsx, rows: []xlsxRow{{C: []xlsxC{{R: "A"}}}}, curRow: 1} - _, err = rows.Columns() - assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`) + // Valued cell sparse distribution test + f = NewFile() + cells := []string{"C1", "E1", "A3", "B3", "C3", "D3", "E3"} + for _, cell := range cells { + f.SetCellValue("Sheet1", cell, 1) + } + rows, err = f.Rows("Sheet1") + require.NoError(t, err) + rowCount = 0 + for rows.Next() { + rowCount++ + require.True(t, rowCount <= 3, "rowCount is greater than expected") + } + assert.Equal(t, 3, rowCount) } func TestRowsError(t *testing.T) { @@ -697,7 +708,7 @@ func TestDuplicateRowInvalidRownum(t *testing.T) { func TestErrSheetNotExistError(t *testing.T) { err := ErrSheetNotExist{SheetName: "Sheet1"} - assert.EqualValues(t, err.Error(), "Sheet Sheet1 is not exist") + assert.EqualValues(t, err.Error(), "sheet Sheet1 is not exist") } func BenchmarkRows(b *testing.B) { -- cgit v1.2.1 From 5d8365ca17240f5b144d437a7b47052f22c4f3c6 Mon Sep 17 00:00:00 2001 From: xuri Date: Wed, 11 Dec 2019 00:02:33 +0800 Subject: Fix #529, handle empty inline rich text --- rows_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index 47c9d96..6b50c75 100644 --- a/rows_test.go +++ b/rows_test.go @@ -706,6 +706,15 @@ func TestDuplicateRowInvalidRownum(t *testing.T) { } } +func TestGetValueFrom(t *testing.T) { + c := &xlsxC{T: "inlineStr"} + f := NewFile() + d := &xlsxSST{} + val, err := c.getValueFrom(f, d) + assert.NoError(t, err) + assert.Equal(t, "", val) +} + func TestErrSheetNotExistError(t *testing.T) { err := ErrSheetNotExist{SheetName: "Sheet1"} assert.EqualValues(t, err.Error(), "sheet Sheet1 is not exist") -- cgit v1.2.1 From ae2865d9237cfd27d7bc4fbef3870b3361597be8 Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 22 Dec 2019 00:02:09 +0800 Subject: Improve code coverage unit tests --- rows_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 20 deletions(-) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index 6b50c75..6494242 100644 --- a/rows_test.go +++ b/rows_test.go @@ -1,6 +1,7 @@ package excelize import ( + "bytes" "fmt" "path/filepath" "testing" @@ -12,12 +13,12 @@ import ( func TestRows(t *testing.T) { const sheet2 = "Sheet2" - xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx")) + f, err := OpenFile(filepath.Join("test", "Book1.xlsx")) if !assert.NoError(t, err) { t.FailNow() } - rows, err := xlsx.Rows(sheet2) + rows, err := f.Rows(sheet2) if !assert.NoError(t, err) { t.FailNow() } @@ -32,7 +33,7 @@ func TestRows(t *testing.T) { t.FailNow() } - returnedRows, err := xlsx.GetRows(sheet2) + returnedRows, err := f.GetRows(sheet2) assert.NoError(t, err) for i := range returnedRows { returnedRows[i] = trimSliceSpace(returnedRows[i]) @@ -40,6 +41,11 @@ func TestRows(t *testing.T) { if !assert.Equal(t, collectedRows, returnedRows) { t.FailNow() } + + f = NewFile() + f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`1B`) + _, err = f.Rows("Sheet1") + assert.EqualError(t, err, `strconv.Atoi: parsing "A": invalid syntax`) } func TestRowsIterator(t *testing.T) { @@ -126,6 +132,35 @@ func TestRowHeight(t *testing.T) { convertColWidthToPixels(0) } +func TestColumns(t *testing.T) { + f := NewFile() + rows, err := f.Rows("Sheet1") + assert.NoError(t, err) + rows.decoder = f.xmlNewDecoder(bytes.NewReader([]byte(`1B`))) + _, err = rows.Columns() + assert.EqualError(t, err, `strconv.Atoi: parsing "A": invalid syntax`) + + rows.decoder = f.xmlNewDecoder(bytes.NewReader([]byte(`1B`))) + _, err = rows.Columns() + assert.NoError(t, err) + + rows.curRow = 3 + rows.decoder = f.xmlNewDecoder(bytes.NewReader([]byte(`1`))) + _, err = rows.Columns() + assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`) + + // Test token is nil + rows.decoder = f.xmlNewDecoder(bytes.NewReader(nil)) + _, err = rows.Columns() + assert.NoError(t, err) +} + +func TestSharedStringsReader(t *testing.T) { + f := NewFile() + f.XLSX["xl/sharedStrings.xml"] = MacintoshCyrillicCharset + f.sharedStringsReader() +} + func TestRowVisibility(t *testing.T) { f, err := prepareTestBook1() if !assert.NoError(t, err) { @@ -149,61 +184,64 @@ func TestRowVisibility(t *testing.T) { } func TestRemoveRow(t *testing.T) { - xlsx := NewFile() - sheet1 := xlsx.GetSheetName(1) - r, err := xlsx.workSheetReader(sheet1) + f := NewFile() + sheet1 := f.GetSheetName(1) + r, err := f.workSheetReader(sheet1) assert.NoError(t, err) const ( colCount = 10 rowCount = 10 ) - fillCells(xlsx, sheet1, colCount, rowCount) + fillCells(f, sheet1, colCount, rowCount) - xlsx.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External") + f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External") - assert.EqualError(t, xlsx.RemoveRow(sheet1, -1), "invalid row number -1") + assert.EqualError(t, f.RemoveRow(sheet1, -1), "invalid row number -1") - assert.EqualError(t, xlsx.RemoveRow(sheet1, 0), "invalid row number 0") + assert.EqualError(t, f.RemoveRow(sheet1, 0), "invalid row number 0") - assert.NoError(t, xlsx.RemoveRow(sheet1, 4)) + assert.NoError(t, f.RemoveRow(sheet1, 4)) if !assert.Len(t, r.SheetData.Row, rowCount-1) { t.FailNow() } - xlsx.MergeCell(sheet1, "B3", "B5") + f.MergeCell(sheet1, "B3", "B5") - assert.NoError(t, xlsx.RemoveRow(sheet1, 2)) + assert.NoError(t, f.RemoveRow(sheet1, 2)) if !assert.Len(t, r.SheetData.Row, rowCount-2) { t.FailNow() } - assert.NoError(t, xlsx.RemoveRow(sheet1, 4)) + assert.NoError(t, f.RemoveRow(sheet1, 4)) if !assert.Len(t, r.SheetData.Row, rowCount-3) { t.FailNow() } - err = xlsx.AutoFilter(sheet1, "A2", "A2", `{"column":"A","expression":"x != blanks"}`) + err = f.AutoFilter(sheet1, "A2", "A2", `{"column":"A","expression":"x != blanks"}`) if !assert.NoError(t, err) { t.FailNow() } - assert.NoError(t, xlsx.RemoveRow(sheet1, 1)) + assert.NoError(t, f.RemoveRow(sheet1, 1)) if !assert.Len(t, r.SheetData.Row, rowCount-4) { t.FailNow() } - assert.NoError(t, xlsx.RemoveRow(sheet1, 2)) + assert.NoError(t, f.RemoveRow(sheet1, 2)) if !assert.Len(t, r.SheetData.Row, rowCount-5) { t.FailNow() } - assert.NoError(t, xlsx.RemoveRow(sheet1, 1)) + assert.NoError(t, f.RemoveRow(sheet1, 1)) if !assert.Len(t, r.SheetData.Row, rowCount-6) { t.FailNow() } - assert.NoError(t, xlsx.RemoveRow(sheet1, 10)) - assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestRemoveRow.xlsx"))) + assert.NoError(t, f.RemoveRow(sheet1, 10)) + assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRemoveRow.xlsx"))) + + // Test remove row on not exist worksheet + assert.EqualError(t, f.RemoveRow("SheetN", 1), `sheet SheetN is not exist`) } func TestInsertRow(t *testing.T) { -- cgit v1.2.1 From 1666d04559d9f5b579ab7c850ccc95863c31bd25 Mon Sep 17 00:00:00 2001 From: xuri Date: Tue, 24 Dec 2019 01:09:28 +0800 Subject: optimization: checking error in unit tests --- rows_test.go | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index 6494242..fc9d866 100644 --- a/rows_test.go +++ b/rows_test.go @@ -69,7 +69,7 @@ func TestRowsIterator(t *testing.T) { f = NewFile() cells := []string{"C1", "E1", "A3", "B3", "C3", "D3", "E3"} for _, cell := range cells { - f.SetCellValue("Sheet1", cell, 1) + assert.NoError(t, f.SetCellValue("Sheet1", cell, 1)) } rows, err = f.Rows("Sheet1") require.NoError(t, err) @@ -169,8 +169,12 @@ func TestRowVisibility(t *testing.T) { f.NewSheet("Sheet3") assert.NoError(t, f.SetRowVisible("Sheet3", 2, false)) assert.NoError(t, f.SetRowVisible("Sheet3", 2, true)) - f.GetRowVisible("Sheet3", 2) - f.GetRowVisible("Sheet3", 25) + visiable, err := f.GetRowVisible("Sheet3", 2) + assert.Equal(t, true, visiable) + assert.NoError(t, err) + visiable, err = f.GetRowVisible("Sheet3", 25) + assert.Equal(t, false, visiable) + assert.NoError(t, err) assert.EqualError(t, f.SetRowVisible("Sheet3", 0, true), "invalid row number 0") assert.EqualError(t, f.SetRowVisible("SheetN", 2, false), "sheet SheetN is not exist") @@ -194,7 +198,7 @@ func TestRemoveRow(t *testing.T) { ) fillCells(f, sheet1, colCount, rowCount) - f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External") + assert.NoError(t, f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")) assert.EqualError(t, f.RemoveRow(sheet1, -1), "invalid row number -1") @@ -205,7 +209,7 @@ func TestRemoveRow(t *testing.T) { t.FailNow() } - f.MergeCell(sheet1, "B3", "B5") + assert.NoError(t, f.MergeCell(sheet1, "B3", "B5")) assert.NoError(t, f.RemoveRow(sheet1, 2)) if !assert.Len(t, r.SheetData.Row, rowCount-2) { @@ -255,7 +259,7 @@ func TestInsertRow(t *testing.T) { ) fillCells(xlsx, sheet1, colCount, rowCount) - xlsx.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External") + assert.NoError(t, xlsx.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")) assert.EqualError(t, xlsx.InsertRow(sheet1, -1), "invalid row number -1") @@ -305,8 +309,8 @@ func TestDuplicateRowFromSingleRow(t *testing.T) { t.Run("FromSingleRow", func(t *testing.T) { xlsx := NewFile() - xlsx.SetCellStr(sheet, "A1", cells["A1"]) - xlsx.SetCellStr(sheet, "B1", cells["B1"]) + assert.NoError(t, xlsx.SetCellStr(sheet, "A1", cells["A1"])) + assert.NoError(t, xlsx.SetCellStr(sheet, "B1", cells["B1"])) assert.NoError(t, xlsx.DuplicateRow(sheet, 1)) if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.FromSingleRow_1"))) { @@ -358,13 +362,13 @@ func TestDuplicateRowUpdateDuplicatedRows(t *testing.T) { t.Run("UpdateDuplicatedRows", func(t *testing.T) { xlsx := NewFile() - xlsx.SetCellStr(sheet, "A1", cells["A1"]) - xlsx.SetCellStr(sheet, "B1", cells["B1"]) + assert.NoError(t, xlsx.SetCellStr(sheet, "A1", cells["A1"])) + assert.NoError(t, xlsx.SetCellStr(sheet, "B1", cells["B1"])) assert.NoError(t, xlsx.DuplicateRow(sheet, 1)) - xlsx.SetCellStr(sheet, "A2", cells["A2"]) - xlsx.SetCellStr(sheet, "B2", cells["B2"]) + assert.NoError(t, xlsx.SetCellStr(sheet, "A2", cells["A2"])) + assert.NoError(t, xlsx.SetCellStr(sheet, "B2", cells["B2"])) if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.UpdateDuplicatedRows"))) { t.FailNow() @@ -399,8 +403,7 @@ func TestDuplicateRowFirstOfMultipleRows(t *testing.T) { newFileWithDefaults := func() *File { f := NewFile() for cell, val := range cells { - f.SetCellStr(sheet, cell, val) - + assert.NoError(t, f.SetCellStr(sheet, cell, val)) } return f } @@ -514,8 +517,7 @@ func TestDuplicateRowWithLargeOffsetToMiddleOfData(t *testing.T) { newFileWithDefaults := func() *File { f := NewFile() for cell, val := range cells { - f.SetCellStr(sheet, cell, val) - + assert.NoError(t, f.SetCellStr(sheet, cell, val)) } return f } @@ -560,8 +562,7 @@ func TestDuplicateRowWithLargeOffsetToEmptyRows(t *testing.T) { newFileWithDefaults := func() *File { f := NewFile() for cell, val := range cells { - f.SetCellStr(sheet, cell, val) - + assert.NoError(t, f.SetCellStr(sheet, cell, val)) } return f } @@ -606,8 +607,7 @@ func TestDuplicateRowInsertBefore(t *testing.T) { newFileWithDefaults := func() *File { f := NewFile() for cell, val := range cells { - f.SetCellStr(sheet, cell, val) - + assert.NoError(t, f.SetCellStr(sheet, cell, val)) } return f } @@ -653,8 +653,7 @@ func TestDuplicateRowInsertBeforeWithLargeOffset(t *testing.T) { newFileWithDefaults := func() *File { f := NewFile() for cell, val := range cells { - f.SetCellStr(sheet, cell, val) - + assert.NoError(t, f.SetCellStr(sheet, cell, val)) } return f } @@ -704,7 +703,7 @@ func TestDuplicateRowInvalidRownum(t *testing.T) { t.Run(name, func(t *testing.T) { xlsx := NewFile() for col, val := range cells { - xlsx.SetCellStr(sheet, col, val) + assert.NoError(t, xlsx.SetCellStr(sheet, col, val)) } assert.EqualError(t, xlsx.DuplicateRow(sheet, row), fmt.Sprintf("invalid row number %d", row)) @@ -726,7 +725,7 @@ func TestDuplicateRowInvalidRownum(t *testing.T) { t.Run(name, func(t *testing.T) { xlsx := NewFile() for col, val := range cells { - xlsx.SetCellStr(sheet, col, val) + assert.NoError(t, xlsx.SetCellStr(sheet, col, val)) } assert.EqualError(t, xlsx.DuplicateRowTo(sheet, row1, row2), fmt.Sprintf("invalid row number %d", row1)) -- cgit v1.2.1 From 5f5ec76740704a8362e5a120b4a3582b409a5fdd Mon Sep 17 00:00:00 2001 From: xuri Date: Tue, 31 Dec 2019 01:01:16 +0800 Subject: Fix #551, handle empty rows in streaming reading --- rows_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index fc9d866..1127bb1 100644 --- a/rows_test.go +++ b/rows_test.go @@ -136,7 +136,16 @@ func TestColumns(t *testing.T) { f := NewFile() rows, err := f.Rows("Sheet1") assert.NoError(t, err) + + rows.decoder = f.xmlNewDecoder(bytes.NewReader([]byte(`1`))) + _, err = rows.Columns() + assert.NoError(t, err) + rows.decoder = f.xmlNewDecoder(bytes.NewReader([]byte(`1`))) + rows.curRow = 1 + _, err = rows.Columns() + rows.decoder = f.xmlNewDecoder(bytes.NewReader([]byte(`1B`))) + rows.stashRow, rows.curRow = 0, 1 _, err = rows.Columns() assert.EqualError(t, err, `strconv.Atoi: parsing "A": invalid syntax`) -- cgit v1.2.1 From 5ca7231ed408ac264f509ff52b5d28ff4fbda757 Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 3 Jan 2020 23:57:25 +0800 Subject: optimize code and comments: use println errors instead of panic --- rows_test.go | 1 + 1 file changed, 1 insertion(+) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index 1127bb1..9377d5e 100644 --- a/rows_test.go +++ b/rows_test.go @@ -143,6 +143,7 @@ func TestColumns(t *testing.T) { rows.decoder = f.xmlNewDecoder(bytes.NewReader([]byte(`1`))) rows.curRow = 1 _, err = rows.Columns() + assert.NoError(t, err) rows.decoder = f.xmlNewDecoder(bytes.NewReader([]byte(`1B`))) rows.stashRow, rows.curRow = 0, 1 -- cgit v1.2.1 From 8b20ea1685cdb010be8f95ffc047fa44e1a0e90a Mon Sep 17 00:00:00 2001 From: xuri Date: Tue, 25 Feb 2020 00:19:22 +0800 Subject: Fix #586, duplicate row with merged cells --- rows_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index 9377d5e..a5ee428 100644 --- a/rows_test.go +++ b/rows_test.go @@ -693,6 +693,55 @@ func TestDuplicateRowInsertBeforeWithLargeOffset(t *testing.T) { }) } +func TestDuplicateRowInsertBeforeWithMergeCells(t *testing.T) { + const sheet = "Sheet1" + outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx") + + cells := map[string]string{ + "A1": "A1 Value", + "A2": "A2 Value", + "A3": "A3 Value", + "B1": "B1 Value", + "B2": "B2 Value", + "B3": "B3 Value", + } + + newFileWithDefaults := func() *File { + f := NewFile() + for cell, val := range cells { + assert.NoError(t, f.SetCellStr(sheet, cell, val)) + } + assert.NoError(t, f.MergeCell(sheet, "B2", "C2")) + assert.NoError(t, f.MergeCell(sheet, "C6", "C8")) + return f + } + + t.Run("InsertBeforeWithLargeOffset", func(t *testing.T) { + xlsx := newFileWithDefaults() + + assert.NoError(t, xlsx.DuplicateRowTo(sheet, 2, 1)) + assert.NoError(t, xlsx.DuplicateRowTo(sheet, 1, 8)) + + if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.InsertBeforeWithMergeCells"))) { + t.FailNow() + } + + expect := []MergeCell{ + {"B3:C3", "B2 Value"}, + {"C7:C10", ""}, + {"B1:C1", "B2 Value"}, + } + + mergeCells, err := xlsx.GetMergeCells(sheet) + assert.NoError(t, err) + for idx, val := range expect { + if !assert.Equal(t, val, mergeCells[idx]) { + t.FailNow() + } + } + }) +} + func TestDuplicateRowInvalidRownum(t *testing.T) { const sheet = "Sheet1" outFile := filepath.Join("test", "TestDuplicateRowInvalidRownum.%s.xlsx") @@ -753,6 +802,21 @@ func TestDuplicateRowInvalidRownum(t *testing.T) { } } +func TestDuplicateRowTo(t *testing.T) { + f := File{} + assert.EqualError(t, f.DuplicateRowTo("SheetN", 1, 2), "sheet SheetN is not exist") +} + +func TestDuplicateMergeCells(t *testing.T) { + f := File{} + xlsx := &xlsxWorksheet{MergeCells: &xlsxMergeCells{ + Cells: []*xlsxMergeCell{&xlsxMergeCell{Ref: "A1:-"}}, + }} + assert.EqualError(t, f.duplicateMergeCells("Sheet1", xlsx, 0, 0), `cannot convert cell "-" to coordinates: invalid cell name "-"`) + xlsx.MergeCells.Cells[0].Ref = "A1:B1" + assert.EqualError(t, f.duplicateMergeCells("SheetN", xlsx, 1, 2), "sheet SheetN is not exist") +} + func TestGetValueFrom(t *testing.T) { c := &xlsxC{T: "inlineStr"} f := NewFile() -- cgit v1.2.1 From 821a5d86725eb80b3f9e806d91eca5859497c2fa Mon Sep 17 00:00:00 2001 From: xuri Date: Wed, 26 Feb 2020 18:53:50 +0800 Subject: AddPivotTable API changed: new structure PivotTableField to hold pivot table fields for better scalability --- rows_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index a5ee428..a53b0a9 100644 --- a/rows_test.go +++ b/rows_test.go @@ -810,7 +810,7 @@ func TestDuplicateRowTo(t *testing.T) { func TestDuplicateMergeCells(t *testing.T) { f := File{} xlsx := &xlsxWorksheet{MergeCells: &xlsxMergeCells{ - Cells: []*xlsxMergeCell{&xlsxMergeCell{Ref: "A1:-"}}, + Cells: []*xlsxMergeCell{{Ref: "A1:-"}}, }} assert.EqualError(t, f.duplicateMergeCells("Sheet1", xlsx, 0, 0), `cannot convert cell "-" to coordinates: invalid cell name "-"`) xlsx.MergeCells.Cells[0].Ref = "A1:B1" -- cgit v1.2.1 From 1fe660df648422a53eef0c735657cb2f5237ef7b Mon Sep 17 00:00:00 2001 From: xuri Date: Thu, 23 Apr 2020 02:01:14 +0800 Subject: - Resolve #485 use sheet index instead of ID - added 3 internal function: getSheetID, getActiveSheetID, getSheetNameByID --- rows_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index a53b0a9..e5f0524 100644 --- a/rows_test.go +++ b/rows_test.go @@ -92,7 +92,7 @@ func TestRowsError(t *testing.T) { func TestRowHeight(t *testing.T) { xlsx := NewFile() - sheet1 := xlsx.GetSheetName(1) + sheet1 := xlsx.GetSheetName(0) assert.EqualError(t, xlsx.SetRowHeight(sheet1, 0, defaultRowHeightPixels+1.0), "invalid row number 0") @@ -199,7 +199,7 @@ func TestRowVisibility(t *testing.T) { func TestRemoveRow(t *testing.T) { f := NewFile() - sheet1 := f.GetSheetName(1) + sheet1 := f.GetSheetName(0) r, err := f.workSheetReader(sheet1) assert.NoError(t, err) const ( @@ -260,7 +260,7 @@ func TestRemoveRow(t *testing.T) { func TestInsertRow(t *testing.T) { xlsx := NewFile() - sheet1 := xlsx.GetSheetName(1) + sheet1 := xlsx.GetSheetName(0) r, err := xlsx.workSheetReader(sheet1) assert.NoError(t, err) const ( @@ -292,7 +292,7 @@ func TestInsertRow(t *testing.T) { // It is important for insert workflow to be constant to avoid side effect with functions related to internal structure. func TestInsertRowInEmptyFile(t *testing.T) { xlsx := NewFile() - sheet1 := xlsx.GetSheetName(1) + sheet1 := xlsx.GetSheetName(0) r, err := xlsx.workSheetReader(sheet1) assert.NoError(t, err) assert.NoError(t, xlsx.InsertRow(sheet1, 1)) -- cgit v1.2.1 From 2285d4dc718fb8b96c3b2291c63b39c57468b0b9 Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 24 Apr 2020 08:26:16 +0800 Subject: handle the cell without r attribute in a row element --- rows_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'rows_test.go') diff --git a/rows_test.go b/rows_test.go index e5f0524..fd7196d 100644 --- a/rows_test.go +++ b/rows_test.go @@ -831,6 +831,17 @@ func TestErrSheetNotExistError(t *testing.T) { assert.EqualValues(t, err.Error(), "sheet Sheet1 is not exist") } +func TestCheckRow(t *testing.T) { + f := NewFile() + f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`12345`) + _, err := f.GetRows("Sheet1") + assert.NoError(t, err) + assert.NoError(t, f.SetCellValue("Sheet1", "A1", false)) + f = NewFile() + f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`12345`) + assert.EqualError(t, f.SetCellValue("Sheet1", "A1", false), `cannot convert cell "-" to coordinates: invalid cell name "-"`) +} + func BenchmarkRows(b *testing.B) { f, _ := OpenFile(filepath.Join("test", "Book1.xlsx")) for i := 0; i < b.N; i++ { -- cgit v1.2.1