diff --git a/calc.go b/calc.go index 25595d68..f6217a87 100644 --- a/calc.go +++ b/calc.go @@ -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 } @@ -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) } @@ -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) } @@ -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) diff --git a/col.go b/col.go index f51336d3..c0deb58c 100644 --- a/col.go +++ b/col.go @@ -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 diff --git a/col_test.go b/col_test.go index b7d38236..1076f31c 100644 --- a/col_test.go +++ b/col_test.go @@ -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. diff --git a/errors.go b/errors.go index a31330cf..48476bc4 100644 --- a/errors.go +++ b/errors.go @@ -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 diff --git a/excelize_test.go b/excelize_test.go index 1460d4a6..19aba7ee 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -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))) @@ -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() @@ -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) @@ -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 diff --git a/rows.go b/rows.go index 9269ac63..fdb93742 100644 --- a/rows.go +++ b/rows.go @@ -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) diff --git a/rows_test.go b/rows_test.go index 829a27ae..02e2d207 100644 --- a/rows_test.go +++ b/rows_test.go @@ -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") diff --git a/styles.go b/styles.go index 55ee1755..87c4863e 100644 --- a/styles.go +++ b/styles.go @@ -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 diff --git a/styles_test.go b/styles_test.go index 257f98d4..47aee5b8 100644 --- a/styles_test.go +++ b/styles_test.go @@ -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) {