Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import XLSX add error log #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions backends/csv/xlsx2csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
stdcsv "encoding/csv"
"errors"
"fmt"
"io"
"io/ioutil"
"path/filepath"
Expand All @@ -25,28 +26,40 @@ func generateCSVFromXLSXFile(fileName string) (io.ReadCloser, error) {
csvWriter := stdcsv.NewWriter(&buf)

var firstRowSize int
for _, row := range sheet.Rows {
err = sheet.ForEachRow(func(row *xlsx.Row) error {
if row.Hidden {
continue
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So row.Hidden now will abort the process?

}
if len(row.Cells) == 0 {
continue

var record []string
row.ForEachCell(func(cell *xlsx.Cell) error {
record = append(record, cell.Value)
return nil
})

if len(record) == 0 {
return nil
}

if firstRowSize == 0 {
firstRowSize = len(row.Cells)
firstRowSize = len(record)
}

var record = make([]string, firstRowSize)
for i, cell := range row.Cells {
record[i] = cell.Value
if firstRowSize != len(record) {
return errors.New(fmt.Sprintf("This XLSX file data is invalid,Header length:%d,This row length:%d,This row data:%s", firstRowSize, len(record), record))
}

err = csvWriter.Write(record)
if err != nil {
return nil, err
return err
}
return nil
})

if err != nil {
return nil, err
}

csvWriter.Flush()
err = csvWriter.Error()
if err != nil {
Expand Down