Skip to content

Commit 77af252

Browse files
committed
- Conditional format with formula support, relate issue qax-os#75;
- go test and readme update
1 parent 58e2caf commit 77af252

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
## Introduction
1313

14-
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).
14+
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).
1515

1616
## Basic Usage
1717

excelize_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ func TestRemoveRow(t *testing.T) {
900900
func TestConditionalFormat(t *testing.T) {
901901
xlsx := NewFile()
902902
for j := 1; j <= 10; j++ {
903-
for i := 0; i <= 10; i++ {
903+
for i := 0; i <= 15; i++ {
904904
xlsx.SetCellInt("Sheet1", ToAlphaString(i)+strconv.Itoa(j), j)
905905
}
906906
}
@@ -937,6 +937,8 @@ func TestConditionalFormat(t *testing.T) {
937937
xlsx.SetConditionalFormat("Sheet1", "J1:J10", fmt.Sprintf(`[{"type":"average","criteria":"=","format":%d, "above_average": false}]`, format1))
938938
// Data Bars: Gradient Fill.
939939
xlsx.SetConditionalFormat("Sheet1", "K1:K10", `[{"type":"data_bar", "criteria":"=", "min_type":"min","max_type":"max","bar_color":"#638EC6"}]`)
940+
// Use a formula to determine which cells to format.
941+
xlsx.SetConditionalFormat("Sheet1", "L1:L10", fmt.Sprintf(`[{"type":"formula", "criteria":"L2<3", "format":%d}]`, format1))
940942
err = xlsx.SaveAs("./test/Workbook_conditional_format.xlsx")
941943
if err != nil {
942944
t.Log(err)

styles.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ var validType = map[string]string{
793793
"2_color_scale": "2_color_scale",
794794
"3_color_scale": "3_color_scale",
795795
"data_bar": "dataBar",
796-
"formula": "expression", // Doesn't support currently
796+
"formula": "expression",
797797
}
798798

799799
// criteriaType defined the list of valid criteria types.
@@ -2540,6 +2540,7 @@ func (f *File) SetConditionalFormat(sheet, area, formatSet string) {
25402540
"2_color_scale": drawCondFmtColorScale,
25412541
"3_color_scale": drawCondFmtColorScale,
25422542
"dataBar": drawCondFmtDataBar,
2543+
"expression": drawConfFmtExp,
25432544
}
25442545

25452546
xlsx := f.workSheetReader(sheet)
@@ -2554,7 +2555,7 @@ func (f *File) SetConditionalFormat(sheet, area, formatSet string) {
25542555
}
25552556
// Check for valid criteria types.
25562557
ct, ok = criteriaType[v.Criteria]
2557-
if !ok {
2558+
if !ok && vt != "expression" {
25582559
continue
25592560
}
25602561

@@ -2672,6 +2673,17 @@ func drawCondFmtDataBar(p int, ct string, format *formatConditional) *xlsxCfRule
26722673
}
26732674
}
26742675

2676+
// drawConfFmtExp provides function to create conditional formatting rule for
2677+
// expression by given priority, criteria type and format settings.
2678+
func drawConfFmtExp(p int, ct string, format *formatConditional) *xlsxCfRule {
2679+
return &xlsxCfRule{
2680+
Priority: p + 1,
2681+
Type: validType[format.Type],
2682+
Formula: []string{format.Criteria},
2683+
DxfID: &format.Format,
2684+
}
2685+
}
2686+
26752687
// getPaletteColor provides function to convert the RBG color by given string.
26762688
func getPaletteColor(color string) string {
26772689
return "FF" + strings.Replace(strings.ToUpper(color), "#", "", -1)

0 commit comments

Comments
 (0)