Skip to content

Commit

Permalink
This closes qax-os#1323, an error will be returned when set the not e…
Browse files Browse the repository at this point in the history
…xist style ID
  • Loading branch information
xuri committed Jul 11, 2023
1 parent e614b0b commit 89b7d3a
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 17 deletions.
10 changes: 5 additions & 5 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6037,7 +6037,7 @@ func getBetaHelperContFrac(fX, fA, fB float64) float64 {
bfinished = math.Abs(cf-cfnew) < math.Abs(cf)*fMachEps
}
cf = cfnew
rm += 1
rm++
}
return cf
}
Expand Down Expand Up @@ -6914,7 +6914,7 @@ func (fn *formulaFuncs) CHIDIST(argsList *list.List) formulaArg {
for z <= x1 {
e = math.Log(z) + e
s += math.Exp(c*z - a - e)
z += 1
z++
}
return newNumberFormulaArg(s)
}
Expand All @@ -6926,7 +6926,7 @@ func (fn *formulaFuncs) CHIDIST(argsList *list.List) formulaArg {
for z <= x1 {
e = e * (a / z)
c = c + e
z += 1
z++
}
return newNumberFormulaArg(c*y + s)
}
Expand Down Expand Up @@ -15286,10 +15286,10 @@ func (fn *formulaFuncs) coupons(name string, arg formulaArg) formulaArg {
month -= coupon
}
if month > 11 {
year += 1
year++
month -= 12
} else if month < 0 {
year -= 1
year--
month += 12
}
day, lastDay := maturity.Day(), time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)
Expand Down
7 changes: 7 additions & 0 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
if err != nil {
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)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions col_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ func TestSetColStyle(t *testing.T) {
// Test set column style with illegal cell coordinates.
assert.EqualError(t, f.SetColStyle("Sheet1", "*", 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))
// Test set column style with already exists column with style.
Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func newUnzipSizeLimitError(unzipSizeLimit int64) error {
// newInvalidStyleID defined the error message on receiving the invalid style
// ID.
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
Expand Down
45 changes: 35 additions & 10 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@ func TestOpenFile(t *testing.T) {
assert.EqualError(t, err, "sheet Sheet4 does not exist")
// Test get all the rows in a worksheet.
rows, err := f.GetRows("Sheet2")
assert.NoError(t, err)
for _, row := range rows {
for _, cell := range row {
t.Log(cell, "\t")
}
t.Log("\r\n")
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.Equal(t, expected, rows)

assert.NoError(t, f.UpdateLinkedValue())

assert.NoError(t, f.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(100.1588, 'f', -1, 32)))
Expand Down Expand Up @@ -396,13 +405,19 @@ func TestGetCellHyperLink(t *testing.T) {

link, target, err := f.GetCellHyperLink("Sheet1", "A22")
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")
assert.NoError(t, err)
t.Log(link, target)
assert.Equal(t, link, false)
assert.Equal(t, target, "")

link, target, err = f.GetCellHyperLink("Sheet3", "H3")
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())

f = NewFile()
Expand Down Expand Up @@ -709,6 +724,14 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
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}
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 k, d := range data {
c := col[i] + strconv.Itoa(k+1)
Expand All @@ -724,7 +747,9 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
t.FailNow()
}
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
Expand Down
5 changes: 4 additions & 1 deletion rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,10 @@ func (f *File) SetRowStyle(sheet string, start, end, styleID int) error {
if end > TotalRows {
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)
}
ws, err := f.workSheetReader(sheet)
Expand Down
3 changes: 3 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,10 @@ func TestSetRowStyle(t *testing.T) {
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", 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())
// 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.NoError(t, f.SetRowStyle("Sheet1", 5, 1, style2))
cellStyleID, err := f.GetCellStyle("Sheet1", "B2")
Expand Down
8 changes: 8 additions & 0 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2629,6 +2629,14 @@ func (f *File) SetCellStyle(sheet, hCell, vCell string, styleID int) error {
makeContiguousColumns(ws, hRow, vRow, vCol)
ws.Lock()
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 k := hColIdx; k <= vColIdx; k++ {
ws.SheetData.Row[r].C[k].S = styleID
Expand Down
4 changes: 4 additions & 0 deletions styles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ func TestSetCellStyle(t *testing.T) {
f := NewFile()
// Test set cell style on not exists worksheet.
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) {
Expand Down

0 comments on commit 89b7d3a

Please sign in to comment.