Skip to content

Commit

Permalink
add normalizeCellValue
Browse files Browse the repository at this point in the history
  • Loading branch information
zelenin committed Oct 30, 2021
1 parent 1034edd commit cc63dec
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
36 changes: 31 additions & 5 deletions cmd/datasheet-converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"os"
"path/filepath"
"strconv"
"strings"
)

func main() {
Expand Down Expand Up @@ -47,10 +49,6 @@ func main() {
log.Fatalf("datasheet.Parse: %s", err)
}

//base := filepath.Base(file.GetPath())
//name := strings.TrimSuffix(base, filepath.Ext(base))
//csvPath := filepath.Join( outputDir, "datasheets", strings.ReplaceAll(filepath.Dir(file.GetPath()), outputDir, ""), fmt.Sprintf("%s-%s-%s.csv", ds.DataType, ds.UniqueId, name))

csvPath := filepath.Join(outputDir, "datasheets", ds.DataType, fmt.Sprintf("%s.csv", ds.UniqueId))
err = os.MkdirAll(filepath.Dir(csvPath), 0755)
if err != nil {
Expand Down Expand Up @@ -79,7 +77,7 @@ func main() {
for i, row := range ds.Rows {
record := make([]string, len(row))
for j, cell := range row {
record[j] = cell
record[j] = normalizeCellValue(ds.Columns[j], cell)
}
err = csvWriter.Write(record)
if err != nil {
Expand All @@ -101,3 +99,31 @@ func main() {
file.Close()
}
}

func normalizeCellValue(column datasheet.Column, str string) string {
if column.ColumnType == datasheet.ColumnTypeString {
return str
}

if column.ColumnType == datasheet.ColumnTypeNumber {
if strings.Index(str, ".") > 0 || strings.Index(str, "E") > 0 {
f, err := strconv.ParseFloat(str, 64)
if err != nil {
return str
}

return strconv.FormatFloat(f, 'f', -1, 64)
}
}

if column.ColumnType == datasheet.ColumnTypeBoolean {
if str == "1" {
return "true"
}
if str == "2" {
return "false"
}
}

return str
}
2 changes: 1 addition & 1 deletion datasheet/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ type DataSheet struct {
type ColumnType int32

const (
ColumnTypeFloat ColumnType = iota + 1
ColumnTypeString ColumnType = iota + 1
ColumnTypeNumber
ColumnTypeBoolean
)
Expand Down

0 comments on commit cc63dec

Please sign in to comment.