Browse Source

Add support for border colors

Mathias Fredriksson 10 years ago
parent
commit
476e2867de
2 changed files with 61 additions and 15 deletions
  1. 34 9
      style.go
  2. 27 6
      xmlStyle.go

+ 34 - 9
style.go

@@ -56,10 +56,22 @@ func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBo
 	xPatternFill.FgColor.RGB = style.Fill.FgColor
 	xPatternFill.BgColor.RGB = style.Fill.BgColor
 	xFill.PatternFill = xPatternFill
-	xBorder.Left = xlsxLine{Style: style.Border.Left}
-	xBorder.Right = xlsxLine{Style: style.Border.Right}
-	xBorder.Top = xlsxLine{Style: style.Border.Top}
-	xBorder.Bottom = xlsxLine{Style: style.Border.Bottom}
+	xBorder.Left = xlsxLine{
+		Style: style.Border.Left,
+		Color: xlsxColor{RGB: style.Border.LeftColor},
+	}
+	xBorder.Right = xlsxLine{
+		Style: style.Border.Right,
+		Color: xlsxColor{RGB: style.Border.RightColor},
+	}
+	xBorder.Top = xlsxLine{
+		Style: style.Border.Top,
+		Color: xlsxColor{RGB: style.Border.TopColor},
+	}
+	xBorder.Bottom = xlsxLine{
+		Style: style.Border.Bottom,
+		Color: xlsxColor{RGB: style.Border.BottomColor},
+	}
 	xCellXf = makeXLSXCellElement()
 	xCellXf.ApplyBorder = style.ApplyBorder
 	xCellXf.ApplyFill = style.ApplyFill
@@ -83,14 +95,27 @@ func makeXLSXCellElement() (xCellXf xlsxXf) {
 // Border is a high level structure intended to provide user access to
 // the contents of Border Style within an Sheet.
 type Border struct {
-	Left   string
-	Right  string
-	Top    string
-	Bottom string
+	Left        string
+	LeftColor   string
+	Right       string
+	RightColor  string
+	Top         string
+	TopColor    string
+	Bottom      string
+	BottomColor string
 }
 
 func NewBorder(left, right, top, bottom string) *Border {
-	return &Border{Left: left, Right: right, Top: top, Bottom: bottom}
+	return &Border{
+		Left:        left,
+		LeftColor:   "",
+		Right:       right,
+		RightColor:  "",
+		Top:         top,
+		TopColor:    "",
+		Bottom:      bottom,
+		BottomColor: "",
+	}
 }
 
 // Fill is a high level structure intended to provide user access to

+ 27 - 6
xmlStyle.go

@@ -137,9 +137,13 @@ func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style *Style) {
 			var border xlsxBorder
 			border = styles.Borders.Border[xf.BorderId]
 			style.Border.Left = border.Left.Style
+			style.Border.LeftColor = border.Left.Color.RGB
 			style.Border.Right = border.Right.Style
+			style.Border.RightColor = border.Right.Color.RGB
 			style.Border.Top = border.Top.Style
+			style.Border.TopColor = border.Top.Color.RGB
 			style.Border.Bottom = border.Bottom.Style
+			style.Border.BottomColor = border.Bottom.Color.RGB
 		}
 
 		if xf.FillId > -1 && xf.FillId < styles.Fills.Count {
@@ -698,19 +702,35 @@ func (border *xlsxBorder) Marshal() (result string, err error) {
 	subparts := ""
 	if border.Left.Style != "" {
 		emit = true
-		subparts += fmt.Sprintf(`<left style="%s"/>`, border.Left.Style)
+		subparts += fmt.Sprintf(`<left style="%s">`, border.Left.Style)
+		if border.Left.Color.RGB != "" {
+			subparts += fmt.Sprintf(`<color rgb="%s"/>`, border.Left.Color.RGB)
+		}
+		subparts += `</left>`
 	}
 	if border.Right.Style != "" {
 		emit = true
-		subparts += fmt.Sprintf(`<right style="%s"/>`, border.Right.Style)
+		subparts += fmt.Sprintf(`<right style="%s">`, border.Right.Style)
+		if border.Right.Color.RGB != "" {
+			subparts += fmt.Sprintf(`<color rgb="%s"/>`, border.Right.Color.RGB)
+		}
+		subparts += `</right>`
 	}
 	if border.Top.Style != "" {
 		emit = true
-		subparts += fmt.Sprintf(`<top style="%s"/>`, border.Top.Style)
+		subparts += fmt.Sprintf(`<top style="%s">`, border.Top.Style)
+		if border.Top.Color.RGB != "" {
+			subparts += fmt.Sprintf(`<color rgb="%s"/>`, border.Top.Color.RGB)
+		}
+		subparts += `</top>`
 	}
 	if border.Bottom.Style != "" {
 		emit = true
-		subparts += fmt.Sprintf(`<bottom style="%s"/>`, border.Bottom.Style)
+		subparts += fmt.Sprintf(`<bottom style="%s">`, border.Bottom.Style)
+		if border.Bottom.Color.RGB != "" {
+			subparts += fmt.Sprintf(`<color rgb="%s"/>`, border.Bottom.Color.RGB)
+		}
+		subparts += `</bottom>`
 	}
 	if emit {
 		result += `<border>`
@@ -725,11 +745,12 @@ func (border *xlsxBorder) Marshal() (result string, err error) {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxLine struct {
-	Style string `xml:"style,attr,omitempty"`
+	Style string    `xml:"style,attr,omitempty"`
+	Color xlsxColor `xml:"color,omitempty"`
 }
 
 func (line *xlsxLine) Equals(other xlsxLine) bool {
-	return line.Style == other.Style
+	return line.Style == other.Style && line.Color.Equals(other.Color)
 }
 
 // xlsxCellStyleXfs directly maps the cellStyleXfs element in the