diff --git a/README.md b/README.md index b162ee0..f5b4c8d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/xlsx.go b/cmd/xlsx.go index aa83267..1f56b01 100644 --- a/cmd/xlsx.go +++ b/cmd/xlsx.go @@ -26,12 +26,13 @@ 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) @@ -39,6 +40,23 @@ func (sub *XlsxSubcommand) Run(args []string) { 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)