diff options
author | xuri <xuri.me@gmail.com> | 2018-09-27 23:40:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-27 23:40:00 +0800 |
commit | 204139739a34aa6e6c5ea8764008276a6d184ff6 (patch) | |
tree | 0b9a795ea20804753134fd54c40d17c5a01c64f6 /styles.go | |
parent | 250946568ca1e5a69c07f19dff4d1d3a2264e31d (diff) | |
parent | 3e004d900b103379c2d62657a3070de4a2e8585a (diff) |
Merge branch 'master' into master
Diffstat (limited to 'styles.go')
-rw-r--r-- | styles.go | 265 |
1 files changed, 160 insertions, 105 deletions
@@ -1,3 +1,12 @@ +// Copyright 2016 - 2018 The excelize Authors. All rights reserved. Use of +// this source code is governed by a BSD-style license that can be found in +// the LICENSE file. +// +// Package excelize 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. + package excelize import ( @@ -10,8 +19,8 @@ import ( ) // Excel styles can reference number formats that are built-in, all of which -// have an id less than 164. This is a possibly incomplete list comprised of as -// many of them as I could find. +// have an id less than 164. This is a possibly incomplete list comprised of +// as many of them as I could find. var builtInNumFmt = map[int]string{ 0: "general", 1: "0", @@ -798,45 +807,46 @@ var validType = map[string]string{ // criteriaType defined the list of valid criteria types. var criteriaType = map[string]string{ - "between": "between", - "not between": "notBetween", - "equal to": "equal", - "=": "equal", - "==": "equal", - "not equal to": "notEqual", - "!=": "notEqual", - "<>": "notEqual", - "greater than": "greaterThan", - ">": "greaterThan", - "less than": "lessThan", - "<": "lessThan", + "between": "between", + "not between": "notBetween", + "equal to": "equal", + "=": "equal", + "==": "equal", + "not equal to": "notEqual", + "!=": "notEqual", + "<>": "notEqual", + "greater than": "greaterThan", + ">": "greaterThan", + "less than": "lessThan", + "<": "lessThan", "greater than or equal to": "greaterThanOrEqual", - ">=": "greaterThanOrEqual", - "less than or equal to": "lessThanOrEqual", - "<=": "lessThanOrEqual", - "containing": "containsText", - "not containing": "notContains", - "begins with": "beginsWith", - "ends with": "endsWith", - "yesterday": "yesterday", - "today": "today", - "last 7 days": "last7Days", - "last week": "lastWeek", - "this week": "thisWeek", - "continue week": "continueWeek", - "last month": "lastMonth", - "this month": "thisMonth", - "continue month": "continueMonth", + ">=": "greaterThanOrEqual", + "less than or equal to": "lessThanOrEqual", + "<=": "lessThanOrEqual", + "containing": "containsText", + "not containing": "notContains", + "begins with": "beginsWith", + "ends with": "endsWith", + "yesterday": "yesterday", + "today": "today", + "last 7 days": "last7Days", + "last week": "lastWeek", + "this week": "thisWeek", + "continue week": "continueWeek", + "last month": "lastMonth", + "this month": "thisMonth", + "continue month": "continueMonth", } -// formatToString provides function to return original string by given built-in -// number formats code and cell string. +// formatToString provides a function to return original string by given +// built-in number formats code and cell string. func formatToString(i int, v string) string { return v } -// formatToInt provides function to convert original string to integer format as -// string type by given built-in number formats code and cell string. +// formatToInt provides a function to convert original string to integer +// format as string type by given built-in number formats code and cell +// string. func formatToInt(i int, v string) string { f, err := strconv.ParseFloat(v, 64) if err != nil { @@ -845,8 +855,9 @@ func formatToInt(i int, v string) string { return fmt.Sprintf("%d", int(f)) } -// formatToFloat provides function to convert original string to float format as -// string type by given built-in number formats code and cell string. +// formatToFloat provides a function to convert original string to float +// format as string type by given built-in number formats code and cell +// string. func formatToFloat(i int, v string) string { f, err := strconv.ParseFloat(v, 64) if err != nil { @@ -855,8 +866,8 @@ func formatToFloat(i int, v string) string { return fmt.Sprintf("%.2f", f) } -// formatToA provides function to convert original string to special format as -// string type by given built-in number formats code and cell string. +// formatToA provides a function to convert original string to special format +// as string type by given built-in number formats code and cell string. func formatToA(i int, v string) string { f, err := strconv.ParseFloat(v, 64) if err != nil { @@ -870,8 +881,8 @@ func formatToA(i int, v string) string { return fmt.Sprintf("%d", t) } -// formatToB provides function to convert original string to special format as -// string type by given built-in number formats code and cell string. +// formatToB provides a function to convert original string to special format +// as string type by given built-in number formats code and cell string. func formatToB(i int, v string) string { f, err := strconv.ParseFloat(v, 64) if err != nil { @@ -883,8 +894,8 @@ func formatToB(i int, v string) string { return fmt.Sprintf("%.2f", f) } -// formatToC provides function to convert original string to special format as -// string type by given built-in number formats code and cell string. +// formatToC provides a function to convert original string to special format +// as string type by given built-in number formats code and cell string. func formatToC(i int, v string) string { f, err := strconv.ParseFloat(v, 64) if err != nil { @@ -894,8 +905,8 @@ func formatToC(i int, v string) string { return fmt.Sprintf("%d%%", int(f)) } -// formatToD provides function to convert original string to special format as -// string type by given built-in number formats code and cell string. +// formatToD provides a function to convert original string to special format +// as string type by given built-in number formats code and cell string. func formatToD(i int, v string) string { f, err := strconv.ParseFloat(v, 64) if err != nil { @@ -905,8 +916,8 @@ func formatToD(i int, v string) string { return fmt.Sprintf("%.2f%%", f) } -// formatToE provides function to convert original string to special format as -// string type by given built-in number formats code and cell string. +// formatToE provides a function to convert original string to special format +// as string type by given built-in number formats code and cell string. func formatToE(i int, v string) string { f, err := strconv.ParseFloat(v, 64) if err != nil { @@ -915,17 +926,17 @@ func formatToE(i int, v string) string { return fmt.Sprintf("%.e", f) } -// parseTime provides function to returns a string parsed using time.Time. +// parseTime provides a function to returns a string parsed using time.Time. // Replace Excel placeholders with Go time placeholders. For example, replace -// yyyy with 2006. These are in a specific order, due to the fact that m is used -// in month, minute, and am/pm. It would be easier to fix that with regular -// expressions, but if it's possible to keep this simple it would be easier to -// maintain. Full-length month and days (e.g. March, Tuesday) have letters in -// them that would be replaced by other characters below (such as the 'h' in -// March, or the 'd' in Tuesday) below. First we convert them to arbitrary -// characters unused in Excel Date formats, and then at the end, turn them to -// what they should actually be. -// Based off: http://www.ozgrid.com/Excel/CustomFormats.htm +// yyyy with 2006. These are in a specific order, due to the fact that m is +// used in month, minute, and am/pm. It would be easier to fix that with +// regular expressions, but if it's possible to keep this simple it would be +// easier to maintain. Full-length month and days (e.g. March, Tuesday) have +// letters in them that would be replaced by other characters below (such as +// the 'h' in March, or the 'd' in Tuesday) below. First we convert them to +// arbitrary characters unused in Excel Date formats, and then at the end, +// turn them to what they should actually be. Based off: +// http://www.ozgrid.com/Excel/CustomFormats.htm func parseTime(i int, v string) string { f, err := strconv.ParseFloat(v, 64) if err != nil { @@ -983,7 +994,7 @@ func is12HourTime(format string) bool { return strings.Contains(format, "am/pm") || strings.Contains(format, "AM/PM") || strings.Contains(format, "a/p") || strings.Contains(format, "A/P") } -// stylesReader provides function to get the pointer to the structure after +// stylesReader provides a function to get the pointer to the structure after // deserialization of xl/styles.xml. func (f *File) stylesReader() *xlsxStyleSheet { if f.Styles == nil { @@ -994,7 +1005,7 @@ func (f *File) stylesReader() *xlsxStyleSheet { return f.Styles } -// styleSheetWriter provides function to save xl/styles.xml after serialize +// styleSheetWriter provides a function to save xl/styles.xml after serialize // structure. func (f *File) styleSheetWriter() { if f.Styles != nil { @@ -1003,7 +1014,7 @@ func (f *File) styleSheetWriter() { } } -// parseFormatStyleSet provides function to parse the format settings of the +// parseFormatStyleSet provides a function to parse the format settings of the // cells and conditional formats. func parseFormatStyleSet(style string) (*formatStyle, error) { format := formatStyle{ @@ -1013,8 +1024,8 @@ func parseFormatStyleSet(style string) (*formatStyle, error) { return &format, err } -// NewStyle provides function to create style for cells by given style format. -// Note that the color field uses RGB color code. +// NewStyle provides a function to create style for cells by given style +// format. Note that the color field uses RGB color code. // // The following shows the border styles sorted by excelize index number: // @@ -1404,7 +1415,7 @@ func parseFormatStyleSet(style string) (*formatStyle, error) { // 173 | $ English (New Zealand) // 174 | $ English (Singapore) // 175 | $ English (Trinidad & Tobago) -// 176 | $ English (U.S. Vigin Islands) +// 176 | $ English (U.S. Virgin Islands) // 177 | $ English (United States) // 178 | $ French (Canada) // 179 | $ Hawaiian (United States) @@ -1906,10 +1917,10 @@ func (f *File) NewStyle(style string) (int, error) { return cellXfsID, nil } -// NewConditionalStyle provides function to create style for conditional format -// by given style format. The parameters are the same as function NewStyle(). -// Note that the color field uses RGB color code and only support to set font, -// fills, alignment and borders currently. +// NewConditionalStyle provides a function to create style for conditional +// format by given style format. The parameters are the same as function +// NewStyle(). Note that the color field uses RGB color code and only support +// to set font, fills, alignment and borders currently. func (f *File) NewConditionalStyle(style string) (int, error) { s := f.stylesReader() fs, err := parseFormatStyleSet(style) @@ -1935,7 +1946,8 @@ func (f *File) NewConditionalStyle(style string) (int, error) { return s.Dxfs.Count - 1, nil } -// setFont provides function to add font style by given cell format settings. +// setFont provides a function to add font style by given cell format +// settings. func setFont(formatStyle *formatStyle) *font { fontUnderlineType := map[string]string{"single": "single", "double": "double"} if formatStyle.Font.Size < 1 { @@ -1963,8 +1975,8 @@ func setFont(formatStyle *formatStyle) *font { return &f } -// setNumFmt provides function to check if number format code in the range of -// built-in values. +// setNumFmt provides a function to check if number format code in the range +// of built-in values. func setNumFmt(style *xlsxStyleSheet, formatStyle *formatStyle) int { dp := "0." numFmtID := 164 // Default custom number format code from 164. @@ -2011,7 +2023,7 @@ func setNumFmt(style *xlsxStyleSheet, formatStyle *formatStyle) int { return formatStyle.NumFmt } -// setCustomNumFmt provides function to set custom number format code. +// setCustomNumFmt provides a function to set custom number format code. func setCustomNumFmt(style *xlsxStyleSheet, formatStyle *formatStyle) int { nf := xlsxNumFmt{FormatCode: *formatStyle.CustomNumFmt} if style.NumFmts != nil { @@ -2029,7 +2041,7 @@ func setCustomNumFmt(style *xlsxStyleSheet, formatStyle *formatStyle) int { return nf.NumFmtID } -// setLangNumFmt provides function to set number format code with language. +// setLangNumFmt provides a function to set number format code with language. func setLangNumFmt(style *xlsxStyleSheet, formatStyle *formatStyle) int { numFmts, ok := langNumFmt[formatStyle.Lang] if !ok { @@ -2056,8 +2068,8 @@ func setLangNumFmt(style *xlsxStyleSheet, formatStyle *formatStyle) int { return nf.NumFmtID } -// setFills provides function to add fill elements in the styles.xml by given -// cell format settings. +// setFills provides a function to add fill elements in the styles.xml by +// given cell format settings. func setFills(formatStyle *formatStyle, fg bool) *xlsxFill { var patterns = []string{ "none", @@ -2137,9 +2149,10 @@ func setFills(formatStyle *formatStyle, fg bool) *xlsxFill { return &fill } -// setAlignment provides function to formatting information pertaining to text -// alignment in cells. There are a variety of choices for how text is aligned -// both horizontally and vertically, as well as indentation settings, and so on. +// setAlignment provides a function to formatting information pertaining to +// text alignment in cells. There are a variety of choices for how text is +// aligned both horizontally and vertically, as well as indentation settings, +// and so on. func setAlignment(formatStyle *formatStyle) *xlsxAlignment { var alignment xlsxAlignment if formatStyle.Alignment != nil { @@ -2156,7 +2169,7 @@ func setAlignment(formatStyle *formatStyle) *xlsxAlignment { return &alignment } -// setProtection provides function to set protection properties associated +// setProtection provides a function to set protection properties associated // with the cell. func setProtection(formatStyle *formatStyle) *xlsxProtection { var protection xlsxProtection @@ -2167,7 +2180,7 @@ func setProtection(formatStyle *formatStyle) *xlsxProtection { return &protection } -// setBorders provides function to add border elements in the styles.xml by +// setBorders provides a function to add border elements in the styles.xml by // given borders format settings. func setBorders(formatStyle *formatStyle) *xlsxBorder { var styles = []string{ @@ -2219,7 +2232,7 @@ func setBorders(formatStyle *formatStyle) *xlsxBorder { return &border } -// setCellXfs provides function to set describes all of the formatting for a +// setCellXfs provides a function to set describes all of the formatting for a // cell. func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, applyAlignment, applyProtection bool, alignment *xlsxAlignment, protection *xlsxProtection) int { var xf xlsxXf @@ -2246,9 +2259,10 @@ func setCellXfs(style *xlsxStyleSheet, fontID, numFmtID, fillID, borderID int, a return style.CellXfs.Count - 1 } -// SetCellStyle provides function to add style attribute for cells by given +// SetCellStyle provides a function to add style attribute for cells by given // worksheet name, coordinate area and style ID. Note that diagonalDown and -// diagonalUp type border should be use same color in the same coordinate area. +// diagonalUp type border should be use same color in the same coordinate +// area. // // For example create a borders of cell H9 on Sheet1: // @@ -2352,9 +2366,10 @@ func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) { } } -// SetConditionalFormat provides function to create conditional formatting rule -// for cell value. Conditional formatting is a feature of Excel which allows you -// to apply a format to a cell or a range of cells based on certain criteria. +// SetConditionalFormat provides a function to create conditional formatting +// rule for cell value. Conditional formatting is a feature of Excel which +// allows you to apply a format to a cell or a range of cells based on certain +// criteria. // // The type option is a required parameter and it has no default value. // Allowable type values and their associated parameters are: @@ -2407,7 +2422,7 @@ func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) { // // The criteria parameter is used to set the criteria by which the cell data // will be evaluated. It has no default value. The most common criteria as -// applied to {'type': 'cell'} are: +// applied to {"type":"cell"} are: // // between | // not between | @@ -2606,9 +2621,9 @@ func (f *File) SetConditionalFormat(sheet, area, formatSet string) error { return err } -// drawCondFmtCellIs provides function to create conditional formatting rule for -// cell value (include between, not between, equal, not equal, greater than and -// less than) by given priority, criteria type and format settings. +// drawCondFmtCellIs provides a function to create conditional formatting rule +// for cell value (include between, not between, equal, not equal, greater +// than and less than) by given priority, criteria type and format settings. func drawCondFmtCellIs(p int, ct string, format *formatConditional) *xlsxCfRule { c := &xlsxCfRule{ Priority: p + 1, @@ -2629,8 +2644,8 @@ func drawCondFmtCellIs(p int, ct string, format *formatConditional) *xlsxCfRule return c } -// drawCondFmtTop10 provides function to create conditional formatting rule for -// top N (default is top 10) by given priority, criteria type and format +// drawCondFmtTop10 provides a function to create conditional formatting rule +// for top N (default is top 10) by given priority, criteria type and format // settings. func drawCondFmtTop10(p int, ct string, format *formatConditional) *xlsxCfRule { c := &xlsxCfRule{ @@ -2647,9 +2662,9 @@ func drawCondFmtTop10(p int, ct string, format *formatConditional) *xlsxCfRule { return c } -// drawCondFmtAboveAverage provides function to create conditional formatting -// rule for above average and below average by given priority, criteria type and -// format settings. +// drawCondFmtAboveAverage provides a function to create conditional +// formatting rule for above average and below average by given priority, +// criteria type and format settings. func drawCondFmtAboveAverage(p int, ct string, format *formatConditional) *xlsxCfRule { return &xlsxCfRule{ Priority: p + 1, @@ -2659,7 +2674,7 @@ func drawCondFmtAboveAverage(p int, ct string, format *formatConditional) *xlsxC } } -// drawCondFmtDuplicateUniqueValues provides function to create conditional +// drawCondFmtDuplicateUniqueValues provides a function to create conditional // formatting rule for duplicate and unique values by given priority, criteria // type and format settings. func drawCondFmtDuplicateUniqueValues(p int, ct string, format *formatConditional) *xlsxCfRule { @@ -2670,16 +2685,29 @@ func drawCondFmtDuplicateUniqueValues(p int, ct string, format *formatConditiona } } -// drawCondFmtColorScale provides function to create conditional formatting rule -// for color scale (include 2 color scale and 3 color scale) by given priority, -// criteria type and format settings. +// drawCondFmtColorScale provides a function to create conditional formatting +// rule for color scale (include 2 color scale and 3 color scale) by given +// priority, criteria type and format settings. func drawCondFmtColorScale(p int, ct string, format *formatConditional) *xlsxCfRule { + minValue := format.MinValue + if minValue == "" { + minValue = "0" + } + maxValue := format.MaxValue + if maxValue == "" { + maxValue = "0" + } + midValue := format.MidValue + if midValue == "" { + midValue = "50" + } + c := &xlsxCfRule{ Priority: p + 1, Type: "colorScale", ColorScale: &xlsxColorScale{ Cfvo: []*xlsxCfvo{ - {Type: format.MinType}, + {Type: format.MinType, Val: minValue}, }, Color: []*xlsxColor{ {RGB: getPaletteColor(format.MinColor)}, @@ -2687,16 +2715,16 @@ func drawCondFmtColorScale(p int, ct string, format *formatConditional) *xlsxCfR }, } if validType[format.Type] == "3_color_scale" { - c.ColorScale.Cfvo = append(c.ColorScale.Cfvo, &xlsxCfvo{Type: format.MidType, Val: 50}) + c.ColorScale.Cfvo = append(c.ColorScale.Cfvo, &xlsxCfvo{Type: format.MidType, Val: midValue}) c.ColorScale.Color = append(c.ColorScale.Color, &xlsxColor{RGB: getPaletteColor(format.MidColor)}) } - c.ColorScale.Cfvo = append(c.ColorScale.Cfvo, &xlsxCfvo{Type: format.MaxType}) + c.ColorScale.Cfvo = append(c.ColorScale.Cfvo, &xlsxCfvo{Type: format.MaxType, Val: maxValue}) c.ColorScale.Color = append(c.ColorScale.Color, &xlsxColor{RGB: getPaletteColor(format.MaxColor)}) return c } -// drawCondFmtDataBar provides function to create conditional formatting rule -// for data bar by given priority, criteria type and format settings. +// drawCondFmtDataBar provides a function to create conditional formatting +// rule for data bar by given priority, criteria type and format settings. func drawCondFmtDataBar(p int, ct string, format *formatConditional) *xlsxCfRule { return &xlsxCfRule{ Priority: p + 1, @@ -2708,8 +2736,8 @@ 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. +// drawConfFmtExp provides a 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, @@ -2719,7 +2747,34 @@ func drawConfFmtExp(p int, ct string, format *formatConditional) *xlsxCfRule { } } -// getPaletteColor provides function to convert the RBG color by given string. +// getPaletteColor provides a function to convert the RBG color by given +// string. func getPaletteColor(color string) string { return "FF" + strings.Replace(strings.ToUpper(color), "#", "", -1) } + +// themeReader provides a function to get the pointer to the xl/theme/theme1.xml +// structure after deserialization. +func (f *File) themeReader() *xlsxTheme { + var theme xlsxTheme + _ = xml.Unmarshal([]byte(f.readXML("xl/theme/theme1.xml")), &theme) + return &theme +} + +// ThemeColor applied the color with tint value. +func ThemeColor(baseColor string, tint float64) string { + if tint == 0 { + return "FF" + baseColor + } + r, _ := strconv.ParseInt(baseColor[0:2], 16, 64) + g, _ := strconv.ParseInt(baseColor[2:4], 16, 64) + b, _ := strconv.ParseInt(baseColor[4:6], 16, 64) + h, s, l := RGBToHSL(uint8(r), uint8(g), uint8(b)) + if tint < 0 { + l *= (1 + tint) + } else { + l = l*(1-tint) + (1 - (1 - tint)) + } + br, bg, bb := HSLToRGB(h, s, l) + return fmt.Sprintf("FF%02X%02X%02X", br, bg, bb) +} |