This closes #714 and closes #715, fix wrong worksheet index returned by NewSheet in some case, fix panic on formatted value with no built-in number format ID

This commit is contained in:
xuri 2020-10-19 23:55:54 +08:00
parent 520aa679f3
commit 4834a058aa
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
4 changed files with 18 additions and 2 deletions

View File

@ -770,6 +770,9 @@ func (f *File) formattedValue(s int, v string) string {
if ok != nil { if ok != nil {
return ok(v, builtInNumFmt[numFmtId]) return ok(v, builtInNumFmt[numFmtId])
} }
if styleSheet == nil || styleSheet.NumFmts == nil {
return v
}
for _, xlsxFmt := range styleSheet.NumFmts.NumFmt { for _, xlsxFmt := range styleSheet.NumFmts.NumFmt {
if xlsxFmt.NumFmtID == numFmtId { if xlsxFmt.NumFmtID == numFmtId {
format := strings.ToLower(xlsxFmt.FormatCode) format := strings.ToLower(xlsxFmt.FormatCode)

View File

@ -299,4 +299,14 @@ func TestFormattedValue(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
v = f.formattedValue(1, "43528") v = f.formattedValue(1, "43528")
assert.Equal(t, "03/04/2019", v) assert.Equal(t, "03/04/2019", v)
// formatted value with no built-in number format ID
assert.NoError(t, err)
f.Styles.NumFmts = nil
numFmtID := 5
f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{
NumFmtID: &numFmtID,
})
v = f.formattedValue(1, "43528")
assert.Equal(t, "43528", v)
} }

View File

@ -37,8 +37,9 @@ import (
// appending the new sheet. // appending the new sheet.
func (f *File) NewSheet(name string) int { func (f *File) NewSheet(name string) int {
// Check if the worksheet already exists // Check if the worksheet already exists
if f.GetSheetIndex(name) != -1 { index := f.GetSheetIndex(name)
return f.SheetCount if index != -1 {
return index
} }
f.DeleteSheet(name) f.DeleteSheet(name)
f.SheetCount++ f.SheetCount++

View File

@ -71,6 +71,8 @@ func TestNewSheet(t *testing.T) {
// delete original sheet // delete original sheet
f.DeleteSheet(f.GetSheetName(f.GetSheetIndex("Sheet1"))) f.DeleteSheet(f.GetSheetName(f.GetSheetIndex("Sheet1")))
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestNewSheet.xlsx"))) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestNewSheet.xlsx")))
// create new worksheet with already exists name
assert.Equal(t, f.GetSheetIndex("Sheet2"), f.NewSheet("Sheet2"))
} }
func TestSetPane(t *testing.T) { func TestSetPane(t *testing.T) {