From cea3d806ecbec5027a8f0d7f10b700707131a7be Mon Sep 17 00:00:00 2001 From: xuri Date: Mon, 16 Mar 2020 00:13:01 +0800 Subject: [PATCH] Resolve #200, ignore empty conditional format style --- excelize_test.go | 17 ++++++++++++++--- styles.go | 10 +++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/excelize_test.go b/excelize_test.go index 1ce4fe9..815a08d 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -978,7 +978,7 @@ func TestConditionalFormat(t *testing.T) { fillCells(f, sheet1, 10, 15) - var format1, format2, format3 int + var format1, format2, format3, format4 int var err error // Rose format for bad conditional. format1, err = f.NewConditionalStyle(`{"font":{"color":"#9A0511"},"fill":{"type":"pattern","color":["#FEC7CE"],"pattern":1}}`) @@ -998,6 +998,12 @@ func TestConditionalFormat(t *testing.T) { t.FailNow() } + // conditional style with align and left border. + format4, err = f.NewConditionalStyle(`{"alignment":{"wrap_text":true},"border":[{"type":"left","color":"#000000","style":1}]}`) + if !assert.NoError(t, err) { + t.FailNow() + } + // Color scales: 2 color. assert.NoError(t, f.SetConditionalFormat(sheet1, "A1:A10", `[{"type":"2_color_scale","criteria":"=","min_type":"min","max_type":"max","min_color":"#F8696B","max_color":"#63BE7B"}]`)) // Color scales: 3 color. @@ -1022,8 +1028,13 @@ func TestConditionalFormat(t *testing.T) { assert.NoError(t, f.SetConditionalFormat(sheet1, "K1:K10", `[{"type":"data_bar", "criteria":"=", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`)) // Use a formula to determine which cells to format. assert.NoError(t, f.SetConditionalFormat(sheet1, "L1:L10", fmt.Sprintf(`[{"type":"formula", "criteria":"L2<3", "format":%d}]`, format1))) - // Test set invalid format set in conditional format + // Alignment/Border cells rules. + assert.NoError(t, f.SetConditionalFormat(sheet1, "M1:M10", fmt.Sprintf(`[{"type":"cell","criteria":">","format":%d,"value":"0"}]`, format4))) + + // Test set invalid format set in conditional format. assert.EqualError(t, f.SetConditionalFormat(sheet1, "L1:L10", ""), "unexpected end of JSON input") + // Set conditional format on not exists worksheet. + assert.EqualError(t, f.SetConditionalFormat("SheetN", "L1:L10", "[]"), "sheet SheetN is not exist") err = f.SaveAs(filepath.Join("test", "TestConditionalFormat.xlsx")) if !assert.NoError(t, err) { @@ -1053,7 +1064,7 @@ func TestConditionalFormatError(t *testing.T) { fillCells(f, sheet1, 10, 15) - // Set conditional format with illegal JSON string should return error + // Set conditional format with illegal JSON string should return error. _, err := f.NewConditionalStyle("") if !assert.EqualError(t, err, "unexpected end of JSON input") { t.FailNow() diff --git a/styles.go b/styles.go index 175a17c..f2171bb 100644 --- a/styles.go +++ b/styles.go @@ -1943,9 +1943,13 @@ func (f *File) NewConditionalStyle(style string) (int, error) { return 0, err } dxf := dxf{ - Fill: setFills(fs, false), - Alignment: setAlignment(fs), - Border: setBorders(fs), + Fill: setFills(fs, false), + } + if fs.Alignment != nil { + dxf.Alignment = setAlignment(fs) + } + if len(fs.Border) > 0 { + dxf.Border = setBorders(fs) } if fs.Font != nil { dxf.Font = f.setFont(fs)