This closes #1027 and closes #1028

* Fix build-in scientific number format failed
* An error will be returned if given an invalid custom number format when creating a new style
This commit is contained in:
xuri 2021-09-28 22:02:31 +08:00
parent 490f3063c2
commit 2d8b5b1885
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 20 additions and 3 deletions

View File

@ -112,6 +112,8 @@ var (
// ErrDefinedNameduplicate defined the error message on the same name // ErrDefinedNameduplicate defined the error message on the same name
// already exists on the scope. // already exists on the scope.
ErrDefinedNameduplicate = errors.New("the same name already exists on the scope") ErrDefinedNameduplicate = errors.New("the same name already exists on the scope")
// ErrCustomNumFmt defined the error message on receive the empty parameter.
ErrCustomNumFmt = errors.New("custom number format can not be empty")
// ErrFontLength defined the error message on the length of the font // ErrFontLength defined the error message on the length of the font
// family name overflow. // family name overflow.
ErrFontLength = errors.New("the length of the font family name must be smaller than or equal to 31") ErrFontLength = errors.New("the length of the font family name must be smaller than or equal to 31")

View File

@ -930,7 +930,7 @@ func formatToE(v string, format string) string {
if err != nil { if err != nil {
return v return v
} }
return fmt.Sprintf("%.e", f) return fmt.Sprintf("%.2E", f)
} }
// parseTime provides a function to returns a string parsed using time.Time. // parseTime provides a function to returns a string parsed using time.Time.
@ -1115,6 +1115,9 @@ func parseFormatStyleSet(style interface{}) (*Style, error) {
return &fs, ErrFontSize return &fs, ErrFontSize
} }
} }
if fs.CustomNumFmt != nil && len(*fs.CustomNumFmt) == 0 {
err = ErrCustomNumFmt
}
return &fs, err return &fs, err
} }

View File

@ -203,10 +203,13 @@ func TestNewStyle(t *testing.T) {
_, err = f.NewStyle(Style{}) _, err = f.NewStyle(Style{})
assert.EqualError(t, err, ErrParameterInvalid.Error()) assert.EqualError(t, err, ErrParameterInvalid.Error())
var exp string
_, err = f.NewStyle(&Style{CustomNumFmt: &exp})
assert.EqualError(t, err, ErrCustomNumFmt.Error())
_, err = f.NewStyle(&Style{Font: &Font{Family: strings.Repeat("s", MaxFontFamilyLength+1)}}) _, err = f.NewStyle(&Style{Font: &Font{Family: strings.Repeat("s", MaxFontFamilyLength+1)}})
assert.EqualError(t, err, "the length of the font family name must be smaller than or equal to 31") assert.EqualError(t, err, ErrFontLength.Error())
_, err = f.NewStyle(&Style{Font: &Font{Size: MaxFontSize + 1}}) _, err = f.NewStyle(&Style{Font: &Font{Size: MaxFontSize + 1}})
assert.EqualError(t, err, "font size must be between 1 and 409 points") assert.EqualError(t, err, ErrFontSize.Error())
// new numeric custom style // new numeric custom style
fmt := "####;####" fmt := "####;####"
@ -240,6 +243,15 @@ func TestNewStyle(t *testing.T) {
nf = f.Styles.CellXfs.Xf[styleID] nf = f.Styles.CellXfs.Xf[styleID]
assert.Equal(t, 32, *nf.NumFmtID) assert.Equal(t, 32, *nf.NumFmtID)
// Test set build-in scientific number format
styleID, err = f.NewStyle(&Style{NumFmt: 11})
assert.NoError(t, err)
assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "B1", styleID))
assert.NoError(t, f.SetSheetRow("Sheet1", "A1", &[]float64{1.23, 1.234}))
rows, err := f.GetRows("Sheet1")
assert.NoError(t, err)
assert.Equal(t, [][]string{{"1.23E+00", "1.23E+00"}}, rows)
} }
func TestGetDefaultFont(t *testing.T) { func TestGetDefaultFont(t *testing.T) {