- Conditional format with formula support, relate issue #75;

- go test and readme update
This commit is contained in:
Ri Xu 2017-08-18 20:55:27 +08:00
parent 58e2caff33
commit 77af25295e
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 18 additions and 4 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)