diff --git a/README.md b/README.md index 22748f8..3d6383b 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ## Introduction -Excelize is a library written in pure Golang and providing a set of functions that allow you to write to and read from XLSX files. Support reads and writes XLSX file generated by Microsoft Excelâ„¢ 2007 and later. Support save file without losing original charts of XLSX. This library needs Go version 1.8 or later. The full API docs can be seen using go's built-in documentation tool, or online at [godoc.org](https://godoc.org/github.com/xuri/excelize). +Excelize is a library written in pure Golang and providing a set of functions that allow you to write to and read from XLSX files. Support reads and writes XLSX file generated by Microsoft Excelâ„¢ 2007 and later. Support save file without losing original charts of XLSX. This library needs Go version 1.8 or later. The full API docs can be seen using go's built-in documentation tool, or online at [godoc.org](https://godoc.org/github.com/360EntSecGroup-Skylar/excelize). ## Basic Usage diff --git a/excelize_test.go b/excelize_test.go index f4a0774..b075cd0 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -900,7 +900,7 @@ func TestRemoveRow(t *testing.T) { func TestConditionalFormat(t *testing.T) { xlsx := NewFile() for j := 1; j <= 10; j++ { - for i := 0; i <= 10; i++ { + for i := 0; i <= 15; i++ { xlsx.SetCellInt("Sheet1", ToAlphaString(i)+strconv.Itoa(j), j) } } @@ -937,6 +937,8 @@ func TestConditionalFormat(t *testing.T) { xlsx.SetConditionalFormat("Sheet1", "J1:J10", fmt.Sprintf(`[{"type":"average","criteria":"=","format":%d, "above_average": false}]`, format1)) // Data Bars: Gradient Fill. xlsx.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. + xlsx.SetConditionalFormat("Sheet1", "L1:L10", fmt.Sprintf(`[{"type":"formula", "criteria":"L2<3", "format":%d}]`, format1)) err = xlsx.SaveAs("./test/Workbook_conditional_format.xlsx") if err != nil { t.Log(err) diff --git a/styles.go b/styles.go index a055782..225cfe5 100644 --- a/styles.go +++ b/styles.go @@ -793,7 +793,7 @@ var validType = map[string]string{ "2_color_scale": "2_color_scale", "3_color_scale": "3_color_scale", "data_bar": "dataBar", - "formula": "expression", // Doesn't support currently + "formula": "expression", } // criteriaType defined the list of valid criteria types. @@ -2540,6 +2540,7 @@ func (f *File) SetConditionalFormat(sheet, area, formatSet string) { "2_color_scale": drawCondFmtColorScale, "3_color_scale": drawCondFmtColorScale, "dataBar": drawCondFmtDataBar, + "expression": drawConfFmtExp, } xlsx := f.workSheetReader(sheet) @@ -2554,7 +2555,7 @@ func (f *File) SetConditionalFormat(sheet, area, formatSet string) { } // Check for valid criteria types. ct, ok = criteriaType[v.Criteria] - if !ok { + if !ok && vt != "expression" { continue } @@ -2672,6 +2673,17 @@ func drawCondFmtDataBar(p int, ct string, format *formatConditional) *xlsxCfRule } } +// drawConfFmtExp provides function to create conditional formatting rule for +// expression by given priority, criteria type and format settings. +func drawConfFmtExp(p int, ct string, format *formatConditional) *xlsxCfRule { + return &xlsxCfRule{ + Priority: p + 1, + Type: validType[format.Type], + Formula: []string{format.Criteria}, + DxfID: &format.Format, + } +} + // getPaletteColor provides function to convert the RBG color by given string. func getPaletteColor(color string) string { return "FF" + strings.Replace(strings.ToUpper(color), "#", "", -1)