|
1 | 1 | package excelize
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | _ "image/gif"
|
5 | 6 | _ "image/jpeg"
|
6 | 7 | _ "image/png"
|
@@ -895,3 +896,62 @@ func TestRemoveRow(t *testing.T) {
|
895 | 896 | t.Log(err)
|
896 | 897 | }
|
897 | 898 | }
|
| 899 | + |
| 900 | +func TestConditionalFormat(t *testing.T) { |
| 901 | + xlsx := NewFile() |
| 902 | + for j := 1; j <= 10; j++ { |
| 903 | + for i := 0; i <= 10; i++ { |
| 904 | + xlsx.SetCellInt("Sheet1", ToAlphaString(i)+strconv.Itoa(j), j) |
| 905 | + } |
| 906 | + } |
| 907 | + var format1, format2, format3 int |
| 908 | + var err error |
| 909 | + // Rose format for bad conditional. |
| 910 | + format1, err = xlsx.NewConditionalStyle(`{"font":{"color":"#9A0511"},"fill":{"type":"pattern","color":["#FEC7CE"],"pattern":1}}`) |
| 911 | + t.Log(err) |
| 912 | + // Light yellow format for neutral conditional. |
| 913 | + format2, err = xlsx.NewConditionalStyle(`{"fill":{"type":"pattern","color":["#FEEAA0"],"pattern":1}}`) |
| 914 | + t.Log(err) |
| 915 | + // Light green format for good conditional. |
| 916 | + format3, err = xlsx.NewConditionalStyle(`{"font":{"color":"#09600B"},"fill":{"type":"pattern","color":["#C7EECF"],"pattern":1}}`) |
| 917 | + t.Log(err) |
| 918 | + // Color scales: 2 color. |
| 919 | + xlsx.SetConditionalFormat("Sheet1", "A1:A10", `[{"type":"2_color_scale","criteria":"=","min_type":"min","max_type":"max","min_color":"#F8696B","max_color":"#63BE7B"}]`) |
| 920 | + // Color scales: 3 color. |
| 921 | + xlsx.SetConditionalFormat("Sheet1", "B1:B10", `[{"type":"3_color_scale","criteria":"=","min_type":"min","mid_type":"percentile","max_type":"max","min_color":"#F8696B","mid_color":"#FFEB84","max_color":"#63BE7B"}]`) |
| 922 | + // Hightlight cells rules: between... |
| 923 | + xlsx.SetConditionalFormat("Sheet1", "C1:C10", fmt.Sprintf(`[{"type":"cell","criteria":"between","format":%d,"minimum":"6","maximum":"8"}]`, format1)) |
| 924 | + // Hightlight cells rules: Greater Than... |
| 925 | + xlsx.SetConditionalFormat("Sheet1", "D1:D10", fmt.Sprintf(`[{"type":"cell","criteria":">","format":%d,"value":"6"}]`, format3)) |
| 926 | + // Hightlight cells rules: Equal To... |
| 927 | + xlsx.SetConditionalFormat("Sheet1", "E1:E10", fmt.Sprintf(`[{"type":"top","criteria":"=","format":%d}]`, format3)) |
| 928 | + // Hightlight cells rules: Not Equal To... |
| 929 | + xlsx.SetConditionalFormat("Sheet1", "F1:F10", fmt.Sprintf(`[{"type":"unique","criteria":"=","format":%d}]`, format2)) |
| 930 | + // Hightlight cells rules: Duplicate Values... |
| 931 | + xlsx.SetConditionalFormat("Sheet1", "G1:G10", fmt.Sprintf(`[{"type":"duplicate","criteria":"=","format":%d}]`, format2)) |
| 932 | + // Top/Bottom rules: Top 10%. |
| 933 | + xlsx.SetConditionalFormat("Sheet1", "H1:H10", fmt.Sprintf(`[{"type":"top","criteria":"=","format":%d,"value":"6","percent":true}]`, format1)) |
| 934 | + // Top/Bottom rules: Above Average... |
| 935 | + xlsx.SetConditionalFormat("Sheet1", "I1:I10", fmt.Sprintf(`[{"type":"average","criteria":"=","format":%d, "above_average": true}]`, format3)) |
| 936 | + // Top/Bottom rules: Below Average... |
| 937 | + xlsx.SetConditionalFormat("Sheet1", "J1:J10", fmt.Sprintf(`[{"type":"average","criteria":"=","format":%d, "above_average": false}]`, format1)) |
| 938 | + // Data Bars: Gradient Fill. |
| 939 | + xlsx.SetConditionalFormat("Sheet1", "K1:K10", `[{"type":"data_bar", "criteria":"=", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`) |
| 940 | + err = xlsx.SaveAs("./test/Workbook_conditional_format.xlsx") |
| 941 | + if err != nil { |
| 942 | + t.Log(err) |
| 943 | + } |
| 944 | + |
| 945 | + // Set conditional format with illegal JSON string. |
| 946 | + _, err = xlsx.NewConditionalStyle("") |
| 947 | + t.Log(err) |
| 948 | + // Set conditional format with illegal valid type. |
| 949 | + xlsx.SetConditionalFormat("Sheet1", "K1:K10", `[{"type":"", "criteria":"=", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`) |
| 950 | + // Set conditional format with illegal criteria type. |
| 951 | + xlsx.SetConditionalFormat("Sheet1", "K1:K10", `[{"type":"data_bar", "criteria":"", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`) |
| 952 | + // Set conditional format with file without dxfs element. |
| 953 | + xlsx, err = OpenFile("./test/Workbook1.xlsx") |
| 954 | + t.Log(err) |
| 955 | + _, err = xlsx.NewConditionalStyle(`{"font":{"color":"#9A0511"},"fill":{"type":"pattern","color":["#FEC7CE"],"pattern":1}}`) |
| 956 | + t.Log(err) |
| 957 | +} |
0 commit comments