Skip to content

Commit

Permalink
Merge pull request #12 from noborus/writer-check
Browse files Browse the repository at this point in the history
Added error checking
  • Loading branch information
noborus authored Nov 20, 2023
2 parents c9c8a92 + 2462fec commit b28eb1c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion writer.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package xlsxsql

import (
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/xuri/excelize/v2"
)

var ErrInvalidFileName = errors.New("file name must end with .xlsx")

// XLSXWriter is a writer for XLSX files.
type XLSXWriter struct {
fileName string
Expand Down Expand Up @@ -131,6 +136,12 @@ func NewXLSXWriter(options ...WriteOpt) (*XLSXWriter, error) {
func openXLSXFile(fileName string) (*excelize.File, error) {
var f *excelize.File
var err error

// Check if file name ends with .xlsx
if !strings.HasSuffix(fileName, ".xlsx") {
return nil, fmt.Errorf("%w: [%s]", ErrInvalidFileName, fileName)
}

if _, err = os.Stat(fileName); err != nil && !os.IsNotExist(err) {
return nil, err
}
Expand Down Expand Up @@ -171,7 +182,9 @@ func clearSheet(f *excelize.File, sheet string) error {
if err != nil {
return err
}
f.SetCellStr(sheet, axis, "")
if err := f.SetCellStr(sheet, axis, ""); err != nil {
return err
}
}
}
return nil
Expand Down

0 comments on commit b28eb1c

Please sign in to comment.