diff --git a/.travis.yml b/.travis.yml index faf9916..5012f86 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ go: - 1.10.x - 1.11.x - 1.12.x + - 1.13.x os: - linux diff --git a/cell_test.go b/cell_test.go index 7d3339f..1efbc5a 100644 --- a/cell_test.go +++ b/cell_test.go @@ -54,8 +54,8 @@ func TestSetCellFloat(t *testing.T) { sheet := "Sheet1" t.Run("with no decimal", func(t *testing.T) { f := NewFile() - f.SetCellFloat(sheet, "A1", 123.0, -1, 64) - f.SetCellFloat(sheet, "A2", 123.0, 1, 64) + assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.0, -1, 64)) + assert.NoError(t, f.SetCellFloat(sheet, "A2", 123.0, 1, 64)) val, err := f.GetCellValue(sheet, "A1") assert.NoError(t, err) assert.Equal(t, "123", val, "A1 should be 123") @@ -66,7 +66,7 @@ func TestSetCellFloat(t *testing.T) { t.Run("with a decimal and precision limit", func(t *testing.T) { f := NewFile() - f.SetCellFloat(sheet, "A1", 123.42, 1, 64) + assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.42, 1, 64)) val, err := f.GetCellValue(sheet, "A1") assert.NoError(t, err) assert.Equal(t, "123.4", val, "A1 should be 123.4") @@ -74,7 +74,7 @@ func TestSetCellFloat(t *testing.T) { t.Run("with a decimal and no limit", func(t *testing.T) { f := NewFile() - f.SetCellFloat(sheet, "A1", 123.42, -1, 64) + assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.42, -1, 64)) val, err := f.GetCellValue(sheet, "A1") assert.NoError(t, err) assert.Equal(t, "123.42", val, "A1 should be 123.42") @@ -101,7 +101,7 @@ func TestGetCellFormula(t *testing.T) { assert.EqualError(t, err, "sheet SheetN is not exist") // Test get cell formula on no formula cell. - f.SetCellValue("Sheet1", "A1", true) + assert.NoError(t, f.SetCellValue("Sheet1", "A1", true)) _, err = f.GetCellFormula("Sheet1", "A1") assert.NoError(t, err) } @@ -109,7 +109,9 @@ func TestGetCellFormula(t *testing.T) { func ExampleFile_SetCellFloat() { f := NewFile() var x = 3.14159265 - f.SetCellFloat("Sheet1", "A1", x, 2, 64) + if err := f.SetCellFloat("Sheet1", "A1", x, 2, 64); err != nil { + fmt.Println(err) + } val, _ := f.GetCellValue("Sheet1", "A1") fmt.Println(val) // Output: 3.14 @@ -122,7 +124,9 @@ func BenchmarkSetCellValue(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { for j := 0; j < len(values); j++ { - f.SetCellValue("Sheet1", fmt.Sprint(cols[j], i), values[j]) + if err := f.SetCellValue("Sheet1", fmt.Sprint(cols[j], i), values[j]); err != nil { + b.Error(err) + } } } } diff --git a/cellmerged_test.go b/cellmerged_test.go index 1da0eb3..e880d05 100644 --- a/cellmerged_test.go +++ b/cellmerged_test.go @@ -21,14 +21,20 @@ func TestMergeCell(t *testing.T) { assert.NoError(t, f.MergeCell("Sheet1", "H7", "B15")) assert.NoError(t, f.MergeCell("Sheet1", "D11", "F13")) assert.NoError(t, f.MergeCell("Sheet1", "G10", "K12")) - f.SetCellValue("Sheet1", "G11", "set value in merged cell") - f.SetCellInt("Sheet1", "H11", 100) - f.SetCellValue("Sheet1", "I11", float64(0.5)) - f.SetCellHyperLink("Sheet1", "J11", "https://github.com/360EntSecGroup-Skylar/excelize", "External") - f.SetCellFormula("Sheet1", "G12", "SUM(Sheet1!B19,Sheet1!C19)") - f.GetCellValue("Sheet1", "H11") - f.GetCellValue("Sheet2", "A6") // Merged cell ref is single coordinate. - f.GetCellFormula("Sheet1", "G12") + assert.NoError(t, f.SetCellValue("Sheet1", "G11", "set value in merged cell")) + assert.NoError(t, f.SetCellInt("Sheet1", "H11", 100)) + assert.NoError(t, f.SetCellValue("Sheet1", "I11", float64(0.5))) + assert.NoError(t, f.SetCellHyperLink("Sheet1", "J11", "https://github.com/360EntSecGroup-Skylar/excelize", "External")) + assert.NoError(t, f.SetCellFormula("Sheet1", "G12", "SUM(Sheet1!B19,Sheet1!C19)")) + value, err := f.GetCellValue("Sheet1", "H11") + assert.Equal(t, "0.5", value) + assert.NoError(t, err) + value, err = f.GetCellValue("Sheet2", "A6") // Merged cell ref is single coordinate. + assert.Equal(t, "", value) + assert.NoError(t, err) + value, err = f.GetCellFormula("Sheet1", "G12") + assert.Equal(t, "SUM(Sheet1!B19,Sheet1!C19)", value) + assert.NoError(t, err) f.NewSheet("Sheet3") assert.NoError(t, f.MergeCell("Sheet3", "D11", "F13")) diff --git a/chart_test.go b/chart_test.go index bc5c30a..2379ddc 100644 --- a/chart_test.go +++ b/chart_test.go @@ -22,7 +22,7 @@ func TestChartSize(t *testing.T) { "D1": "Pear", } for cell, v := range categories { - xlsx.SetCellValue(sheet1, cell, v) + assert.NoError(t, xlsx.SetCellValue(sheet1, cell, v)) } values := map[string]int{ @@ -37,29 +37,22 @@ func TestChartSize(t *testing.T) { "D4": 8, } for cell, v := range values { - xlsx.SetCellValue(sheet1, cell, v) + assert.NoError(t, xlsx.SetCellValue(sheet1, cell, v)) } - xlsx.AddChart("Sheet1", "E4", `{"type":"col3DClustered","dimension":{"width":640, "height":480},`+ + assert.NoError(t, xlsx.AddChart("Sheet1", "E4", `{"type":"col3DClustered","dimension":{"width":640, "height":480},`+ `"series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},`+ `{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},`+ `{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],`+ - `"title":{"name":"3D Clustered Column Chart"}}`) + `"title":{"name":"3D Clustered Column Chart"}}`)) - var ( - buffer bytes.Buffer - ) + var buffer bytes.Buffer // Save xlsx file by the given path. - err := xlsx.Write(&buffer) - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, xlsx.Write(&buffer)) newFile, err := OpenReader(&buffer) - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, err) chartsNum := newFile.countCharts() if !assert.Equal(t, 1, chartsNum, "Expected 1 chart, actual %d", chartsNum) { diff --git a/col_test.go b/col_test.go index edbdae7..cdb7edf 100644 --- a/col_test.go +++ b/col_test.go @@ -37,17 +37,29 @@ func TestColumnVisibility(t *testing.T) { t.Run("TestBook3", func(t *testing.T) { f, err := prepareTestBook3() assert.NoError(t, err) - f.GetColVisible("Sheet1", "B") + visible, err := f.GetColVisible("Sheet1", "B") + assert.Equal(t, true, visible) + assert.NoError(t, err) }) } func TestOutlineLevel(t *testing.T) { f := NewFile() - f.GetColOutlineLevel("Sheet1", "D") + level, err := f.GetColOutlineLevel("Sheet1", "D") + assert.Equal(t, uint8(0), level) + assert.NoError(t, err) + f.NewSheet("Sheet2") assert.NoError(t, f.SetColOutlineLevel("Sheet1", "D", 4)) - f.GetColOutlineLevel("Sheet1", "D") - f.GetColOutlineLevel("Shee2", "A") + + level, err = f.GetColOutlineLevel("Sheet1", "D") + assert.Equal(t, uint8(4), level) + assert.NoError(t, err) + + level, err = f.GetColOutlineLevel("Shee2", "A") + assert.Equal(t, uint8(0), level) + assert.EqualError(t, err, "sheet Shee2 is not exist") + assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13)) assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2)) assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7)) @@ -56,7 +68,7 @@ func TestOutlineLevel(t *testing.T) { // Test set row outline level on not exists worksheet. assert.EqualError(t, f.SetRowOutlineLevel("SheetN", 1, 4), "sheet SheetN is not exist") // Test get row outline level on not exists worksheet. - _, err := f.GetRowOutlineLevel("SheetN", 1) + _, err = f.GetRowOutlineLevel("SheetN", 1) assert.EqualError(t, err, "sheet SheetN is not exist") // Test set and get column outline level with illegal cell coordinates. @@ -68,7 +80,7 @@ func TestOutlineLevel(t *testing.T) { assert.EqualError(t, f.SetColOutlineLevel("SheetN", "E", 2), "sheet SheetN is not exist") assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 0, 1), "invalid row number 0") - level, err := f.GetRowOutlineLevel("Sheet1", 2) + level, err = f.GetRowOutlineLevel("Sheet1", 2) assert.NoError(t, err) assert.Equal(t, uint8(7), level) @@ -83,7 +95,7 @@ func TestOutlineLevel(t *testing.T) { f, err = OpenFile(filepath.Join("test", "Book1.xlsx")) assert.NoError(t, err) - f.SetColOutlineLevel("Sheet2", "B", 2) + assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2)) } func TestSetColStyle(t *testing.T) { @@ -105,13 +117,18 @@ func TestSetColStyle(t *testing.T) { func TestColWidth(t *testing.T) { f := NewFile() - f.SetColWidth("Sheet1", "B", "A", 12) - f.SetColWidth("Sheet1", "A", "B", 12) - f.GetColWidth("Sheet1", "A") - f.GetColWidth("Sheet1", "C") + assert.NoError(t, f.SetColWidth("Sheet1", "B", "A", 12)) + assert.NoError(t, f.SetColWidth("Sheet1", "A", "B", 12)) + width, err := f.GetColWidth("Sheet1", "A") + assert.Equal(t, float64(12), width) + assert.NoError(t, err) + width, err = f.GetColWidth("Sheet1", "C") + assert.Equal(t, float64(64), width) + assert.NoError(t, err) // Test set and get column width with illegal cell coordinates. - _, err := f.GetColWidth("Sheet1", "*") + width, err = f.GetColWidth("Sheet1", "*") + assert.Equal(t, float64(64), width) assert.EqualError(t, err, `invalid column name "*"`) assert.EqualError(t, f.SetColWidth("Sheet1", "*", "B", 1), `invalid column name "*"`) assert.EqualError(t, f.SetColWidth("Sheet1", "A", "*", 1), `invalid column name "*"`) @@ -133,8 +150,8 @@ func TestInsertCol(t *testing.T) { fillCells(f, sheet1, 10, 10) - f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External") - f.MergeCell(sheet1, "A1", "C3") + assert.NoError(t, f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")) + assert.NoError(t, f.MergeCell(sheet1, "A1", "C3")) assert.NoError(t, f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`)) assert.NoError(t, f.InsertCol(sheet1, "A")) @@ -151,11 +168,11 @@ func TestRemoveCol(t *testing.T) { fillCells(f, sheet1, 10, 15) - f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External") - f.SetCellHyperLink(sheet1, "C5", "https://github.com", "External") + assert.NoError(t, f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")) + assert.NoError(t, f.SetCellHyperLink(sheet1, "C5", "https://github.com", "External")) - f.MergeCell(sheet1, "A1", "B1") - f.MergeCell(sheet1, "A2", "B2") + assert.NoError(t, f.MergeCell(sheet1, "A1", "B1")) + assert.NoError(t, f.MergeCell(sheet1, "A2", "B2")) assert.NoError(t, f.RemoveCol(sheet1, "A")) assert.NoError(t, f.RemoveCol(sheet1, "A")) diff --git a/datavalidation_test.go b/datavalidation_test.go index 763bad1..7e54d55 100644 --- a/datavalidation_test.go +++ b/datavalidation_test.go @@ -24,53 +24,45 @@ func TestDataValidation(t *testing.T) { dvRange := NewDataValidation(true) dvRange.Sqref = "A1:B2" - dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween) + assert.NoError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween)) dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body") dvRange.SetError(DataValidationErrorStyleWarning, "error title", "error body") dvRange.SetError(DataValidationErrorStyleInformation, "error title", "error body") - f.AddDataValidation("Sheet1", dvRange) - if !assert.NoError(t, f.SaveAs(resultFile)) { - t.FailNow() - } + assert.NoError(t, f.AddDataValidation("Sheet1", dvRange)) + assert.NoError(t, f.SaveAs(resultFile)) dvRange = NewDataValidation(true) dvRange.Sqref = "A3:B4" - dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan) + assert.NoError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)) dvRange.SetInput("input title", "input body") - f.AddDataValidation("Sheet1", dvRange) - if !assert.NoError(t, f.SaveAs(resultFile)) { - t.FailNow() - } + assert.NoError(t, f.AddDataValidation("Sheet1", dvRange)) + assert.NoError(t, f.SaveAs(resultFile)) dvRange = NewDataValidation(true) dvRange.Sqref = "A5:B6" - dvRange.SetDropList([]string{"1", "2", "3"}) - f.AddDataValidation("Sheet1", dvRange) - if !assert.NoError(t, f.SaveAs(resultFile)) { - t.FailNow() - } + assert.NoError(t, dvRange.SetDropList([]string{"1", "2", "3"})) + assert.NoError(t, f.AddDataValidation("Sheet1", dvRange)) + assert.NoError(t, f.SaveAs(resultFile)) } func TestDataValidationError(t *testing.T) { resultFile := filepath.Join("test", "TestDataValidationError.xlsx") f := NewFile() - f.SetCellStr("Sheet1", "E1", "E1") - f.SetCellStr("Sheet1", "E2", "E2") - f.SetCellStr("Sheet1", "E3", "E3") + assert.NoError(t, f.SetCellStr("Sheet1", "E1", "E1")) + assert.NoError(t, f.SetCellStr("Sheet1", "E2", "E2")) + assert.NoError(t, f.SetCellStr("Sheet1", "E3", "E3")) dvRange := NewDataValidation(true) dvRange.SetSqref("A7:B8") dvRange.SetSqref("A7:B8") - dvRange.SetSqrefDropList("$E$1:$E$3", true) + assert.NoError(t, dvRange.SetSqrefDropList("$E$1:$E$3", true)) err := dvRange.SetSqrefDropList("$E$1:$E$3", false) assert.EqualError(t, err, "cross-sheet sqref cell are not supported") - f.AddDataValidation("Sheet1", dvRange) - if !assert.NoError(t, f.SaveAs(resultFile)) { - t.FailNow() - } + assert.NoError(t, f.AddDataValidation("Sheet1", dvRange)) + assert.NoError(t, f.SaveAs(resultFile)) dvRange = NewDataValidation(true) err = dvRange.SetDropList(make([]string, 258)) @@ -79,13 +71,11 @@ func TestDataValidationError(t *testing.T) { return } assert.EqualError(t, err, "data validation must be 0-255 characters") - dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan) + assert.NoError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)) dvRange.SetSqref("A9:B10") - f.AddDataValidation("Sheet1", dvRange) - if !assert.NoError(t, f.SaveAs(resultFile)) { - t.FailNow() - } + assert.NoError(t, f.AddDataValidation("Sheet1", dvRange)) + assert.NoError(t, f.SaveAs(resultFile)) // Test width invalid data validation formula. dvRange.Formula1 = strings.Repeat("s", dataValidationFormulaStrLen+22) diff --git a/excelize.go b/excelize.go index 135028c..94f401c 100644 --- a/excelize.go +++ b/excelize.go @@ -138,7 +138,7 @@ func (f *File) setDefaultTimeStyle(sheet, axis string, format int) error { } if s == 0 { style, _ := f.NewStyle(`{"number_format": ` + strconv.Itoa(format) + `}`) - f.SetCellStyle(sheet, axis, axis, style) + _ = f.SetCellStyle(sheet, axis, axis, style) } return err } diff --git a/excelize_test.go b/excelize_test.go index 1d6ed24..c4b600d 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -24,12 +24,11 @@ import ( func TestOpenFile(t *testing.T) { // Test update a XLSX file. f, err := OpenFile(filepath.Join("test", "Book1.xlsx")) - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, err) // Test get all the rows in a not exists worksheet. - f.GetRows("Sheet4") + _, err = f.GetRows("Sheet4") + assert.EqualError(t, err, "sheet Sheet4 is not exist") // Test get all the rows in a worksheet. rows, err := f.GetRows("Sheet2") assert.NoError(t, err) @@ -39,10 +38,10 @@ func TestOpenFile(t *testing.T) { } t.Log("\r\n") } - f.UpdateLinkedValue() + assert.NoError(t, f.UpdateLinkedValue()) - f.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(float64(100.1588), 'f', -1, 32)) - f.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64)) + assert.NoError(t, f.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(float64(100.1588), 'f', -1, 32))) + assert.NoError(t, f.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64))) // Test set cell value with illegal row number. assert.EqualError(t, f.SetCellDefault("Sheet2", "A", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64)), @@ -53,14 +52,14 @@ func TestOpenFile(t *testing.T) { // Test set cell integer value with illegal row number. assert.EqualError(t, f.SetCellInt("Sheet2", "A", 100), `cannot convert cell "A" to coordinates: invalid cell name "A"`) - f.SetCellStr("Sheet2", "C11", "Knowns") + assert.NoError(t, f.SetCellStr("Sheet2", "C11", "Knowns")) // Test max characters in a cell. - f.SetCellStr("Sheet2", "D11", strings.Repeat("c", 32769)) + assert.NoError(t, f.SetCellStr("Sheet2", "D11", strings.Repeat("c", 32769))) f.NewSheet(":\\/?*[]Maximum 31 characters allowed in sheet title.") // Test set worksheet name with illegal name. f.SetSheetName("Maximum 31 characters allowed i", "[Rename]:\\/?* Maximum 31 characters allowed in sheet title.") - f.SetCellInt("Sheet3", "A23", 10) - f.SetCellStr("Sheet3", "b230", "10") + assert.EqualError(t, f.SetCellInt("Sheet3", "A23", 10), "sheet Sheet3 is not exist") + assert.EqualError(t, f.SetCellStr("Sheet3", "b230", "10"), "sheet Sheet3 is not exist") assert.EqualError(t, f.SetCellStr("Sheet10", "b230", "10"), "sheet Sheet10 is not exist") // Test set cell string value with illegal row number. @@ -137,21 +136,21 @@ func TestOpenFile(t *testing.T) { {true, "1"}, } for _, test := range booltest { - f.SetCellValue("Sheet2", "F16", test.value) + assert.NoError(t, f.SetCellValue("Sheet2", "F16", test.value)) val, err := f.GetCellValue("Sheet2", "F16") assert.NoError(t, err) assert.Equal(t, test.expected, val) } - f.SetCellValue("Sheet2", "G2", nil) + assert.NoError(t, f.SetCellValue("Sheet2", "G2", nil)) assert.EqualError(t, f.SetCellValue("Sheet2", "G4", time.Now()), "only UTC time expected") - f.SetCellValue("Sheet2", "G4", time.Now().UTC()) + assert.NoError(t, f.SetCellValue("Sheet2", "G4", time.Now().UTC())) // 02:46:40 - f.SetCellValue("Sheet2", "G5", time.Duration(1e13)) + assert.NoError(t, f.SetCellValue("Sheet2", "G5", time.Duration(1e13))) // Test completion column. - f.SetCellValue("Sheet2", "M2", nil) + assert.NoError(t, f.SetCellValue("Sheet2", "M2", nil)) // Test read cell value with given axis large than exists row. _, err = f.GetCellValue("Sheet2", "E231") assert.NoError(t, err) @@ -161,10 +160,10 @@ func TestOpenFile(t *testing.T) { f.GetSheetIndex("Sheet1") // Test get worksheet name of XLSX by given invalid worksheet index. f.GetSheetName(4) - // Test get worksheet map of f. + // Test get worksheet map of workbook. f.GetSheetMap() for i := 1; i <= 300; i++ { - f.SetCellStr("Sheet3", "c"+strconv.Itoa(i), strconv.Itoa(i)) + assert.NoError(t, f.SetCellStr("Sheet2", "c"+strconv.Itoa(i), strconv.Itoa(i))) } assert.NoError(t, f.SaveAs(filepath.Join("test", "TestOpenFile.xlsx"))) } @@ -259,8 +258,8 @@ func TestNewFile(t *testing.T) { f.NewSheet("Sheet1") f.NewSheet("XLSXSheet2") f.NewSheet("XLSXSheet3") - f.SetCellInt("XLSXSheet2", "A23", 56) - f.SetCellStr("Sheet1", "B20", "42") + assert.NoError(t, f.SetCellInt("XLSXSheet2", "A23", 56)) + assert.NoError(t, f.SetCellStr("Sheet1", "B20", "42")) f.SetActiveSheet(0) // Test add picture to sheet with scaling and positioning. @@ -310,12 +309,14 @@ func TestSetCellHyperLink(t *testing.T) { assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCellHyperLink.xlsx"))) f = NewFile() - f.workSheetReader("Sheet1") + _, err = f.workSheetReader("Sheet1") + assert.NoError(t, err) f.Sheet["xl/worksheets/sheet1.xml"].Hyperlinks = &xlsxHyperlinks{Hyperlink: make([]xlsxHyperlink, 65530)} assert.EqualError(t, f.SetCellHyperLink("Sheet1", "A65531", "https://github.com/360EntSecGroup-Skylar/excelize", "External"), "over maximum limit hyperlinks in a worksheet") f = NewFile() - f.workSheetReader("Sheet1") + _, err = f.workSheetReader("Sheet1") + assert.NoError(t, err) f.Sheet["xl/worksheets/sheet1.xml"].MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:A"}}} err = f.SetCellHyperLink("Sheet1", "A1", "https://github.com/360EntSecGroup-Skylar/excelize", "External") assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`) @@ -341,7 +342,8 @@ func TestGetCellHyperLink(t *testing.T) { t.Log(link, target) f = NewFile() - f.workSheetReader("Sheet1") + _, err = f.workSheetReader("Sheet1") + assert.NoError(t, err) f.Sheet["xl/worksheets/sheet1.xml"].Hyperlinks = &xlsxHyperlinks{ Hyperlink: []xlsxHyperlink{{Ref: "A1"}}, } @@ -364,8 +366,8 @@ func TestSetCellFormula(t *testing.T) { t.FailNow() } - f.SetCellFormula("Sheet1", "B19", "SUM(Sheet2!D2,Sheet2!D11)") - f.SetCellFormula("Sheet1", "C19", "SUM(Sheet2!D2,Sheet2!D9)") + assert.NoError(t, f.SetCellFormula("Sheet1", "B19", "SUM(Sheet2!D2,Sheet2!D11)")) + assert.NoError(t, f.SetCellFormula("Sheet1", "C19", "SUM(Sheet2!D2,Sheet2!D9)")) // Test set cell formula with illegal rows number. assert.EqualError(t, f.SetCellFormula("Sheet1", "C", "SUM(Sheet2!D2,Sheet2!D9)"), `cannot convert cell "C" to coordinates: invalid cell name "C"`) @@ -377,10 +379,10 @@ func TestSetCellFormula(t *testing.T) { t.FailNow() } // Test remove cell formula. - f.SetCellFormula("Sheet1", "A1", "") + assert.NoError(t, f.SetCellFormula("Sheet1", "A1", "")) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCellFormula2.xlsx"))) // Test remove all cell formula. - f.SetCellFormula("Sheet1", "B1", "") + assert.NoError(t, f.SetCellFormula("Sheet1", "B1", "")) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCellFormula3.xlsx"))) } @@ -471,31 +473,31 @@ func TestWriteArrayFormula(t *testing.T) { } // Line 2 contains the results of AVERAGEIF - f.SetCellStr("Sheet1", "A2", "Average") + assert.NoError(t, f.SetCellStr("Sheet1", "A2", "Average")) // Line 3 contains the average that was calculated in Go - f.SetCellStr("Sheet1", "A3", "Average (calculated)") + assert.NoError(t, f.SetCellStr("Sheet1", "A3", "Average (calculated)")) // Line 4 contains the results of the array function that calculates the standard deviation - f.SetCellStr("Sheet1", "A4", "Std. deviation") + assert.NoError(t, f.SetCellStr("Sheet1", "A4", "Std. deviation")) // Line 5 contains the standard deviations calculated in Go - f.SetCellStr("Sheet1", "A5", "Std. deviation (calculated)") + assert.NoError(t, f.SetCellStr("Sheet1", "A5", "Std. deviation (calculated)")) - f.SetCellStr("Sheet1", "B1", sample[0]) - f.SetCellStr("Sheet1", "C1", sample[1]) - f.SetCellStr("Sheet1", "D1", sample[2]) + assert.NoError(t, f.SetCellStr("Sheet1", "B1", sample[0])) + assert.NoError(t, f.SetCellStr("Sheet1", "C1", sample[1])) + assert.NoError(t, f.SetCellStr("Sheet1", "D1", sample[2])) firstResLine := 8 - f.SetCellStr("Sheet1", cell(1, firstResLine-1), "Result Values") - f.SetCellStr("Sheet1", cell(2, firstResLine-1), "Sample") + assert.NoError(t, f.SetCellStr("Sheet1", cell(1, firstResLine-1), "Result Values")) + assert.NoError(t, f.SetCellStr("Sheet1", cell(2, firstResLine-1), "Sample")) for i := 0; i != len(values); i++ { valCell := cell(1, i+firstResLine) assocCell := cell(2, i+firstResLine) - f.SetCellInt("Sheet1", valCell, values[i]) - f.SetCellStr("Sheet1", assocCell, sample[assoc[i]]) + assert.NoError(t, f.SetCellInt("Sheet1", valCell, values[i])) + assert.NoError(t, f.SetCellStr("Sheet1", assocCell, sample[assoc[i]])) } valRange := fmt.Sprintf("$A$%d:$A$%d", firstResLine, len(values)+firstResLine-1) @@ -508,11 +510,11 @@ func TestWriteArrayFormula(t *testing.T) { stdevCell := cell(i+2, 4) calcStdevCell := cell(i+2, 5) - f.SetCellInt("Sheet1", calcAvgCell, average(i)) - f.SetCellInt("Sheet1", calcStdevCell, stdev(i)) + assert.NoError(t, f.SetCellInt("Sheet1", calcAvgCell, average(i))) + assert.NoError(t, f.SetCellInt("Sheet1", calcStdevCell, stdev(i))) // Average can be done with AVERAGEIF - f.SetCellFormula("Sheet1", avgCell, fmt.Sprintf("ROUND(AVERAGEIF(%s,%s,%s),0)", assocRange, nameCell, valRange)) + assert.NoError(t, f.SetCellFormula("Sheet1", avgCell, fmt.Sprintf("ROUND(AVERAGEIF(%s,%s,%s),0)", assocRange, nameCell, valRange))) ref := stdevCell + ":" + stdevCell t := STCellFormulaTypeArray @@ -623,9 +625,9 @@ func TestSetCellStyleNumberFormat(t *testing.T) { var val float64 val, err = strconv.ParseFloat(v, 64) if err != nil { - f.SetCellValue("Sheet2", c, v) + assert.NoError(t, f.SetCellValue("Sheet2", c, v)) } else { - f.SetCellValue("Sheet2", c, val) + assert.NoError(t, f.SetCellValue("Sheet2", c, val)) } style, err := f.NewStyle(`{"fill":{"type":"gradient","color":["#FFFFFF","#E0EBF5"],"shading":5},"number_format": ` + strconv.Itoa(d) + `}`) if !assert.NoError(t, err) { @@ -652,8 +654,8 @@ func TestSetCellStyleCurrencyNumberFormat(t *testing.T) { t.FailNow() } - f.SetCellValue("Sheet1", "A1", 56) - f.SetCellValue("Sheet1", "A2", -32.3) + assert.NoError(t, f.SetCellValue("Sheet1", "A1", 56)) + assert.NoError(t, f.SetCellValue("Sheet1", "A2", -32.3)) var style int style, err = f.NewStyle(`{"number_format": 188, "decimal_places": -1}`) if !assert.NoError(t, err) { @@ -676,8 +678,8 @@ func TestSetCellStyleCurrencyNumberFormat(t *testing.T) { if !assert.NoError(t, err) { t.FailNow() } - f.SetCellValue("Sheet1", "A1", 42920.5) - f.SetCellValue("Sheet1", "A2", 42920.5) + assert.NoError(t, f.SetCellValue("Sheet1", "A1", 42920.5)) + assert.NoError(t, f.SetCellValue("Sheet1", "A2", 42920.5)) _, err = f.NewStyle(`{"number_format": 26, "lang": "zh-tw"}`) if !assert.NoError(t, err) { @@ -709,8 +711,8 @@ func TestSetCellStyleCurrencyNumberFormat(t *testing.T) { func TestSetCellStyleCustomNumberFormat(t *testing.T) { f := NewFile() - f.SetCellValue("Sheet1", "A1", 42920.5) - f.SetCellValue("Sheet1", "A2", 42920.5) + assert.NoError(t, f.SetCellValue("Sheet1", "A1", 42920.5)) + assert.NoError(t, f.SetCellValue("Sheet1", "A2", 42920.5)) style, err := f.NewStyle(`{"custom_number_format": "[$-380A]dddd\\,\\ dd\" de \"mmmm\" de \"yyyy;@"}`) if err != nil { t.Log(err) @@ -841,8 +843,8 @@ func TestSetDeleteSheet(t *testing.T) { t.FailNow() } f.DeleteSheet("Sheet1") - f.AddComment("Sheet1", "A1", "") - f.AddComment("Sheet1", "A1", `{"author":"Excelize: ","text":"This is a comment."}`) + assert.EqualError(t, f.AddComment("Sheet1", "A1", ""), "unexpected end of JSON input") + assert.NoError(t, f.AddComment("Sheet1", "A1", `{"author":"Excelize: ","text":"This is a comment."}`)) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetDeleteSheet.TestBook4.xlsx"))) }) } @@ -853,10 +855,10 @@ func TestSheetVisibility(t *testing.T) { t.FailNow() } - f.SetSheetVisible("Sheet2", false) - f.SetSheetVisible("Sheet1", false) - f.SetSheetVisible("Sheet1", true) - f.GetSheetVisible("Sheet1") + assert.NoError(t, f.SetSheetVisible("Sheet2", false)) + assert.NoError(t, f.SetSheetVisible("Sheet1", false)) + assert.NoError(t, f.SetSheetVisible("Sheet1", true)) + assert.Equal(t, true, f.GetSheetVisible("Sheet1")) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSheetVisibility.xlsx"))) } @@ -870,7 +872,7 @@ func TestCopySheet(t *testing.T) { idx := f.NewSheet("CopySheet") assert.NoError(t, f.CopySheet(1, idx)) - f.SetCellValue("CopySheet", "F1", "Hello") + assert.NoError(t, f.SetCellValue("CopySheet", "F1", "Hello")) val, err := f.GetCellValue("Sheet1", "F1") assert.NoError(t, err) assert.NotEqual(t, "Hello", val) @@ -1072,31 +1074,31 @@ func TestConditionalFormat(t *testing.T) { } // Color scales: 2 color. - f.SetConditionalFormat(sheet1, "A1:A10", `[{"type":"2_color_scale","criteria":"=","min_type":"min","max_type":"max","min_color":"#F8696B","max_color":"#63BE7B"}]`) + assert.NoError(t, f.SetConditionalFormat(sheet1, "A1:A10", `[{"type":"2_color_scale","criteria":"=","min_type":"min","max_type":"max","min_color":"#F8696B","max_color":"#63BE7B"}]`)) // Color scales: 3 color. - f.SetConditionalFormat(sheet1, "B1:B10", `[{"type":"3_color_scale","criteria":"=","min_type":"min","mid_type":"percentile","max_type":"max","min_color":"#F8696B","mid_color":"#FFEB84","max_color":"#63BE7B"}]`) + assert.NoError(t, f.SetConditionalFormat(sheet1, "B1:B10", `[{"type":"3_color_scale","criteria":"=","min_type":"min","mid_type":"percentile","max_type":"max","min_color":"#F8696B","mid_color":"#FFEB84","max_color":"#63BE7B"}]`)) // Hightlight cells rules: between... - f.SetConditionalFormat(sheet1, "C1:C10", fmt.Sprintf(`[{"type":"cell","criteria":"between","format":%d,"minimum":"6","maximum":"8"}]`, format1)) + assert.NoError(t, f.SetConditionalFormat(sheet1, "C1:C10", fmt.Sprintf(`[{"type":"cell","criteria":"between","format":%d,"minimum":"6","maximum":"8"}]`, format1))) // Hightlight cells rules: Greater Than... - f.SetConditionalFormat(sheet1, "D1:D10", fmt.Sprintf(`[{"type":"cell","criteria":">","format":%d,"value":"6"}]`, format3)) + assert.NoError(t, f.SetConditionalFormat(sheet1, "D1:D10", fmt.Sprintf(`[{"type":"cell","criteria":">","format":%d,"value":"6"}]`, format3))) // Hightlight cells rules: Equal To... - f.SetConditionalFormat(sheet1, "E1:E10", fmt.Sprintf(`[{"type":"top","criteria":"=","format":%d}]`, format3)) + assert.NoError(t, f.SetConditionalFormat(sheet1, "E1:E10", fmt.Sprintf(`[{"type":"top","criteria":"=","format":%d}]`, format3))) // Hightlight cells rules: Not Equal To... - f.SetConditionalFormat(sheet1, "F1:F10", fmt.Sprintf(`[{"type":"unique","criteria":"=","format":%d}]`, format2)) + assert.NoError(t, f.SetConditionalFormat(sheet1, "F1:F10", fmt.Sprintf(`[{"type":"unique","criteria":"=","format":%d}]`, format2))) // Hightlight cells rules: Duplicate Values... - f.SetConditionalFormat(sheet1, "G1:G10", fmt.Sprintf(`[{"type":"duplicate","criteria":"=","format":%d}]`, format2)) + assert.NoError(t, f.SetConditionalFormat(sheet1, "G1:G10", fmt.Sprintf(`[{"type":"duplicate","criteria":"=","format":%d}]`, format2))) // Top/Bottom rules: Top 10%. - f.SetConditionalFormat(sheet1, "H1:H10", fmt.Sprintf(`[{"type":"top","criteria":"=","format":%d,"value":"6","percent":true}]`, format1)) + assert.NoError(t, f.SetConditionalFormat(sheet1, "H1:H10", fmt.Sprintf(`[{"type":"top","criteria":"=","format":%d,"value":"6","percent":true}]`, format1))) // Top/Bottom rules: Above Average... - f.SetConditionalFormat(sheet1, "I1:I10", fmt.Sprintf(`[{"type":"average","criteria":"=","format":%d, "above_average": true}]`, format3)) + assert.NoError(t, f.SetConditionalFormat(sheet1, "I1:I10", fmt.Sprintf(`[{"type":"average","criteria":"=","format":%d, "above_average": true}]`, format3))) // Top/Bottom rules: Below Average... - f.SetConditionalFormat(sheet1, "J1:J10", fmt.Sprintf(`[{"type":"average","criteria":"=","format":%d, "above_average": false}]`, format1)) + assert.NoError(t, f.SetConditionalFormat(sheet1, "J1:J10", fmt.Sprintf(`[{"type":"average","criteria":"=","format":%d, "above_average": false}]`, format1))) // Data Bars: Gradient Fill. - f.SetConditionalFormat(sheet1, "K1:K10", `[{"type":"data_bar", "criteria":"=", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`) + assert.NoError(t, f.SetConditionalFormat(sheet1, "K1:K10", `[{"type":"data_bar", "criteria":"=", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`)) // Use a formula to determine which cells to format. - f.SetConditionalFormat(sheet1, "L1:L10", fmt.Sprintf(`[{"type":"formula", "criteria":"L2<3", "format":%d}]`, format1)) + assert.NoError(t, f.SetConditionalFormat(sheet1, "L1:L10", fmt.Sprintf(`[{"type":"formula", "criteria":"L2<3", "format":%d}]`, format1))) // Test set invalid format set in conditional format - f.SetConditionalFormat(sheet1, "L1:L10", "") + assert.EqualError(t, f.SetConditionalFormat(sheet1, "L1:L10", ""), "unexpected end of JSON input") err = f.SaveAs(filepath.Join("test", "TestConditionalFormat.xlsx")) if !assert.NoError(t, err) { @@ -1104,9 +1106,9 @@ func TestConditionalFormat(t *testing.T) { } // Set conditional format with illegal valid type. - f.SetConditionalFormat(sheet1, "K1:K10", `[{"type":"", "criteria":"=", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`) + assert.NoError(t, f.SetConditionalFormat(sheet1, "K1:K10", `[{"type":"", "criteria":"=", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`)) // Set conditional format with illegal criteria type. - f.SetConditionalFormat(sheet1, "K1:K10", `[{"type":"data_bar", "criteria":"", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`) + assert.NoError(t, f.SetConditionalFormat(sheet1, "K1:K10", `[{"type":"data_bar", "criteria":"", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`)) // Set conditional format with file without dxfs element shold not return error. f, err = OpenFile(filepath.Join("test", "Book1.xlsx")) @@ -1156,7 +1158,7 @@ func TestSetSheetRow(t *testing.T) { t.FailNow() } - f.SetSheetRow("Sheet1", "B27", &[]interface{}{"cell", nil, int32(42), float64(42), time.Now().UTC()}) + assert.NoError(t, f.SetSheetRow("Sheet1", "B27", &[]interface{}{"cell", nil, int32(42), float64(42), time.Now().UTC()})) assert.EqualError(t, f.SetSheetRow("Sheet1", "", &[]interface{}{"cell", nil, 2}), `cannot convert cell "" to coordinates: invalid cell name ""`) @@ -1193,11 +1195,11 @@ func TestHSL(t *testing.T) { func TestProtectSheet(t *testing.T) { f := NewFile() - f.ProtectSheet("Sheet1", nil) - f.ProtectSheet("Sheet1", &FormatSheetProtection{ + assert.NoError(t, f.ProtectSheet("Sheet1", nil)) + assert.NoError(t, f.ProtectSheet("Sheet1", &FormatSheetProtection{ Password: "password", EditScenarios: false, - }) + })) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestProtectSheet.xlsx"))) // Test protect not exists worksheet. @@ -1212,7 +1214,7 @@ func TestUnprotectSheet(t *testing.T) { // Test unprotect not exists worksheet. assert.EqualError(t, f.UnprotectSheet("SheetN"), "sheet SheetN is not exist") - f.UnprotectSheet("Sheet1") + assert.NoError(t, f.UnprotectSheet("Sheet1")) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestUnprotectSheet.xlsx"))) } @@ -1325,8 +1327,12 @@ func prepareTestBook3() (*File, error) { f.NewSheet("Sheet1") f.NewSheet("XLSXSheet2") f.NewSheet("XLSXSheet3") - f.SetCellInt("XLSXSheet2", "A23", 56) - f.SetCellStr("Sheet1", "B20", "42") + if err := f.SetCellInt("XLSXSheet2", "A23", 56); err != nil { + return nil, err + } + if err := f.SetCellStr("Sheet1", "B20", "42"); err != nil { + return nil, err + } f.SetActiveSheet(0) err := f.AddPicture("Sheet1", "H2", filepath.Join("test", "images", "excel.gif"), @@ -1345,10 +1351,18 @@ func prepareTestBook3() (*File, error) { func prepareTestBook4() (*File, error) { f := NewFile() - f.SetColWidth("Sheet1", "B", "A", 12) - f.SetColWidth("Sheet1", "A", "B", 12) - f.GetColWidth("Sheet1", "A") - f.GetColWidth("Sheet1", "C") + if err := f.SetColWidth("Sheet1", "B", "A", 12); err != nil { + return f, err + } + if err := f.SetColWidth("Sheet1", "A", "B", 12); err != nil { + return f, err + } + if _, err := f.GetColWidth("Sheet1", "A"); err != nil { + return f, err + } + if _, err := f.GetColWidth("Sheet1", "C"); err != nil { + return f, err + } return f, nil } @@ -1357,13 +1371,17 @@ func fillCells(f *File, sheet string, colCount, rowCount int) { for col := 1; col <= colCount; col++ { for row := 1; row <= rowCount; row++ { cell, _ := CoordinatesToCellName(col, row) - f.SetCellStr(sheet, cell, cell) + if err := f.SetCellStr(sheet, cell, cell); err != nil { + panic(err) + } } } } func BenchmarkOpenFile(b *testing.B) { for i := 0; i < b.N; i++ { - OpenFile(filepath.Join("test", "Book1.xlsx")) + if _, err := OpenFile(filepath.Join("test", "Book1.xlsx")); err != nil { + b.Error(err) + } } } diff --git a/file_test.go b/file_test.go index 6c30f4a..97ff720 100644 --- a/file_test.go +++ b/file_test.go @@ -14,7 +14,9 @@ func BenchmarkWrite(b *testing.B) { if err != nil { panic(err) } - f.SetCellDefault("Sheet1", val, s) + if err := f.SetCellDefault("Sheet1", val, s); err != nil { + panic(err) + } } } // Save xlsx file by the given path. diff --git a/picture_test.go b/picture_test.go index 6af3904..ca38f41 100644 --- a/picture_test.go +++ b/picture_test.go @@ -25,7 +25,9 @@ func BenchmarkAddPictureFromBytes(b *testing.B) { } b.ResetTimer() for i := 1; i <= b.N; i++ { - f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", i), "", "excel", ".png", imgFile) + if err := f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", i), "", "excel", ".png", imgFile); err != nil { + b.Error(err) + } } } @@ -36,23 +38,14 @@ func TestAddPicture(t *testing.T) { } // Test add picture to worksheet with offset and location hyperlink. - err = f.AddPicture("Sheet2", "I9", filepath.Join("test", "images", "excel.jpg"), - `{"x_offset": 140, "y_offset": 120, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`) - if !assert.NoError(t, err) { - t.FailNow() - } - + assert.NoError(t, f.AddPicture("Sheet2", "I9", filepath.Join("test", "images", "excel.jpg"), + `{"x_offset": 140, "y_offset": 120, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`)) // Test add picture to worksheet with offset, external hyperlink and positioning. - err = f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"), - `{"x_offset": 10, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "positioning": "oneCell"}`) - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"), + `{"x_offset": 10, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "positioning": "oneCell"}`)) file, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.png")) - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, err) // Test add picture to worksheet from bytes. assert.NoError(t, f.AddPictureFromBytes("Sheet1", "Q1", "", "Excel Logo", ".png", file)) @@ -69,9 +62,7 @@ func TestAddPicture(t *testing.T) { func TestAddPictureErrors(t *testing.T) { xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx")) - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, err) // Test add picture to worksheet with invalid file path. err = xlsx.AddPicture("Sheet1", "G21", filepath.Join("test", "not_exists_dir", "not_exists.icon"), "") @@ -127,14 +118,10 @@ func TestGetPicture(t *testing.T) { f.deleteSheetRelationships("", "") // Try to get picture from a local storage file. - if !assert.NoError(t, f.SaveAs(filepath.Join("test", "TestGetPicture.xlsx"))) { - t.FailNow() - } + assert.NoError(t, f.SaveAs(filepath.Join("test", "TestGetPicture.xlsx"))) f, err = OpenFile(filepath.Join("test", "TestGetPicture.xlsx")) - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, err) file, raw, err = f.GetPicture("Sheet1", "F21") assert.NoError(t, err) diff --git a/pivotTable_test.go b/pivotTable_test.go index 9bf08e8..5d841d8 100644 --- a/pivotTable_test.go +++ b/pivotTable_test.go @@ -16,13 +16,13 @@ func TestAddPivotTable(t *testing.T) { year := []int{2017, 2018, 2019} types := []string{"Meat", "Dairy", "Beverages", "Produce"} region := []string{"East", "West", "North", "South"} - f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "Year", "Type", "Sales", "Region"}) + assert.NoError(t, f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "Year", "Type", "Sales", "Region"})) for i := 0; i < 30; i++ { - f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i+2), month[rand.Intn(12)]) - f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i+2), year[rand.Intn(3)]) - f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i+2), types[rand.Intn(4)]) - f.SetCellValue("Sheet1", fmt.Sprintf("D%d", i+2), rand.Intn(5000)) - f.SetCellValue("Sheet1", fmt.Sprintf("E%d", i+2), region[rand.Intn(4)]) + assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i+2), month[rand.Intn(12)])) + assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i+2), year[rand.Intn(3)])) + assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i+2), types[rand.Intn(4)])) + assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("D%d", i+2), rand.Intn(5000))) + assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("E%d", i+2), region[rand.Intn(4)])) } assert.NoError(t, f.AddPivotTable(&PivotTableOption{ DataRange: "Sheet1!$A$1:$E$31", 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)) diff --git a/sheet_test.go b/sheet_test.go index aada60a..7a58248 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -192,7 +192,7 @@ func TestGetPageLayout(t *testing.T) { func TestSetHeaderFooter(t *testing.T) { f := excelize.NewFile() - f.SetCellStr("Sheet1", "A1", "Test SetHeaderFooter") + assert.NoError(t, f.SetCellStr("Sheet1", "A1", "Test SetHeaderFooter")) // 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. diff --git a/sparkline_test.go b/sparkline_test.go index a5cb216..dca32e9 100644 --- a/sparkline_test.go +++ b/sparkline_test.go @@ -17,40 +17,40 @@ func TestAddSparkline(t *testing.T) { assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "B1", style)) assert.NoError(t, f.SetSheetViewOptions("Sheet1", 0, ZoomScale(150))) - f.SetColWidth("Sheet1", "A", "A", 14) - f.SetColWidth("Sheet1", "B", "B", 50) + assert.NoError(t, f.SetColWidth("Sheet1", "A", "A", 14)) + assert.NoError(t, f.SetColWidth("Sheet1", "B", "B", 50)) // Headings - f.SetCellValue("Sheet1", "A1", "Sparkline") - f.SetCellValue("Sheet1", "B1", "Description") + assert.NoError(t, f.SetCellValue("Sheet1", "A1", "Sparkline")) + assert.NoError(t, f.SetCellValue("Sheet1", "B1", "Description")) - f.SetCellValue("Sheet1", "B2", `A default "line" sparkline.`) + assert.NoError(t, f.SetCellValue("Sheet1", "B2", `A default "line" sparkline.`)) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A2"}, Range: []string{"Sheet3!A1:J1"}, })) - f.SetCellValue("Sheet1", "B3", `A default "column" sparkline.`) + assert.NoError(t, f.SetCellValue("Sheet1", "B3", `A default "column" sparkline.`)) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A3"}, Range: []string{"Sheet3!A2:J2"}, Type: "column", })) - f.SetCellValue("Sheet1", "B4", `A default "win/loss" sparkline.`) + assert.NoError(t, f.SetCellValue("Sheet1", "B4", `A default "win/loss" sparkline.`)) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A4"}, Range: []string{"Sheet3!A3:J3"}, Type: "win_loss", })) - f.SetCellValue("Sheet1", "B6", "Line with markers.") + assert.NoError(t, f.SetCellValue("Sheet1", "B6", "Line with markers.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A6"}, Range: []string{"Sheet3!A1:J1"}, Markers: true, })) - f.SetCellValue("Sheet1", "B7", "Line with high and low points.") + assert.NoError(t, f.SetCellValue("Sheet1", "B7", "Line with high and low points.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A7"}, Range: []string{"Sheet3!A1:J1"}, @@ -58,7 +58,7 @@ func TestAddSparkline(t *testing.T) { Low: true, })) - f.SetCellValue("Sheet1", "B8", "Line with first and last point markers.") + assert.NoError(t, f.SetCellValue("Sheet1", "B8", "Line with first and last point markers.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A8"}, Range: []string{"Sheet3!A1:J1"}, @@ -66,28 +66,28 @@ func TestAddSparkline(t *testing.T) { Last: true, })) - f.SetCellValue("Sheet1", "B9", "Line with negative point markers.") + assert.NoError(t, f.SetCellValue("Sheet1", "B9", "Line with negative point markers.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A9"}, Range: []string{"Sheet3!A1:J1"}, Negative: true, })) - f.SetCellValue("Sheet1", "B10", "Line with axis.") + assert.NoError(t, f.SetCellValue("Sheet1", "B10", "Line with axis.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A10"}, Range: []string{"Sheet3!A1:J1"}, Axis: true, })) - f.SetCellValue("Sheet1", "B12", "Column with default style (1).") + assert.NoError(t, f.SetCellValue("Sheet1", "B12", "Column with default style (1).")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A12"}, Range: []string{"Sheet3!A2:J2"}, Type: "column", })) - f.SetCellValue("Sheet1", "B13", "Column with style 2.") + assert.NoError(t, f.SetCellValue("Sheet1", "B13", "Column with style 2.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A13"}, Range: []string{"Sheet3!A2:J2"}, @@ -95,7 +95,7 @@ func TestAddSparkline(t *testing.T) { Style: 2, })) - f.SetCellValue("Sheet1", "B14", "Column with style 3.") + assert.NoError(t, f.SetCellValue("Sheet1", "B14", "Column with style 3.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A14"}, Range: []string{"Sheet3!A2:J2"}, @@ -103,7 +103,7 @@ func TestAddSparkline(t *testing.T) { Style: 3, })) - f.SetCellValue("Sheet1", "B15", "Column with style 4.") + assert.NoError(t, f.SetCellValue("Sheet1", "B15", "Column with style 4.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A15"}, Range: []string{"Sheet3!A2:J2"}, @@ -111,7 +111,7 @@ func TestAddSparkline(t *testing.T) { Style: 4, })) - f.SetCellValue("Sheet1", "B16", "Column with style 5.") + assert.NoError(t, f.SetCellValue("Sheet1", "B16", "Column with style 5.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A16"}, Range: []string{"Sheet3!A2:J2"}, @@ -119,7 +119,7 @@ func TestAddSparkline(t *testing.T) { Style: 5, })) - f.SetCellValue("Sheet1", "B17", "Column with style 6.") + assert.NoError(t, f.SetCellValue("Sheet1", "B17", "Column with style 6.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A17"}, Range: []string{"Sheet3!A2:J2"}, @@ -127,7 +127,7 @@ func TestAddSparkline(t *testing.T) { Style: 6, })) - f.SetCellValue("Sheet1", "B18", "Column with a user defined color.") + assert.NoError(t, f.SetCellValue("Sheet1", "B18", "Column with a user defined color.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A18"}, Range: []string{"Sheet3!A2:J2"}, @@ -135,14 +135,14 @@ func TestAddSparkline(t *testing.T) { SeriesColor: "#E965E0", })) - f.SetCellValue("Sheet1", "B20", "A win/loss sparkline.") + assert.NoError(t, f.SetCellValue("Sheet1", "B20", "A win/loss sparkline.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A20"}, Range: []string{"Sheet3!A3:J3"}, Type: "win_loss", })) - f.SetCellValue("Sheet1", "B21", "A win/loss sparkline with negative points highlighted.") + assert.NoError(t, f.SetCellValue("Sheet1", "B21", "A win/loss sparkline with negative points highlighted.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A21"}, Range: []string{"Sheet3!A3:J3"}, @@ -150,7 +150,7 @@ func TestAddSparkline(t *testing.T) { Negative: true, })) - f.SetCellValue("Sheet1", "B23", "A left to right column (the default).") + assert.NoError(t, f.SetCellValue("Sheet1", "B23", "A left to right column (the default).")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A23"}, Range: []string{"Sheet3!A4:J4"}, @@ -158,7 +158,7 @@ func TestAddSparkline(t *testing.T) { Style: 20, })) - f.SetCellValue("Sheet1", "B24", "A right to left column.") + assert.NoError(t, f.SetCellValue("Sheet1", "B24", "A right to left column.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A24"}, Range: []string{"Sheet3!A4:J4"}, @@ -167,16 +167,16 @@ func TestAddSparkline(t *testing.T) { Reverse: true, })) - f.SetCellValue("Sheet1", "B25", "Sparkline and text in one cell.") + assert.NoError(t, f.SetCellValue("Sheet1", "B25", "Sparkline and text in one cell.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A25"}, Range: []string{"Sheet3!A4:J4"}, Type: "column", Style: 20, })) - f.SetCellValue("Sheet1", "A25", "Growth") + assert.NoError(t, f.SetCellValue("Sheet1", "A25", "Growth")) - f.SetCellValue("Sheet1", "B27", "A grouped sparkline. Changes are applied to all three.") + assert.NoError(t, f.SetCellValue("Sheet1", "B27", "A grouped sparkline. Changes are applied to all three.")) assert.NoError(t, f.AddSparkline("Sheet1", &SparklineOption{ Location: []string{"A27", "A28", "A29"}, Range: []string{"Sheet3!A5:J5", "Sheet3!A6:J6", "Sheet3!A7:J7"}, @@ -297,10 +297,14 @@ func prepareSparklineDataset() *File { f.NewSheet("Sheet2") f.NewSheet("Sheet3") for row, data := range sheet2 { - f.SetSheetRow("Sheet2", fmt.Sprintf("A%d", row+1), &data) + if err := f.SetSheetRow("Sheet2", fmt.Sprintf("A%d", row+1), &data); err != nil { + panic(err) + } } for row, data := range sheet3 { - f.SetSheetRow("Sheet3", fmt.Sprintf("A%d", row+1), &data) + if err := f.SetSheetRow("Sheet3", fmt.Sprintf("A%d", row+1), &data); err != nil { + panic(err) + } } return f } diff --git a/stream.go b/stream.go index 5e74e8e..1b1bbe3 100644 --- a/stream.go +++ b/stream.go @@ -167,7 +167,7 @@ func (sw *StreamWriter) Flush() error { sheetDataByte = append(sheetDataByte, sw.SheetData.Bytes()...) replaceMap := map[string][]byte{ - "XMLName": []byte{}, + "XMLName": {}, "SheetData": sheetDataByte, } sw.SheetData.Reset() diff --git a/table.go b/table.go index c5a704c..3d76690 100644 --- a/table.go +++ b/table.go @@ -139,7 +139,7 @@ func (f *File) addTable(sheet, tableXML string, x1, y1, x2, y2, i int, formatSet } name, _ := f.GetCellValue(sheet, cell) if _, err := strconv.Atoi(name); err == nil { - f.SetCellStr(sheet, cell, name) + _ = f.SetCellStr(sheet, cell, name) } if name == "" { name = "Column" + strconv.Itoa(idx)