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

Trying to improve documentation on behavior of xlsx subcommand #59

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,21 +562,23 @@ If the length of a cell exceeds `--max-width` it will be truncated with an ellip

### xlsx

Convert sheets of a XLSX file to CSV.
Convert sheets of an XLSX file to CSVs.

The command defaults to writing all converted sheets to a directory with the same name as the XLSX file (without the ".xlsx" extension).

Usage:

```shell
gocsv xlsx [--list-sheets] [--dirname DIRNAME] [--sheet SHEET] FILE
```none
gocsv xlsx [--list-sheets | --dirname DIRNAME | --sheet SHEET] FILE
```

Arguments:

- `--list-sheets` (optional) List the sheets in the XLSX file.
- `--sheet` (optional) Specify the sheet (by index or name) of the sheet to convert.
- `--dirname` (optional) Name of directory to output CSV conversions of sheets from `FILE`. If this is not specified, the command will output the CSV files to a directory with the same name as `FILE` (without the `.xlsx` extension).
- `--list-sheets` (optional) List the sheets, by index and name, in the XLSX file.
- `--sheet` (optional) Specify a single sheet, by index or name, to convert and write to stdout.
- `--dirname` (optional) Specify the name of the directory for the converted sheets. The command defaults to the same name as `FILE`, minus the extension.

By default the `xlsx` subcommand will convert all the sheets in `FILE` to CSVs to a directory with the same name as `FILE`.
Only one option can be used; multiple options cannot be combined.

### zip

Expand Down
24 changes: 21 additions & 3 deletions cmd/xlsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,37 @@ func (sub *XlsxSubcommand) Description() string {
return "Convert sheets of a XLSX file to CSV."
}
func (sub *XlsxSubcommand) SetFlags(fs *flag.FlagSet) {
fs.BoolVar(&sub.listSheets, "list-sheets", false, "List sheets in file")
fs.StringVar(&sub.dirname, "dirname", "", "Name of folder to output sheets to")
fs.StringVar(&sub.sheet, "sheet", "", "Name of sheet to convert")
fs.BoolVar(&sub.listSheets, "list-sheets", false, "List sheets in file by index and name")
fs.StringVar(&sub.dirname, "dirname", "", "Name of folder to write converted sheets to (defaults to file name minus \".xlsx\" extension)")
fs.StringVar(&sub.sheet, "sheet", "", "Index or name of sheet to write to stdout")
}

func (sub *XlsxSubcommand) Run(args []string) {
// Check args
if len(args) > 1 {
fmt.Fprintln(os.Stderr, "Can only convert one file")
os.Exit(1)
} else if len(args) < 1 {
fmt.Fprintln(os.Stderr, "Cannot convert file from stdin")
os.Exit(1)
}
// Check flags
flags := []string{}
if sub.listSheets {
flags = append(flags, "list-sheets")
}
if sub.dirname != "" {
flags = append(flags, "dirname")
}
if sub.sheet != "" {
flags = append(flags, "sheet")
}
if len(flags) > 1 {
fmt.Fprintf(os.Stderr, "Cannot combine flags %s\n", strings.Join(flags, ", "))
os.Exit(1)
}

// Run
filename := args[0]
if sub.listSheets {
ListXlxsSheets(filename)
Expand Down