Browse Source

Allow setting of fill patterns on cell.

Geoffrey J. Teale 11 years ago
parent
commit
8faffed7b8
2 changed files with 24 additions and 1 deletions
  1. 13 0
      cell.go
  2. 11 1
      cell_test.go

+ 13 - 0
cell.go

@@ -20,6 +20,10 @@ func (s *Style) SetFont(font Font) {
 	s.Font = font
 }
 
+func (s *Style) SetFill(fill Fill) {
+	s.Fill = fill
+}
+
 // Border is a high level structure intended to provide user access to
 // the contents of Border Style within an Sheet.
 type Border struct {
@@ -37,6 +41,10 @@ type Fill struct {
 	FgColor     string
 }
 
+func NewFill(patternType, fgColor, bgColor string) *Fill {
+	return &Fill{PatternType: patternType, FgColor: fgColor, BgColor: bgColor}
+}
+
 type Font struct {
 	Size    int
 	Name    string
@@ -114,6 +122,11 @@ func (c *Cell) SetStyle(style *Style) int {
 	xFont.Name.Val = style.Font.Name
 	xFont.Family.Val = strconv.Itoa(style.Font.Family)
 	xFont.Charset.Val = strconv.Itoa(style.Font.Charset)
+	xPatternFill := xlsxPatternFill{}
+	xPatternFill.PatternType = style.Fill.PatternType
+	xPatternFill.FgColor.RGB = style.Fill.FgColor
+	xPatternFill.BgColor.RGB = style.Fill.BgColor
+	xFill.PatternFill = xPatternFill
 	c.styles.Fonts = append(c.styles.Fonts, xFont)
 	c.styles.Fills = append(c.styles.Fills, xFill)
 	c.styles.Borders = append(c.styles.Borders, xBorder)

+ 11 - 1
cell_test.go

@@ -128,7 +128,17 @@ func (s *CellSuite) TestSetStyleWithFills(c *C) {
 	sheet := file.AddSheet("Test")
 	row := sheet.AddRow()
 	cell := row.AddCell()
-	fill := NewFill(
+	fill := NewFill("solid", "00FF0000", "FF000000")
+	style := NewStyle()
+	style.SetFill(*fill)
+	cell.SetStyle(style)
+	c.Assert(cell.styleIndex, Equals, 0)
+	c.Assert(cell.styles.Fills, HasLen, 1)
+	xFill := cell.styles.Fills[0]
+	xPatternFill := xFill.PatternFill
+	c.Assert(xPatternFill.PatternType, Equals, "solid")
+	c.Assert(xPatternFill.FgColor.RGB, Equals, "00FF0000")
+	c.Assert(xPatternFill.BgColor.RGB, Equals, "FF000000")
 }