Skip to content

Commit afb2d27

Browse files
authored
This fix formula calculation accuracy issue and panic when set pane
- Fix `GROWTH` and `TREND` calculation accuracy issue - Fix panic when add pane on empty sheet views worksheet - New exported constants `MinFontSize`
1 parent 63adac2 commit afb2d27

File tree

7 files changed

+19
-10
lines changed

7 files changed

+19
-10
lines changed

calc.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4579,8 +4579,7 @@ func newFormulaArgMatrix(numMtx [][]float64) (arg [][]formulaArg) {
45794579
for r, row := range numMtx {
45804580
arg = append(arg, make([]formulaArg, len(row)))
45814581
for c, cell := range row {
4582-
decimal, _ := big.NewFloat(cell).SetPrec(15).Float64()
4583-
arg[r][c] = newNumberFormulaArg(decimal)
4582+
arg[r][c] = newNumberFormulaArg(cell)
45844583
}
45854584
}
45864585
return

calc_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4518,17 +4518,17 @@ func TestCalcGROWTHandTREND(t *testing.T) {
45184518
formulaList := map[string]string{
45194519
"=GROWTH(A2:B2)": "1",
45204520
"=GROWTH(B2:B5,A2:A5,A8:A10)": "160",
4521-
"=GROWTH(B2:B5,A2:A5,A8:A10,FALSE)": "467.84375",
4521+
"=GROWTH(B2:B5,A2:A5,A8:A10,FALSE)": "467.842838114059",
45224522
"=GROWTH(A4:A5,A2:B3,A8:A10,FALSE)": "",
45234523
"=GROWTH(A3:A5,A2:B4,A2:B3)": "2",
45244524
"=GROWTH(A4:A5,A2:B3)": "",
45254525
"=GROWTH(A2:B2,A2:B3)": "",
4526-
"=GROWTH(A2:B2,A2:B3,A2:B3,FALSE)": "1.28399658203125",
4526+
"=GROWTH(A2:B2,A2:B3,A2:B3,FALSE)": "1.28402541668774",
45274527
"=GROWTH(A2:B2,A4:B5,A4:B5,FALSE)": "1",
45284528
"=GROWTH(A3:C3,A2:C3,A2:B3)": "2",
45294529
"=TREND(A2:B2)": "1",
45304530
"=TREND(B2:B5,A2:A5,A8:A10)": "95",
4531-
"=TREND(B2:B5,A2:A5,A8:A10,FALSE)": "81.66796875",
4531+
"=TREND(B2:B5,A2:A5,A8:A10,FALSE)": "81.6666666666667",
45324532
"=TREND(A4:A5,A2:B3,A8:A10,FALSE)": "",
45334533
"=TREND(A4:A5,A2:B3,A2:B3,FALSE)": "1.5",
45344534
"=TREND(A3:A5,A2:B4,A2:B3)": "2",

errors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ var (
102102
ErrMaxRows = errors.New("row number exceeds maximum limit")
103103
// ErrMaxRowHeight defined the error message on receive an invalid row
104104
// height.
105-
ErrMaxRowHeight = errors.New("the height of the row must be smaller than or equal to 409 points")
105+
ErrMaxRowHeight = fmt.Errorf("the height of the row must be smaller than or equal to %d points", MaxRowHeight)
106106
// ErrImgExt defined the error message on receive an unsupported image
107107
// extension.
108108
ErrImgExt = errors.New("unsupported image extension")
@@ -145,9 +145,9 @@ var (
145145
ErrCustomNumFmt = errors.New("custom number format can not be empty")
146146
// ErrFontLength defined the error message on the length of the font
147147
// family name overflow.
148-
ErrFontLength = errors.New("the length of the font family name must be smaller than or equal to 31")
148+
ErrFontLength = fmt.Errorf("the length of the font family name must be smaller than or equal to %d", MaxFontFamilyLength)
149149
// ErrFontSize defined the error message on the size of the font is invalid.
150-
ErrFontSize = errors.New("font size must be between 1 and 409 points")
150+
ErrFontSize = fmt.Errorf("font size must be between %d and %d points", MinFontSize, MaxFontSize)
151151
// ErrSheetIdx defined the error message on receive the invalid worksheet
152152
// index.
153153
ErrSheetIdx = errors.New("invalid worksheet index")
@@ -161,7 +161,7 @@ var (
161161
ErrGroupSheets = errors.New("group worksheet must contain an active worksheet")
162162
// ErrDataValidationFormulaLength defined the error message for receiving a
163163
// data validation formula length that exceeds the limit.
164-
ErrDataValidationFormulaLength = errors.New("data validation must be 0-255 characters")
164+
ErrDataValidationFormulaLength = fmt.Errorf("data validation must be 0-%d characters", MaxFieldLength)
165165
// ErrDataValidationRange defined the error message on set decimal range
166166
// exceeds limit.
167167
ErrDataValidationRange = errors.New("data validation range exceeds limit")

sheet.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,9 @@ func (f *File) SetPanes(sheet, panes string) error {
773773
if fs.Freeze {
774774
p.State = "frozen"
775775
}
776+
if ws.SheetViews == nil {
777+
ws.SheetViews = &xlsxSheetViews{SheetView: []xlsxSheetView{{}}}
778+
}
776779
ws.SheetViews.SheetView[len(ws.SheetViews.SheetView)-1].Pane = p
777780
if !(fs.Freeze) && !(fs.Split) {
778781
if len(ws.SheetViews.SheetView) > 0 {

sheet_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ func TestSetPane(t *testing.T) {
104104
assert.NoError(t, f.SetPanes("Panes 4", ""))
105105
assert.EqualError(t, f.SetPanes("SheetN", ""), "sheet SheetN is not exist")
106106
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetPane.xlsx")))
107+
// Test add pane on empty sheet views worksheet
108+
f = NewFile()
109+
f.checked = nil
110+
f.Sheet.Delete("xl/worksheets/sheet1.xml")
111+
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(`<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData/></worksheet>`))
112+
assert.NoError(t, f.SetPanes("Sheet1", `{"freeze":true,"split":false,"x_split":1,"y_split":0,"top_left_cell":"B1","active_pane":"topRight","panes":[{"sqref":"K16","active_cell":"K16","pane":"topRight"}]}`))
107113
}
108114

109115
func TestPageLayoutOption(t *testing.T) {

styles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ func (f *File) getFontID(styleSheet *xlsxStyleSheet, style *Style) (fontID int)
20422042
// settings.
20432043
func (f *File) newFont(style *Style) *xlsxFont {
20442044
fontUnderlineType := map[string]string{"single": "single", "double": "double"}
2045-
if style.Font.Size < 1 {
2045+
if style.Font.Size < MinFontSize {
20462046
style.Font.Size = 11
20472047
}
20482048
if style.Font.Color == "" {

xmlDrawing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const (
107107
MaxFieldLength = 255
108108
MaxColumnWidth = 255
109109
MaxRowHeight = 409
110+
MinFontSize = 1
110111
TotalRows = 1048576
111112
TotalColumns = 16384
112113
TotalSheetHyperlinks = 65529

0 commit comments

Comments
 (0)