From 5ca7231ed408ac264f509ff52b5d28ff4fbda757 Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 3 Jan 2020 23:57:25 +0800 Subject: [PATCH] optimize code and comments: use println errors instead of panic --- cell_test.go | 2 +- excelize_test.go | 2 +- file_test.go | 7 +++---- cellmerged.go => merge.go | 0 cellmerged_test.go => merge_test.go | 0 rows.go | 15 ++++++++++----- rows_test.go | 1 + sheet.go | 4 ++-- sheet_test.go | 12 ++++++------ sheetpr_test.go | 8 ++++---- sheetview_test.go | 24 ++++++++++++------------ sparkline_test.go | 4 ++-- 12 files changed, 42 insertions(+), 37 deletions(-) rename cellmerged.go => merge.go (100%) rename cellmerged_test.go => merge_test.go (100%) diff --git a/cell_test.go b/cell_test.go index 1efbc5a8..60f8751a 100644 --- a/cell_test.go +++ b/cell_test.go @@ -110,7 +110,7 @@ func ExampleFile_SetCellFloat() { f := NewFile() var x = 3.14159265 if err := f.SetCellFloat("Sheet1", "A1", x, 2, 64); err != nil { - fmt.Println(err) + println(err.Error()) } val, _ := f.GetCellValue("Sheet1", "A1") fmt.Println(val) diff --git a/excelize_test.go b/excelize_test.go index ea828288..c7f5cad7 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -1259,7 +1259,7 @@ func fillCells(f *File, sheet string, colCount, rowCount int) { for row := 1; row <= rowCount; row++ { cell, _ := CoordinatesToCellName(col, row) if err := f.SetCellStr(sheet, cell, cell); err != nil { - panic(err) + println(err.Error()) } } } diff --git a/file_test.go b/file_test.go index 97ff7200..8c5050cc 100644 --- a/file_test.go +++ b/file_test.go @@ -12,18 +12,17 @@ func BenchmarkWrite(b *testing.B) { for col := 1; col <= 20; col++ { val, err := CoordinatesToCellName(col, row) if err != nil { - panic(err) + b.Error(err) } if err := f.SetCellDefault("Sheet1", val, s); err != nil { - panic(err) + b.Error(err) } } } // Save xlsx file by the given path. err := f.SaveAs("./test.xlsx") if err != nil { - panic(err) + b.Error(err) } } - } diff --git a/cellmerged.go b/merge.go similarity index 100% rename from cellmerged.go rename to merge.go diff --git a/cellmerged_test.go b/merge_test.go similarity index 100% rename from cellmerged_test.go rename to merge_test.go diff --git a/rows.go b/rows.go index d24b1a6c..40972ae5 100644 --- a/rows.go +++ b/rows.go @@ -63,7 +63,6 @@ type Rows struct { err error curRow, totalRow, stashRow int sheet string - stashColumn []string rows []xlsxRow f *File decoder *xml.Decoder @@ -111,7 +110,6 @@ func (rows *Rows) Columns() ([]string, error) { } if row > rows.curRow { rows.stashRow = row - 1 - rows.stashColumn = columns return columns, err } } @@ -153,12 +151,19 @@ func (err ErrSheetNotExist) Error() string { // Rows return a rows iterator. For example: // // rows, err := f.Rows("Sheet1") +// if err != nil { +// println(err.Error()) +// return +// } // for rows.Next() { // row, err := rows.Columns() -// for _, colCell := range row { -// fmt.Print(colCell, "\t") +// if err != nil { +// println(err.Error()) // } -// fmt.Println() +// for _, colCell := range row { +// print(colCell, "\t") +// } +// println() // } // func (f *File) Rows(sheet string) (*Rows, error) { diff --git a/rows_test.go b/rows_test.go index 1127bb14..9377d5e3 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 diff --git a/sheet.go b/sheet.go index 2654b8f9..19b90c63 100644 --- a/sheet.go +++ b/sheet.go @@ -339,12 +339,12 @@ func (f *File) GetSheetIndex(name string) int { // GetSheetMap provides a function to get worksheet name and index map of XLSX. // For example: // -// f, err := excelize.OpenFile("./Book1.xlsx") +// f, err := excelize.OpenFile("Book1.xlsx") // if err != nil { // return // } // for index, name := range f.GetSheetMap() { -// fmt.Println(index, name) +// println(index, name) // } // func (f *File) GetSheetMap() map[int]string { diff --git a/sheet_test.go b/sheet_test.go index 7a582486..a03066a7 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -19,7 +19,7 @@ func ExampleFile_SetPageLayout() { "Sheet1", excelize.PageLayoutOrientation(excelize.OrientationLandscape), ); err != nil { - panic(err) + println(err.Error()) } if err := f.SetPageLayout( "Sheet1", @@ -27,7 +27,7 @@ func ExampleFile_SetPageLayout() { excelize.FitToHeight(2), excelize.FitToWidth(2), ); err != nil { - panic(err) + println(err.Error()) } // Output: } @@ -41,17 +41,17 @@ func ExampleFile_GetPageLayout() { fitToWidth excelize.FitToWidth ) if err := f.GetPageLayout("Sheet1", &orientation); err != nil { - panic(err) + println(err.Error()) } if err := f.GetPageLayout("Sheet1", &paperSize); err != nil { - panic(err) + println(err.Error()) } if err := f.GetPageLayout("Sheet1", &fitToHeight); err != nil { - panic(err) + println(err.Error()) } if err := f.GetPageLayout("Sheet1", &fitToWidth); err != nil { - panic(err) + println(err.Error()) } fmt.Println("Defaults:") fmt.Printf("- orientation: %q\n", orientation) diff --git a/sheetpr_test.go b/sheetpr_test.go index d1ae2f13..6a35a6ec 100644 --- a/sheetpr_test.go +++ b/sheetpr_test.go @@ -40,7 +40,7 @@ func ExampleFile_SetSheetPrOptions() { excelize.AutoPageBreaks(true), excelize.OutlineSummaryBelow(false), ); err != nil { - panic(err) + println(err.Error()) } // Output: } @@ -66,7 +66,7 @@ func ExampleFile_GetSheetPrOptions() { &autoPageBreaks, &outlineSummaryBelow, ); err != nil { - panic(err) + println(err.Error()) } fmt.Println("Defaults:") fmt.Printf("- codeName: %q\n", codeName) @@ -189,7 +189,7 @@ func ExampleFile_SetPageMargins() { excelize.PageMarginRight(1.0), excelize.PageMarginTop(1.0), ); err != nil { - panic(err) + println(err.Error()) } // Output: } @@ -215,7 +215,7 @@ func ExampleFile_GetPageMargins() { &marginRight, &marginTop, ); err != nil { - panic(err) + println(err.Error()) } fmt.Println("Defaults:") fmt.Println("- marginBottom:", marginBottom) diff --git a/sheetview_test.go b/sheetview_test.go index e45b8cec..84120026 100644 --- a/sheetview_test.go +++ b/sheetview_test.go @@ -47,7 +47,7 @@ func ExampleFile_SetSheetViewOptions() { excelize.ZoomScale(80), excelize.TopLeftCell("C3"), ); err != nil { - panic(err) + println(err.Error()) } var zoomScale excelize.ZoomScale @@ -55,22 +55,22 @@ func ExampleFile_SetSheetViewOptions() { fmt.Println("- zoomScale: 80") if err := f.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(500)); err != nil { - panic(err) + println(err.Error()) } if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil { - panic(err) + println(err.Error()) } fmt.Println("Used out of range value:") fmt.Println("- zoomScale:", zoomScale) if err := f.SetSheetViewOptions(sheet, 0, excelize.ZoomScale(123)); err != nil { - panic(err) + println(err.Error()) } if err := f.GetSheetViewOptions(sheet, 0, &zoomScale); err != nil { - panic(err) + println(err.Error()) } fmt.Println("Used correct value:") @@ -111,7 +111,7 @@ func ExampleFile_GetSheetViewOptions() { &zoomScale, &topLeftCell, ); err != nil { - panic(err) + println(err.Error()) } fmt.Println("Default:") @@ -125,27 +125,27 @@ func ExampleFile_GetSheetViewOptions() { fmt.Println("- topLeftCell:", `"`+topLeftCell+`"`) if err := f.SetSheetViewOptions(sheet, 0, excelize.TopLeftCell("B2")); err != nil { - panic(err) + println(err.Error()) } if err := f.GetSheetViewOptions(sheet, 0, &topLeftCell); err != nil { - panic(err) + println(err.Error()) } if err := f.SetSheetViewOptions(sheet, 0, excelize.ShowGridLines(false)); err != nil { - panic(err) + println(err.Error()) } if err := f.GetSheetViewOptions(sheet, 0, &showGridLines); err != nil { - panic(err) + println(err.Error()) } if err := f.SetSheetViewOptions(sheet, 0, excelize.ShowZeros(false)); err != nil { - panic(err) + println(err.Error()) } if err := f.GetSheetViewOptions(sheet, 0, &showZeros); err != nil { - panic(err) + println(err.Error()) } fmt.Println("After change:") diff --git a/sparkline_test.go b/sparkline_test.go index dca32e9c..45bf386e 100644 --- a/sparkline_test.go +++ b/sparkline_test.go @@ -298,12 +298,12 @@ func prepareSparklineDataset() *File { f.NewSheet("Sheet3") for row, data := range sheet2 { if err := f.SetSheetRow("Sheet2", fmt.Sprintf("A%d", row+1), &data); err != nil { - panic(err) + println(err.Error()) } } for row, data := range sheet3 { if err := f.SetSheetRow("Sheet3", fmt.Sprintf("A%d", row+1), &data); err != nil { - panic(err) + println(err.Error()) } } return f