This closes #1323, an error will be returned when set the not exist style ID

This commit is contained in:
xuri 2022-09-01 00:41:52 +08:00
parent 18cd63a548
commit 75ce231728
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
9 changed files with 71 additions and 17 deletions

10
calc.go
View File

@ -6037,7 +6037,7 @@ func getBetaHelperContFrac(fX, fA, fB float64) float64 {
bfinished = math.Abs(cf-cfnew) < math.Abs(cf)*fMachEps bfinished = math.Abs(cf-cfnew) < math.Abs(cf)*fMachEps
} }
cf = cfnew cf = cfnew
rm += 1 rm++
} }
return cf return cf
} }
@ -6914,7 +6914,7 @@ func (fn *formulaFuncs) CHIDIST(argsList *list.List) formulaArg {
for z <= x1 { for z <= x1 {
e = math.Log(z) + e e = math.Log(z) + e
s += math.Exp(c*z - a - e) s += math.Exp(c*z - a - e)
z += 1 z++
} }
return newNumberFormulaArg(s) return newNumberFormulaArg(s)
} }
@ -6926,7 +6926,7 @@ func (fn *formulaFuncs) CHIDIST(argsList *list.List) formulaArg {
for z <= x1 { for z <= x1 {
e = e * (a / z) e = e * (a / z)
c = c + e c = c + e
z += 1 z++
} }
return newNumberFormulaArg(c*y + s) return newNumberFormulaArg(c*y + s)
} }
@ -15286,10 +15286,10 @@ func (fn *formulaFuncs) coupons(name string, arg formulaArg) formulaArg {
month -= coupon month -= coupon
} }
if month > 11 { if month > 11 {
year += 1 year++
month -= 12 month -= 12
} else if month < 0 { } else if month < 0 {
year -= 1 year--
month += 12 month += 12
} }
day, lastDay := maturity.Day(), time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) day, lastDay := maturity.Day(), time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)

7
col.go
View File

