From a885bb0fb92dd7a55f27f0fd57d174121db79b0f Mon Sep 17 00:00:00 2001 From: Alex Whitney Date: Wed, 25 Jul 2018 10:40:08 -0400 Subject: [PATCH 1/2] Add failing unit tests for issue-252 --- styles_test.go | 134 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 styles_test.go diff --git a/styles_test.go b/styles_test.go new file mode 100644 index 0000000..7a7e228 --- /dev/null +++ b/styles_test.go @@ -0,0 +1,134 @@ +package excelize + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSetConditionalFormat(t *testing.T) { + cases := []struct { + label string + format string + rules []*xlsxCfRule + }{{ + label: "3_color_scale", + format: `[{ + "type":"3_color_scale", + "criteria":"=", + "min_type":"num", + "mid_type":"num", + "max_type":"num", + "min_value": "-10", + "mid_value": "0", + "max_value": "10", + "min_color":"ff0000", + "mid_color":"00ff00", + "max_color":"0000ff" + }]`, + rules: []*xlsxCfRule{{ + Priority: 1, + Type: "colorScale", + ColorScale: &xlsxColorScale{ + Cfvo: []*xlsxCfvo{{ + Type: "num", + Val: -10, + }, { + Type: "num", + Val: 0, + }, { + Type: "num", + Val: 10, + }}, + Color: []*xlsxColor{{ + RGB: "FFFF0000", + }, { + RGB: "FF00FF00", + }, { + RGB: "FF0000FF", + }}, + }, + }}, + }, { + label: "3_color_scale default min/mid/max", + format: `[{ + "type":"3_color_scale", + "criteria":"=", + "min_type":"num", + "mid_type":"num", + "max_type":"num", + "min_color":"ff0000", + "mid_color":"00ff00", + "max_color":"0000ff" + }]`, + rules: []*xlsxCfRule{{ + Priority: 1, + Type: "colorScale", + ColorScale: &xlsxColorScale{ + Cfvo: []*xlsxCfvo{{ + Type: "num", + Val: 0, + }, { + Type: "num", + Val: 50, + }, { + Type: "num", + Val: 0, + }}, + Color: []*xlsxColor{{ + RGB: "FFFF0000", + }, { + RGB: "FF00FF00", + }, { + RGB: "FF0000FF", + }}, + }, + }}, + }, { + label: "2_color_scale default min/max", + format: `[{ + "type":"2_color_scale", + "criteria":"=", + "min_type":"num", + "max_type":"num", + "min_color":"ff0000", + "max_color":"0000ff" + }]`, + rules: []*xlsxCfRule{{ + Priority: 1, + Type: "colorScale", + ColorScale: &xlsxColorScale{ + Cfvo: []*xlsxCfvo{{ + Type: "num", + Val: 0, + }, { + Type: "num", + Val: 0, + }}, + Color: []*xlsxColor{{ + RGB: "FFFF0000", + }, { + RGB: "FF0000FF", + }}, + }, + }}, + }} + + for _, testCase := range cases { + xl := NewFile() + const sheet = "Sheet1" + const cellRange = "A1:A1" + + err := xl.SetConditionalFormat(sheet, cellRange, testCase.format) + if err != nil { + t.Fatalf("%s", err) + } + + xlsx := xl.workSheetReader(sheet) + cf := xlsx.ConditionalFormatting + assert.Len(t, cf, 1, testCase.label) + assert.Len(t, cf[0].CfRule, 1, testCase.label) + assert.Equal(t, cellRange, cf[0].SQRef, testCase.label) + assert.EqualValues(t, testCase.rules, cf[0].CfRule, testCase.label) + } +} From db7a605cf8384f86dd3d3f766050a201b5f10eec Mon Sep 17 00:00:00 2001 From: Alex Whitney Date: Tue, 24 Jul 2018 16:12:26 -0400 Subject: [PATCH 2/2] Use min/mid/max value for 2 and 3 color scale conditional formatting --- styles.go | 19 ++++++++++++++++--- styles_test.go | 16 ++++++++-------- xmlWorksheet.go | 2 +- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/styles.go b/styles.go index 251e335..fdbf932 100644 --- a/styles.go +++ b/styles.go @@ -2674,12 +2674,25 @@ func drawCondFmtDuplicateUniqueValues(p int, ct string, format *formatConditiona // for color scale (include 2 color scale and 3 color scale) by given priority, // criteria type and format settings. func drawCondFmtColorScale(p int, ct string, format *formatConditional) *xlsxCfRule { + minValue := format.MinValue + if minValue == "" { + minValue = "0" + } + maxValue := format.MaxValue + if maxValue == "" { + maxValue = "0" + } + midValue := format.MidValue + if midValue == "" { + midValue = "50" + } + c := &xlsxCfRule{ Priority: p + 1, Type: "colorScale", ColorScale: &xlsxColorScale{ Cfvo: []*xlsxCfvo{ - {Type: format.MinType}, + {Type: format.MinType, Val: minValue}, }, Color: []*xlsxColor{ {RGB: getPaletteColor(format.MinColor)}, @@ -2687,10 +2700,10 @@ func drawCondFmtColorScale(p int, ct string, format *formatConditional) *xlsxCfR }, } if validType[format.Type] == "3_color_scale" { - c.ColorScale.Cfvo = append(c.ColorScale.Cfvo, &xlsxCfvo{Type: format.MidType, Val: 50}) + c.ColorScale.Cfvo = append(c.ColorScale.Cfvo, &xlsxCfvo{Type: format.MidType, Val: midValue}) c.ColorScale.Color = append(c.ColorScale.Color, &xlsxColor{RGB: getPaletteColor(format.MidColor)}) } - c.ColorScale.Cfvo = append(c.ColorScale.Cfvo, &xlsxCfvo{Type: format.MaxType}) + c.ColorScale.Cfvo = append(c.ColorScale.Cfvo, &xlsxCfvo{Type: format.MaxType, Val: maxValue}) c.ColorScale.Color = append(c.ColorScale.Color, &xlsxColor{RGB: getPaletteColor(format.MaxColor)}) return c } diff --git a/styles_test.go b/styles_test.go index 7a7e228..baa66f0 100644 --- a/styles_test.go +++ b/styles_test.go @@ -32,13 +32,13 @@ func TestSetConditionalFormat(t *testing.T) { ColorScale: &xlsxColorScale{ Cfvo: []*xlsxCfvo{{ Type: "num", - Val: -10, + Val: "-10", }, { Type: "num", - Val: 0, + Val: "0", }, { Type: "num", - Val: 10, + Val: "10", }}, Color: []*xlsxColor{{ RGB: "FFFF0000", @@ -67,13 +67,13 @@ func TestSetConditionalFormat(t *testing.T) { ColorScale: &xlsxColorScale{ Cfvo: []*xlsxCfvo{{ Type: "num", - Val: 0, + Val: "0", }, { Type: "num", - Val: 50, + Val: "50", }, { Type: "num", - Val: 0, + Val: "0", }}, Color: []*xlsxColor{{ RGB: "FFFF0000", @@ -100,10 +100,10 @@ func TestSetConditionalFormat(t *testing.T) { ColorScale: &xlsxColorScale{ Cfvo: []*xlsxCfvo{{ Type: "num", - Val: 0, + Val: "0", }, { Type: "num", - Val: 0, + Val: "0", }}, Color: []*xlsxColor{{ RGB: "FFFF0000", diff --git a/xmlWorksheet.go b/xmlWorksheet.go index 37c0d18..87d66a1 100644 --- a/xmlWorksheet.go +++ b/xmlWorksheet.go @@ -446,7 +446,7 @@ type xlsxIconSet struct { type xlsxCfvo struct { Gte bool `xml:"gte,attr,omitempty"` Type string `xml:"type,attr,omitempty"` - Val int `xml:"val,attr"` + Val string `xml:"val,attr"` ExtLst *xlsxExtLst `xml:"extLst"` }