Skip to content

Commit

Permalink
Merge pull request qax-os#103 from takayuki/trim-sheet-name-as-rune
Browse files Browse the repository at this point in the history
Conut and trim sheet name in UTF-8
  • Loading branch information
xuri authored Aug 13, 2017
2 parents 845e339 + 02728de commit 58e2caf
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"strconv"
"strings"
"unicode/utf8"
)

// NewSheet provides function to create a new sheet by given index, when
Expand Down Expand Up @@ -121,11 +122,7 @@ func (f *File) setSheet(index int) {
// setWorkbook update workbook property of XLSX. Maximum 31 characters are
// allowed in sheet title.
func (f *File) setWorkbook(name string, rid int) {
r := strings.NewReplacer(":", "", "\\", "", "/", "", "?", "", "*", "", "[", "", "]", "")
name = r.Replace(name)
if len(name) > 31 {
name = name[0:31]
}
name = trimSheetName(name)
content := f.workbookReader()
content.Sheets.Sheet = append(content.Sheets.Sheet, xlsxSheet{
Name: name,
Expand Down Expand Up @@ -646,8 +643,8 @@ func (f *File) GetSheetVisible(name string) bool {
func trimSheetName(name string) string {
r := strings.NewReplacer(":", "", "\\", "", "/", "", "?", "", "*", "", "[", "", "]", "")
name = r.Replace(name)
if len(name) > 31 {
name = name[0:31]
if utf8.RuneCountInString(name) > 31 {
name = string([]rune(name)[0:31])
}
return name
}

0 comments on commit 58e2caf

Please sign in to comment.