Skip to content

Commit

Permalink
- Conditional format with formula support, relate issue qax-os#75;
Browse files Browse the repository at this point in the history
- go test and readme update
  • Loading branch information
xuri committed Aug 18, 2017
1 parent 58e2caf commit 77af252
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 77af252

Please sign in to comment.