Skip to content

Commit

Permalink
splitxlsx: fix the error of invalid worksheet index. github.com/qax-o…
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Aug 18, 2023
1 parent 8b39184 commit 3af5499
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
@@ -1,8 +1,12 @@
- [csvtk v0.27.2](https://github.com/shenwei356/csvtk/releases/tag/v0.27.2)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.27.2/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.27.2)
- `csvtk pretty`:
- fix bug of empty first row with `-H/--no-header-row`, introduced in v0.27.0.
- fix the bug of empty first row with `-H/--no-header-row`, introduced in v0.27.0.
- new style `3line` for three-line table.
- `csvtk csv2xlsx`:
- binaries compiled with go1.21 would result in a broken xlsx file. [#243](https://github.com/shenwei356/csvtk/issues/243)
- `csvtk splitxlsx`:
- fix the error of `invalid worksheet index`. [#1617](https://github.com/qax-os/excelize/issues/1617)
- [csvtk v0.27.1](https://github.com/shenwei356/csvtk/releases/tag/v0.27.1)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/csvtk/v0.27.1/total.svg)](https://github.com/shenwei356/csvtk/releases/tag/v0.27.1)
- `csvtk filter2/mutate2`:
Expand Down
17 changes: 13 additions & 4 deletions csvtk/cmd/csv2xlsx.go
Expand Up @@ -70,7 +70,8 @@ Attention:
var col, line int
var valFloat float64
var nSheets int
for _, file := range files {
var idx, firstIdx int
for i, file := range files {
csvReader, err := newCSVReaderByConfig(config, file)
if err != nil {
if err == xopen.ErrNoContent {
Expand All @@ -88,12 +89,20 @@ Attention:

if singleInput {
sheet = "Sheet1"

firstIdx, err = xlsx.GetSheetIndex(sheet)
checkError(err)
} else {
sheet, _ = filepathTrimExtension(filepath.Base(file))
if nSheets == 1 {
xlsx.SetSheetName("Sheet1", sheet)
checkError(xlsx.SetSheetName("Sheet1", sheet))
} else {
xlsx.NewSheet(sheet)
idx, err = xlsx.NewSheet(sheet)
checkError(err)

if i == 0 {
firstIdx = idx
}
}
}

Expand Down Expand Up @@ -132,7 +141,7 @@ Attention:
readerReport(&config, csvReader, file)
}

xlsx.SetActiveSheet(1)
xlsx.SetActiveSheet(firstIdx)
checkError(xlsx.SaveAs(outFile))
},
}
Expand Down
13 changes: 9 additions & 4 deletions csvtk/cmd/splitxlsx.go
Expand Up @@ -260,22 +260,27 @@ Weakness : Complicated sheet structures are not well supported, e.g.,
Keys2RowIndex[key][rowIndex] = struct{}{}
}

from, err := xlsx.GetSheetIndex(sheetName)
checkError(err)

for _, key := range keysList {
index, err := xlsx.NewSheet(key)
// https://github.com/qax-os/excelize/issues/1617
to, err := xlsx.NewSheet(key)
checkError(err)
checkError(xlsx.CopySheet(sheetName2Index[sheetName], index))
checkError(xlsx.CopySheet(from, to))

for i := len(rows) - 1; i >= 0; i-- {
if i == 0 && needParseHeaderRow {
continue
}
if _, ok = Keys2RowIndex[key][i]; !ok {
xlsx.RemoveRow(key, i)
xlsx.RemoveRow(key, i+1)
}
}
}
log.Infof("Sheet %s is split into %d sheets", sheetName, len(keysList))

xlsx.SetActiveSheet(sheetName2Index[sheetName])
xlsx.SetActiveSheet(from)
if config.OutFile == "-" {
prefx, _ := filepathTrimExtension(files[0])
config.OutFile = fmt.Sprintf("%s.split.xlsx", prefx)
Expand Down

0 comments on commit 3af5499

Please sign in to comment.