Skip to content

Commit

Permalink
csv2xlsx: add -f: save numbers in number format, instead of text. #217
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Jan 30, 2023
1 parent 2bd4188 commit 4585d4e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- near all commands skip empty file now. [#204](https://github.com/shenwei356/csvtk/issues/204)
- the global flag `--infile-list` accepts stdin "-". [#210](https://github.com/shenwei356/csvtk/issues/210)
- `csvtk cut/fmtdate/freq/grep/rename/rename2/replace/round`: allow duplicated column names.
- `csvtk csv2xlsx`: optionally stores numbers as float. [#217](https://github.com/shenwei356/csvtk/issues/217)
- `csvtk xlsx2csv`: fix bug wheree `xlsx2csv` treats small number (padj < 1e-25) as 0. It's solved by updating the excelize package. [#261](https://github.com/shenwei356/csvtk/issues/201)
- `csvtk join`: new flag for adding filename as column name prefix. by @tetedange13 [#202](https://github.com/shenwei356/csvtk/issues/202)
- [csvtk v0.25.0](https://github.com/shenwei356/csvtk/releases/tag/v0.25.0)
Expand Down
16 changes: 12 additions & 4 deletions csvtk/cmd/csv2xlsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Attention:
config := getConfigs(cmd)
files := getFileListFromArgsAndFile(cmd, args, true, "infile-list", true)

formatNumbers := getFlagBool(cmd, "format-numbers")

runtime.GOMAXPROCS(config.NumCPUs)

singleInput := len(files) == 1
Expand Down Expand Up @@ -104,11 +106,15 @@ Attention:
for _, record = range chunk.Data {
for col, val = range record {
cell = fmt.Sprintf("%s%d", ExcelColumnIndex(col), line)
valFloat, err = strconv.ParseFloat(val, 64)
if err != nil {
xlsx.SetCellValue(sheet, cell, val)
if formatNumbers {
valFloat, err = strconv.ParseFloat(val, 64)
if err != nil {
xlsx.SetCellValue(sheet, cell, val)
} else {
xlsx.SetCellFloat(sheet, cell, valFloat, -1, 64)
}
} else {
xlsx.SetCellFloat(sheet, cell, valFloat, -1, 64)
xlsx.SetCellValue(sheet, cell, val)
}
}
line++
Expand All @@ -127,6 +133,8 @@ Attention:
func init() {
RootCmd.AddCommand(csv2xlsxCmd)

csv2xlsxCmd.Flags().BoolP("format-numbers", "f", false, `save numbers in number format, instead of text`)

}

func ExcelColumnIndex(col int) string {
Expand Down
1 change: 1 addition & 0 deletions doc/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Usage:
csvtk csv2xlsx [flags]
Flags:
-f, --format-numbers save numbers in number format, instead of text
-h, --help help for csv2xlsx
```
Expand Down

0 comments on commit 4585d4e

Please sign in to comment.