optimize code and comments: use println errors instead of panic

This commit is contained in:
xuri 2020-01-03 23:57:25 +08:00
parent 5f5ec76740
commit 5ca7231ed4
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
12 changed files with 42 additions and 37 deletions

View File

@ -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)

View File

@ -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())
}
}
}

View File

@ -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)
}
}
}

15
rows.go
View File

@ -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) {

View File

@ -143,6 +143,7 @@ func TestColumns(t *testing.T) {
rows.decoder = f.xmlNewDecoder(bytes.NewReader([]byte(`<worksheet><sheetData><row r="2"><c r="A1" t="s"><v>1</v></c></row></sheetData></worksheet>`)))
rows.curRow = 1
_, err = rows.Columns()
assert.NoError(t, err)
rows.decoder = f.xmlNewDecoder(bytes.NewReader([]byte(`<worksheet><sheetData><row r="A"><c r="A1" t="s"><v>1</v></c></row><row r="A"><c r="2" t="str"><v>B</v></c></row></sheetData></worksheet>`)))
rows.stashRow, rows.curRow = 0, 1

View File

@ -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 {

View File

@ -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)

View File

@ -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)

View File

@ -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:")

View File

@ -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