Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
修订版 | 75ce2317286181e2c250c10206df892278d5b981 (tree) |
---|---|
时间 | 2022-09-01 01:41:52 |
作者 | ![]() |
Commiter | xuri |
This closes #1323, an error will be returned when set the not exist style ID
@@ -6037,7 +6037,7 @@ func getBetaHelperContFrac(fX, fA, fB float64) float64 { | ||
6037 | 6037 | bfinished = math.Abs(cf-cfnew) < math.Abs(cf)*fMachEps |
6038 | 6038 | } |
6039 | 6039 | cf = cfnew |
6040 | - rm += 1 | |
6040 | + rm++ | |
6041 | 6041 | } |
6042 | 6042 | return cf |
6043 | 6043 | } |
@@ -6914,7 +6914,7 @@ func (fn *formulaFuncs) CHIDIST(argsList *list.List) formulaArg { | ||
6914 | 6914 | for z <= x1 { |
6915 | 6915 | e = math.Log(z) + e |
6916 | 6916 | s += math.Exp(c*z - a - e) |
6917 | - z += 1 | |
6917 | + z++ | |
6918 | 6918 | } |
6919 | 6919 | return newNumberFormulaArg(s) |
6920 | 6920 | } |
@@ -6926,7 +6926,7 @@ func (fn *formulaFuncs) CHIDIST(argsList *list.List) formulaArg { | ||
6926 | 6926 | for z <= x1 { |
6927 | 6927 | e = e * (a / z) |
6928 | 6928 | c = c + e |
6929 | - z += 1 | |
6929 | + z++ | |
6930 | 6930 | } |
6931 | 6931 | return newNumberFormulaArg(c*y + s) |
6932 | 6932 | } |
@@ -15286,10 +15286,10 @@ func (fn *formulaFuncs) coupons(name string, arg formulaArg) formulaArg { | ||
15286 | 15286 | month -= coupon |
15287 | 15287 | } |
15288 | 15288 | if month > 11 { |
15289 | - year += 1 | |
15289 | + year++ | |
15290 | 15290 | month -= 12 |
15291 | 15291 | } else if month < 0 { |
15292 | - year -= 1 | |
15292 | + year-- | |
15293 | 15293 | month += 12 |
15294 | 15294 | } |
15295 | 15295 | day, lastDay := maturity.Day(), time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) |
@@ -415,6 +415,13 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error { | ||
415 | 415 | if err != nil { |
416 | 416 | return err |
417 | 417 | } |
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() | |
418 | 425 | ws, err := f.workSheetReader(sheet) |
419 | 426 | if err != nil { |
420 | 427 | return err |
@@ -296,6 +296,10 @@ func TestSetColStyle(t *testing.T) { | ||
296 | 296 | // Test set column style with illegal cell coordinates. |
297 | 297 | assert.EqualError(t, f.SetColStyle("Sheet1", "*", styleID), newInvalidColumnNameError("*").Error()) |
298 | 298 | 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()) | |
299 | 303 | |
300 | 304 | assert.NoError(t, f.SetColStyle("Sheet1", "B", styleID)) |
301 | 305 | // Test set column style with already exists column with style. |
@@ -55,7 +55,7 @@ func newUnzipSizeLimitError(unzipSizeLimit int64) error { | ||
55 | 55 | // newInvalidStyleID defined the error message on receiving the invalid style |
56 | 56 | // ID. |
57 | 57 | 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) | |
59 | 59 | } |
60 | 60 | |
61 | 61 | // newFieldLengthError defined the error message on receiving the field length |
@@ -32,13 +32,22 @@ func TestOpenFile(t *testing.T) { | ||
32 | 32 | assert.EqualError(t, err, "sheet Sheet4 does not exist") |
33 | 33 | // Test get all the rows in a worksheet. |
34 | 34 | 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", "", "", "", "", ""}, | |
41 | 47 | } |
48 | + assert.NoError(t, err) | |
49 | + assert.Equal(t, expected, rows) | |
50 | + | |
42 | 51 | assert.NoError(t, f.UpdateLinkedValue()) |
43 | 52 | |
44 | 53 | assert.NoError(t, f.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(100.1588, 'f', -1, 32))) |
@@ -396,13 +405,19 @@ func TestGetCellHyperLink(t *testing.T) { | ||
396 | 405 | |
397 | 406 | link, target, err := f.GetCellHyperLink("Sheet1", "A22") |
398 | 407 | 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 | + | |
400 | 411 | link, target, err = f.GetCellHyperLink("Sheet2", "D6") |
401 | 412 | assert.NoError(t, err) |
402 | - t.Log(link, target) | |
413 | + assert.Equal(t, link, false) | |
414 | + assert.Equal(t, target, "") | |
415 | + | |
403 | 416 | link, target, err = f.GetCellHyperLink("Sheet3", "H3") |
404 | 417 | 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 | + | |
406 | 421 | assert.NoError(t, f.Close()) |
407 | 422 | |
408 | 423 | f = NewFile() |
@@ -709,6 +724,14 @@ func TestSetCellStyleNumberFormat(t *testing.T) { | ||
709 | 724 | col := []string{"L", "M", "N", "O", "P"} |
710 | 725 | 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} |
711 | 726 | 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 | + | |
712 | 735 | for i, v := range value { |
713 | 736 | for k, d := range data { |
714 | 737 | c := col[i] + strconv.Itoa(k+1) |
@@ -724,7 +747,9 @@ func TestSetCellStyleNumberFormat(t *testing.T) { | ||
724 | 747 | t.FailNow() |
725 | 748 | } |
726 | 749 | 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) | |
728 | 753 | } |
729 | 754 | } |
730 | 755 | var style int |
@@ -857,7 +857,10 @@ func (f *File) SetRowStyle(sheet string, start, end, styleID int) error { | ||
857 | 857 | if end > TotalRows { |
858 | 858 | return ErrMaxRows |
859 | 859 | } |
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 { | |
861 | 864 | return newInvalidStyleID(styleID) |
862 | 865 | } |
863 | 866 | ws, err := f.workSheetReader(sheet) |
@@ -956,7 +956,10 @@ func TestSetRowStyle(t *testing.T) { | ||
956 | 956 | assert.NoError(t, f.SetCellStyle("Sheet1", "B2", "B2", style1)) |
957 | 957 | assert.EqualError(t, f.SetRowStyle("Sheet1", 5, -1, style2), newInvalidRowNumberError(-1).Error()) |
958 | 958 | assert.EqualError(t, f.SetRowStyle("Sheet1", 1, TotalRows+1, style2), ErrMaxRows.Error()) |
959 | + // Test set row style with invalid style ID. | |
959 | 960 | 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()) | |
960 | 963 | assert.EqualError(t, f.SetRowStyle("SheetN", 1, 1, style2), "sheet SheetN does not exist") |
961 | 964 | assert.NoError(t, f.SetRowStyle("Sheet1", 5, 1, style2)) |
962 | 965 | cellStyleID, err := f.GetCellStyle("Sheet1", "B2") |
@@ -2629,6 +2629,14 @@ func (f *File) SetCellStyle(sheet, hCell, vCell string, styleID int) error { | ||
2629 | 2629 | makeContiguousColumns(ws, hRow, vRow, vCol) |
2630 | 2630 | ws.Lock() |
2631 | 2631 | 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 | + | |
2632 | 2640 | for r := hRowIdx; r <= vRowIdx; r++ { |
2633 | 2641 | for k := hColIdx; k <= vColIdx; k++ { |
2634 | 2642 | ws.SheetData.Row[r].C[k].S = styleID |
@@ -342,6 +342,10 @@ func TestSetCellStyle(t *testing.T) { | ||
342 | 342 | f := NewFile() |
343 | 343 | // Test set cell style on not exists worksheet. |
344 | 344 | 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()) | |
345 | 349 | } |
346 | 350 | |
347 | 351 | func TestGetStyleID(t *testing.T) { |