@ -415,6 +415,13 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
if err != nil { if err != nil {
return err return err
} }
s := f.stylesReader()
s.Lock()
if styleID < 0 || s.CellXfs == nil || len(s.CellXfs.Xf) <= styleID {
s.Unlock()
return newInvalidStyleID(styleID)
}
s.Unlock()
ws, err := f.workSheetReader(sheet) ws, err := f.workSheetReader(sheet)
if err != nil { if err != nil {
return err return err

View File

@ -296,6 +296,10 @@ func TestSetColStyle(t *testing.T) {
// Test set column style with illegal cell coordinates. // Test set column style with illegal cell coordinates.
assert.EqualError(t, f.SetColStyle("Sheet1", "*", styleID), newInvalidColumnNameError("*").Error()) assert.EqualError(t, f.SetColStyle("Sheet1", "*", styleID), newInvalidColumnNameError("*").Error())
assert.EqualError(t, f.SetColStyle("Sheet1", "A:*", styleID), newInvalidColumnNameError("*").Error()) assert.EqualError(t, f.SetColStyle("Sheet1", "A:*", styleID), newInvalidColumnNameError("*").Error())
// Test set column style with invalid style ID.
assert.EqualError(t, f.SetColStyle("Sheet1", "B", -1), newInvalidStyleID(-1).Error())
// Test set column style with not exists style ID.
assert.EqualError(t, f.SetColStyle("Sheet1", "B", 10), newInvalidStyleID(10).Error())
assert.NoError(t, f.SetColStyle("Sheet1", "B", styleID)) assert.NoError(t, f.SetColStyle("Sheet1", "B", styleID))
// Test set column style with already exists column with style. // Test set column style with already exists column with style.

View File

@ -55,7 +55,7 @@ func newUnzipSizeLimitError(unzipSizeLimit int64) error {
// newInvalidStyleID defined the error message on receiving the invalid style // newInvalidStyleID defined the error message on receiving the invalid style
// ID. // ID.
func newInvalidStyleID(styleID int) error { func newInvalidStyleID(styleID int) error {
return fmt.Errorf("invalid style ID %d, negative values are not supported", styleID) return fmt.Errorf("invalid style ID %d", styleID)
} }
// newFieldLengthError defined the error message on receiving the field length // newFieldLengthError defined the error message on receiving the field length

View File

@ -32,13 +32,22 @@ func TestOpenFile(t *testing.T) {
assert.EqualError(t, err, "sheet Sheet4 does not exist") assert.EqualError(t, err, "sheet Sheet4 does not exist")
// Test get all the rows in a worksheet. // Test get all the rows in a worksheet.
rows, err := f.GetRows("Sheet2") rows, err := f.GetRows("Sheet2")
expected := [][]string{
{"Monitor", "", "Brand", "", "inlineStr"},
{"> 23 Inch", "19", "HP", "200"},
{"20-23 Inch", "24", "DELL", "450"},
{"17-20 Inch", "56", "Lenove", "200"},
{"< 17 Inch", "21", "SONY", "510"},
{"", "", "Acer", "315"},
{"", "", "IBM", "127"},
{"", "", "ASUS", "89"},
{"", "", "Apple", "348"},
{"", "", "SAMSUNG", "53"},
{"", "", "Other", "37", "", "", "", "", ""},
}
assert.NoError(t, err) assert.NoError(t, err)
for _, row := range rows { assert.Equal(t, expected, rows)
for _, cell := range row {
t.Log(cell, "\t")
}
t.Log("\r\n")
}
assert.NoError(t, f.UpdateLinkedValue()) assert.NoError(t, f.UpdateLinkedValue())
assert.NoError(t, f.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(100.1588, 'f', -1, 32))) assert.NoError(t, f.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(100.1588, 'f', -1, 32)))
@ -396,13 +405,19 @@ func TestGetCellHyperLink(t *testing.T) {
link, target, err := f.GetCellHyperLink("Sheet1", "A22") link, target, err := f.GetCellHyperLink("Sheet1", "A22")
assert.NoError(t, err) assert.NoError(t, err)
t.Log(link, target) assert.Equal(t, link, true)
assert.Equal(t, target, "https://github.com/xuri/excelize")
link, target, err = f.GetCellHyperLink("Sheet2", "D6") link, target, err = f.GetCellHyperLink("Sheet2", "D6")
assert.NoError(t, err) assert.NoError(t, err)
t.Log(link, target) assert.Equal(t, link, false)
assert.Equal(t, target, "")
link, target, err = f.GetCellHyperLink("Sheet3", "H3") link, target, err = f.GetCellHyperLink("Sheet3", "H3")
assert.EqualError(t, err, "sheet Sheet3 does not exist") assert.EqualError(t, err, "sheet Sheet3 does not exist")
t.Log(link, target) assert.Equal(t, link, false)
assert.Equal(t, target, "")
assert.NoError(t, f.Close()) assert.NoError(t, f.Close())
f = NewFile() f = NewFile()
@ -709,6 +724,14 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
col := []string{"L", "M", "N", "O", "P"} col := []string{"L", "M", "N", "O", "P"}
data := []int{0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49} data := []int{0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49}
value := []string{"37947.7500001", "-37947.7500001", "0.007", "2.1", "String"} value := []string{"37947.7500001", "-37947.7500001", "0.007", "2.1", "String"}
expected := [][]string{
{"37947.7500001", "37948", "37947.75", "37948", "37947.75", "3794775%", "3794775.00%", "3.79E+04", "37947.7500001", "37947.7500001", "11-22-03", "22-Nov-03", "22-Nov", "Nov-03", "6:00 pm", "6:00:00 pm", "18:00", "18:00:00", "11/22/03 18:00", "37947", "37947", "37947.75", "37947.75", "37947.7500001", "37947.7500001", "37947.7500001", "37947.7500001", "00:00", "910746:00:00", "37947.7500001", "3.79E+04", "37947.7500001"},
{"-37947.7500001", "-37948", "-37947.75", "-37948", "-37947.75", "-3794775%", "-3794775.00%", "-3.79E+04", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "(37947)", "(37947)", "(-37947.75)", "(-37947.75)", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-3.79E+04", "-37947.7500001"},
{"0.007", "0", "0.01", "0", "0.01", "1%", "0.70%", "7.00E-03", "0.007", "0.007", "12-30-99", "30-Dec-99", "30-Dec", "Dec-99", "0:10 am", "0:10:04 am", "00:10", "00:10:04", "12/30/99 00:10", "0", "0", "0.01", "0.01", "0.007", "0.007", "0.007", "0.007", "10:04", "0:10:04", "0.007", "7.00E-03", "0.007"},
{"2.1", "2", "2.10", "2", "2.10", "210%", "210.00%", "2.10E+00", "2.1", "2.1", "01-01-00", "1-Jan-00", "1-Jan", "Jan-00", "2:24 am", "2:24:00 am", "02:24", "02:24:00", "1/1/00 02:24", "2", "2", "2.10", "2.10", "2.1", "2.1", "2.1", "2.1", "24:00", "50:24:00", "2.1", "2.10E+00", "2.1"},
{"String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String"},
}
for i, v := range value { for i, v := range value {
for k, d := range data { for k, d := range data {
c := col[i] + strconv.Itoa(k+1) c := col[i] + strconv.Itoa(k+1)
@ -724,7 +747,9 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
t.FailNow() t.FailNow()
} }
assert.NoError(t, f.SetCellStyle("Sheet2", c, c, style)) assert.NoError(t, f.SetCellStyle("Sheet2", c, c, style))
t.Log(f.GetCellValue("Sheet2", c)) cellValue, err := f.GetCellValue("Sheet2", c)
assert.Equal(t, expected[i][k], cellValue)
assert.NoError(t, err)
} }
} }
var style int var style int

View File

@ -857,7 +857,10 @@ func (f *File) SetRowStyle(sheet string, start, end, styleID int) error {
if end > TotalRows { if end > TotalRows {
return ErrMaxRows return ErrMaxRows
} }
if styleID < 0 { s := f.stylesReader()
s.Lock()
defer s.Unlock()
if styleID < 0 || s.CellXfs == nil || len(s.CellXfs.Xf) <= styleID {
return newInvalidStyleID(styleID) return newInvalidStyleID(styleID)
} }
ws, err := f.workSheetReader(sheet) ws, err := f.workSheetReader(sheet)

View File

@ -956,7 +956,10 @@ func TestSetRowStyle(t *testing.T) {
assert.NoError(t, f.SetCellStyle("Sheet1", "B2", "B2", style1)) assert.NoError(t, f.SetCellStyle("Sheet1", "B2", "B2", style1))
assert.EqualError(t, f.SetRowStyle("Sheet1", 5, -1, style2), newInvalidRowNumberError(-1).Error()) assert.EqualError(t, f.SetRowStyle("Sheet1", 5, -1, style2), newInvalidRowNumberError(-1).Error())
assert.EqualError(t, f.SetRowStyle("Sheet1", 1, TotalRows+1, style2), ErrMaxRows.Error()) assert.EqualError(t, f.SetRowStyle("Sheet1", 1, TotalRows+1, style2), ErrMaxRows.Error())
// Test set row style with invalid style ID.
assert.EqualError(t, f.SetRowStyle("Sheet1", 1, 1, -1), newInvalidStyleID(-1).Error()) assert.EqualError(t, f.SetRowStyle("Sheet1", 1, 1, -1), newInvalidStyleID(-1).Error())
// Test set row style with not exists style ID.
assert.EqualError(t, f.SetRowStyle("Sheet1", 1, 1, 10), newInvalidStyleID(10).Error())
assert.EqualError(t, f.SetRowStyle("SheetN", 1, 1, style2), "sheet SheetN does not exist") assert.EqualError(t, f.SetRowStyle("SheetN", 1, 1, style2), "sheet SheetN does not exist")
assert.NoError(t, f.SetRowStyle("Sheet1", 5, 1, style2)) assert.NoError(t, f.SetRowStyle("Sheet1", 5, 1, style2))
cellStyleID, err := f.GetCellStyle("Sheet1", "B2") cellStyleID, err := f.GetCellStyle("Sheet1", "B2")

View File

@ -2629,6 +2629,14 @@ func (f *File) SetCellStyle(sheet, hCell, vCell string, styleID int) error {
makeContiguousColumns(ws, hRow, vRow, vCol) makeContiguousColumns(ws, hRow, vRow, vCol)
ws.Lock() ws.Lock()
defer ws.Unlock() defer ws.Unlock()
s := f.stylesReader()
s.Lock()
defer s.Unlock()
if styleID < 0 || s.CellXfs == nil || len(s.CellXfs.Xf) <= styleID {
return newInvalidStyleID(styleID)
}
for r := hRowIdx; r <= vRowIdx; r++ { for r := hRowIdx; r <= vRowIdx; r++ {
for k := hColIdx; k <= vColIdx; k++ { for k := hColIdx; k <= vColIdx; k++ {
ws.SheetData.Row[r].C[k].S = styleID ws.SheetData.Row[r].C[k].S = styleID

View File

@ -342,6 +342,10 @@ func TestSetCellStyle(t *testing.T) {
f := NewFile() f := NewFile()
// Test set cell style on not exists worksheet. // Test set cell style on not exists worksheet.
assert.EqualError(t, f.SetCellStyle("SheetN", "A1", "A2", 1), "sheet SheetN does not exist") assert.EqualError(t, f.SetCellStyle("SheetN", "A1", "A2", 1), "sheet SheetN does not exist")
// Test set cell style with invalid style ID.
assert.EqualError(t, f.SetCellStyle("Sheet1", "A1", "A2", -1), newInvalidStyleID(-1).Error())
// Test set cell style with not exists style ID.
assert.EqualError(t, f.SetCellStyle("Sheet1", "A1", "A2", 10), newInvalidStyleID(10).Error())
} }
func TestGetStyleID(t *testing.T) { func TestGetStyleID(t *testing.T) {