Browse Source

Create xlsxStyles from a sheet's Styles.

Geoffrey J. Teale 11 years ago
parent
commit
f2931eadfb
2 changed files with 86 additions and 0 deletions
  1. 35 0
      cell.go
  2. 51 0
      cell_test.go

+ 35 - 0
cell.go

@@ -12,6 +12,14 @@ type Style struct {
 	Font   Font
 }
 
+func NewStyle() *Style {
+	return &Style{}
+}
+
+func (s *Style) SetFont(font Font) {
+	s.Font = font
+}
+
 // Border is a high level structure intended to provide user access to
 // the contents of Border Style within an Sheet.
 type Border struct {
@@ -36,6 +44,10 @@ type Font struct {
 	Charset int
 }
 
+func NewFont(size int, name string) *Font {
+	return &Font{Size: size, Name: name}
+}
+
 // Cell is a high level structure intended to provide user access to
 // the contents of Cell within an xlsx.Row.
 type Cell struct {
@@ -86,3 +98,26 @@ func (c *Cell) GetStyle() *Style {
 	}
 	return style
 }
+
+
+func (c *Cell) SetStyle(style *Style) int {
+	if c.styles == nil {
+		c.styles = &xlsxStyles{}
+	}
+	index := len(c.styles.Fonts)
+	xFont := xlsxFont{}
+	xFill := xlsxFill{}
+	xBorder := xlsxBorder{}
+	xCellStyleXf := xlsxXf{}
+	xCellXf := xlsxXf{}
+	xFont.Sz.Val = strconv.Itoa(style.Font.Size)
+	xFont.Name.Val = style.Font.Name
+	xFont.Family.Val = strconv.Itoa(style.Font.Family)
+	xFont.Charset.Val = strconv.Itoa(style.Font.Charset)
+	c.styles.Fonts = append(c.styles.Fonts, xFont)
+	c.styles.Fills = append(c.styles.Fills, xFill)
+	c.styles.Borders = append(c.styles.Borders, xBorder)
+	c.styles.CellStyleXfs = append(c.styles.CellStyleXfs, xCellStyleXf)
+	c.styles.CellXfs = append(c.styles.CellXfs, xCellXf)
+	return index
+}

+ 51 - 0
cell_test.go

@@ -4,6 +4,38 @@ import (
 	. "gopkg.in/check.v1"
 )
 
+type StyleSuite struct {}
+
+var _ = Suite(&StyleSuite{})
+
+
+func (s *StyleSuite) TestNewStyle(c *C){
+	style := NewStyle()
+	c.Assert(style, NotNil)
+}
+
+
+// Test that SetFont correctly updates the Font associated with a Style.
+func (s *StyleSuite) TestSetFont(c *C) {
+	font := NewFont(12, "Calibra")
+	style := Style{}
+	style.SetFont(*font)
+	c.Assert(style.Font.Size, Equals, 12)
+	c.Assert(style.Font.Name, Equals, "Calibra")
+}
+
+
+type FontSuite struct {}
+
+var _ = Suite(&FontSuite{})
+
+func (s *FontSuite) TestNewFont(c *C) {
+	font := NewFont(12, "Verdana")
+	c.Assert(font, NotNil)
+	c.Assert(font.Name, Equals, "Verdana")
+	c.Assert(font.Size, Equals, 12)
+}
+
 type CellSuite struct {}
 
 var _ = Suite(&CellSuite{})
@@ -44,6 +76,25 @@ func (s *CellSuite) TestGetStyleWithFonts(c *C) {
 	c.Assert(style.Font.Name, Equals, "Calibra")
 }
 
+
+// Test that SetStyle correctly updates the xlsxStyle.Fonts.
+func (s *CellSuite) TestSetStyleWithFonts(c *C) {
+	file := NewFile()
+	sheet := file.AddSheet("Test")
+	row := sheet.AddRow()
+	cell := row.AddCell()
+	font := NewFont(12, "Calibra")
+	style := NewStyle()
+	style.SetFont(*font)
+	cell.SetStyle(style)
+	c.Assert(cell.styleIndex, Equals, 0)
+	c.Assert(cell.styles.Fonts, HasLen, 1)
+	xFont := cell.styles.Fonts[0]
+	c.Assert(xFont.Sz.Val, Equals, "12")
+	c.Assert(xFont.Name.Val, Equals, "Calibra")
+}
+
+
 // Test that GetStyle correctly converts the xlsxStyle.Fills.
 func (s *CellSuite) TestGetStyleWithFills(c *C) {
 	var cell *Cell