• R/O
  • HTTP
  • SSH
  • HTTPS

excelize: 提交

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets


Commit MetaInfo

修订版75ce2317286181e2c250c10206df892278d5b981 (tree)
时间2022-09-01 01:41:52
作者xuri <xuri.me@gmai...>
Commiterxuri

Log Message

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

更改概述

差异

--- a/calc.go
+++ b/calc.go
@@ -6037,7 +6037,7 @@ func getBetaHelperContFrac(fX, fA, fB float64) float64 {
60376037 bfinished = math.Abs(cf-cfnew) < math.Abs(cf)*fMachEps
60386038 }
60396039 cf = cfnew
6040- rm += 1
6040+ rm++
60416041 }
60426042 return cf
60436043 }
@@ -6914,7 +6914,7 @@ func (fn *formulaFuncs) CHIDIST(argsList *list.List) formulaArg {
69146914 for z <= x1 {
69156915 e = math.Log(z) + e
69166916 s += math.Exp(c*z - a - e)
6917- z += 1
6917+ z++
69186918 }
69196919 return newNumberFormulaArg(s)
69206920 }
@@ -6926,7 +6926,7 @@ func (fn *formulaFuncs) CHIDIST(argsList *list.List) formulaArg {
69266926 for z <= x1 {
69276927 e = e * (a / z)
69286928 c = c + e
6929- z += 1
6929+ z++
69306930 }
69316931 return newNumberFormulaArg(c*y + s)
69326932 }
@@ -15286,10 +15286,10 @@ func (fn *formulaFuncs) coupons(name string, arg formulaArg) formulaArg {
1528615286 month -= coupon
1528715287 }
1528815288 if month > 11 {
15289- year += 1
15289+ year++
1529015290 month -= 12
1529115291 } else if month < 0 {
15292- year -= 1
15292+ year--
1529315293 month += 12
1529415294 }
1529515295 day, lastDay := maturity.Day(), time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)
--- a/col.go
+++ b/col.go
@@ -415,6 +415,13 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
415415 if err != nil {
416416 return err
417417 }
418+ s := f.stylesReader()
419+ s.Lock()
420+ if styleID < 0 || s.CellXfs == nil || len(s.CellXfs.Xf) <= styleID {
421+ s.Unlock()
422+ return newInvalidStyleID(styleID)
423+ }
424+ s.Unlock()
418425 ws, err := f.workSheetReader(sheet)
419426 if err != nil {
420427 return err
--- a/col_test.go
+++ b/col_test.go
@@ -296,6 +296,10 @@ func TestSetColStyle(t *testing.T) {
296296 // Test set column style with illegal cell coordinates.
297297 assert.EqualError(t, f.SetColStyle("Sheet1", "*", styleID), newInvalidColumnNameError("*").Error())
298298 assert.EqualError(t, f.SetColStyle("Sheet1", "A:*", styleID), newInvalidColumnNameError("*").Error())
299+ // Test set column style with invalid style ID.
300+ assert.EqualError(t, f.SetColStyle("Sheet1", "B", -1), newInvalidStyleID(-1).Error())
301+ // Test set column style with not exists style ID.
302+ assert.EqualError(t, f.SetColStyle("Sheet1", "B", 10), newInvalidStyleID(10).Error())
299303
300304 assert.NoError(t, f.SetColStyle("Sheet1", "B", styleID))
301305 // Test set column style with already exists column with style.
--- a/errors.go
+++ b/errors.go
@@ -55,7 +55,7 @@ func newUnzipSizeLimitError(unzipSizeLimit int64) error {
5555 // newInvalidStyleID defined the error message on receiving the invalid style
5656 // ID.
5757 func newInvalidStyleID(styleID int) error {
58- return fmt.Errorf("invalid style ID %d, negative values are not supported", styleID)
58+ return fmt.Errorf("invalid style ID %d", styleID)
5959 }
6060
6161 // newFieldLengthError defined the error message on receiving the field length
--- a/excelize_test.go
+++ b/excelize_test.go
@@ -32,13 +32,22 @@ func TestOpenFile(t *testing.T) {
3232 assert.EqualError(t, err, "sheet Sheet4 does not exist")
3333 // Test get all the rows in a worksheet.
3434 rows, err := f.GetRows("Sheet2")
35- assert.NoError(t, err)
36- for _, row := range rows {
37- for _, cell := range row {
38- t.Log(cell, "\t")
39- }
40- t.Log("\r\n")
35+ expected := [][]string{
36+ {"Monitor", "", "Brand", "", "inlineStr"},
37+ {"> 23 Inch", "19", "HP", "200"},
38+ {"20-23 Inch", "24", "DELL", "450"},
39+ {"17-20 Inch", "56", "Lenove", "200"},
40+ {"< 17 Inch", "21", "SONY", "510"},
41+ {"", "", "Acer", "315"},
42+ {"", "", "IBM", "127"},
43+ {"", "", "ASUS", "89"},
44+ {"", "", "Apple", "348"},
45+ {"", "", "SAMSUNG", "53"},
46+ {"", "", "Other", "37", "", "", "", "", ""},
4147 }
48+ assert.NoError(t, err)
49+ assert.Equal(t, expected, rows)
50+
4251 assert.NoError(t, f.UpdateLinkedValue())
4352
4453 assert.NoError(t, f.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(100.1588, 'f', -1, 32)))
@@ -396,13 +405,19 @@ func TestGetCellHyperLink(t *testing.T) {
396405
397406 link, target, err := f.GetCellHyperLink("Sheet1", "A22")
398407 assert.NoError(t, err)
399- t.Log(link, target)
408+ assert.Equal(t, link, true)
409+ assert.Equal(t, target, "https://github.com/xuri/excelize")
410+
400411 link, target, err = f.GetCellHyperLink("Sheet2", "D6")
401412 assert.NoError(t, err)
402- t.Log(link, target)
413+ assert.Equal(t, link, false)
414+ assert.Equal(t, target, "")
415+
403416 link, target, err = f.GetCellHyperLink("Sheet3", "H3")
404417 assert.EqualError(t, err, "sheet Sheet3 does not exist")
405- t.Log(link, target)
418+ assert.Equal(t, link, false)
419+ assert.Equal(t, target, "")
420+
406421 assert.NoError(t, f.Close())
407422
408423 f = NewFile()
@@ -709,6 +724,14 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
709724 col := []string{"L", "M", "N", "O", "P"}
710725 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}
711726 value := []string{"37947.7500001", "-37947.7500001", "0.007", "2.1", "String"}
727+ expected := [][]string{
728+ {"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"},
729+ {"-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"},
730+ {"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"},
731+ {"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"},
732+ {"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"},
733+ }
734+
712735 for i, v := range value {
713736 for k, d := range data {
714737 c := col[i] + strconv.Itoa(k+1)
@@ -724,7 +747,9 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
724747 t.FailNow()
725748 }
726749 assert.NoError(t, f.SetCellStyle("Sheet2", c, c, style))
727- t.Log(f.GetCellValue("Sheet2", c))
750+ cellValue, err := f.GetCellValue("Sheet2", c)
751+ assert.Equal(t, expected[i][k], cellValue)
752+ assert.NoError(t, err)
728753 }
729754 }
730755 var style int
--- a/rows.go
+++ b/rows.go
@@ -857,7 +857,10 @@ func (f *File) SetRowStyle(sheet string, start, end, styleID int) error {
857857 if end > TotalRows {
858858 return ErrMaxRows
859859 }
860- if styleID < 0 {
860+ s := f.stylesReader()
861+ s.Lock()
862+ defer s.Unlock()
863+ if styleID < 0 || s.CellXfs == nil || len(s.CellXfs.Xf) <= styleID {
861864 return newInvalidStyleID(styleID)
862865 }
863866 ws, err := f.workSheetReader(sheet)
--- a/rows_test.go
+++ b/rows_test.go
@@ -956,7 +956,10 @@ func TestSetRowStyle(t *testing.T) {
956956 assert.NoError(t, f.SetCellStyle("Sheet1", "B2", "B2", style1))
957957 assert.EqualError(t, f.SetRowStyle("Sheet1", 5, -1, style2), newInvalidRowNumberError(-1).Error())
958958 assert.EqualError(t, f.SetRowStyle("Sheet1", 1, TotalRows+1, style2), ErrMaxRows.Error())
959+ // Test set row style with invalid style ID.
959960 assert.EqualError(t, f.SetRowStyle("Sheet1", 1, 1, -1), newInvalidStyleID(-1).Error())
961+ // Test set row style with not exists style ID.
962+ assert.EqualError(t, f.SetRowStyle("Sheet1", 1, 1, 10), newInvalidStyleID(10).Error())
960963 assert.EqualError(t, f.SetRowStyle("SheetN", 1, 1, style2), "sheet SheetN does not exist")
961964 assert.NoError(t, f.SetRowStyle("Sheet1", 5, 1, style2))
962965 cellStyleID, err := f.GetCellStyle("Sheet1", "B2")
--- a/styles.go
+++ b/styles.go
@@ -2629,6 +2629,14 @@ func (f *File) SetCellStyle(sheet, hCell, vCell string, styleID int) error {
26292629 makeContiguousColumns(ws, hRow, vRow, vCol)
26302630 ws.Lock()
26312631 defer ws.Unlock()
2632+
2633+ s := f.stylesReader()
2634+ s.Lock()
2635+ defer s.Unlock()
2636+ if styleID < 0 || s.CellXfs == nil || len(s.CellXfs.Xf) <= styleID {
2637+ return newInvalidStyleID(styleID)
2638+ }
2639+
26322640 for r := hRowIdx; r <= vRowIdx; r++ {
26332641 for k := hColIdx; k <= vColIdx; k++ {
26342642 ws.SheetData.Row[r].C[k].S = styleID
--- a/styles_test.go
+++ b/styles_test.go
@@ -342,6 +342,10 @@ func TestSetCellStyle(t *testing.T) {
342342 f := NewFile()
343343 // Test set cell style on not exists worksheet.
344344 assert.EqualError(t, f.SetCellStyle("SheetN", "A1", "A2", 1), "sheet SheetN does not exist")
345+ // Test set cell style with invalid style ID.
346+ assert.EqualError(t, f.SetCellStyle("Sheet1", "A1", "A2", -1), newInvalidStyleID(-1).Error())
347+ // Test set cell style with not exists style ID.
348+ assert.EqualError(t, f.SetCellStyle("Sheet1", "A1", "A2", 10), newInvalidStyleID(10).Error())
345349 }
346350
347351 func TestGetStyleID(t *testing.T) {
Show on old repository browser