Browse Source

Tests style elements getting added into the styles.

Geoffrey J. Teale 11 years ago
parent
commit
a1ecaf804a
5 changed files with 52 additions and 9 deletions
  1. 5 4
      cell.go
  2. 2 1
      file.go
  3. 8 1
      sheet.go
  4. 36 2
      sheet_test.go
  5. 1 1
      xmlStyle.go

+ 5 - 4
cell.go

@@ -32,6 +32,11 @@ func (c *Cell) GetStyle() Style {
 	return c.style
 }
 
+// SetStyle sets the style of a cell.
+func (c *Cell) SetStyle(style Style) {
+	c.style = style
+}
+
 // The number format string is returnable from a cell.
 func (c *Cell) GetNumberFormat() string {
 	return c.numFmt
@@ -176,7 +181,3 @@ func (c *Cell) FormattedValue() string {
 	}
 	return c.Value
 }
-
-func (c *Cell) SetStyle(style Style) {
-	c.style = style
-}

+ 2 - 1
file.go

@@ -144,8 +144,9 @@ func (f *File) MarshallParts() (map[string]string, error) {
 	workbook = f.makeWorkbook()
 	sheetIndex := 1
 
+	styles := &xlsxStyles{}
 	for _, sheet := range f.Sheets {
-		xSheet := sheet.makeXLSXSheet(refTable)
+		xSheet := sheet.makeXLSXSheet(refTable, styles)
 		rId := fmt.Sprintf("rId%d", sheetIndex)
 		sheetId := strconv.Itoa(sheetIndex)
 		sheetPath := fmt.Sprintf("worksheets/sheet%d.xml", sheetIndex)

+ 8 - 1
sheet.go

@@ -42,7 +42,7 @@ func (sh *Sheet) Cell(row, col int) *Cell {
 }
 
 // Dump sheet to it's XML representation, intended for internal use only
-func (s *Sheet) makeXLSXSheet(refTable *RefTable) *xlsxWorksheet {
+func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyles) *xlsxWorksheet {
 	worksheet := &xlsxWorksheet{}
 	xSheet := xlsxSheetData{}
 	maxRow := 0
@@ -54,6 +54,13 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable) *xlsxWorksheet {
 		xRow := xlsxRow{}
 		xRow.R = r + 1
 		for c, cell := range row.Cells {
+			style := cell.GetStyle()
+			xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
+			styles.addFont(xFont)
+			styles.addFill(xFill)
+			styles.addBorder(xBorder)
+			styles.addCellStyleXf(xCellStyleXf)
+			styles.addCellXf(xCellXf)
 			if c > maxCell {
 				maxCell = c
 			}

+ 36 - 2
sheet_test.go

@@ -3,6 +3,7 @@ package xlsx
 import (
 	"bytes"
 	"encoding/xml"
+
 	. "gopkg.in/check.v1"
 )
 
@@ -27,7 +28,8 @@ func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {
 	cell := row.AddCell()
 	cell.Value = "A cell!"
 	refTable := NewSharedStringRefTable()
-	xSheet := sheet.makeXLSXSheet(refTable)
+	styles := &xlsxStyles{}
+	xSheet := sheet.makeXLSXSheet(refTable, styles)
 	c.Assert(xSheet.Dimension.Ref, Equals, "A1:A1")
 	c.Assert(xSheet.SheetData.Row, HasLen, 1)
 	xRow := xSheet.SheetData.Row[0]
@@ -47,6 +49,37 @@ func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {
 	c.Assert(xSI.T, Equals, "A cell!")
 }
 
+// When we create the xlsxSheet we also populate the xlsxStyles struct
+// with style information.
+func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
+	file := NewFile()
+	sheet := file.AddSheet("Sheet1")
+	row := sheet.AddRow()
+	cell := row.AddCell()
+	cell.Value = "A cell!"
+	style := *NewStyle()
+	style.Font = *NewFont(10, "Verdana")
+	style.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
+	style.Border = *NewBorder("none", "thin", "none", "thin")
+	cell.SetStyle(style)
+	refTable := NewSharedStringRefTable()
+	styles := &xlsxStyles{}
+	_ = sheet.makeXLSXSheet(refTable, styles)
+	c.Assert(len(styles.Fonts), Equals, 1)
+	// YOU ARE HERE
+	c.Assert(styles.Fonts[0].Sz.Val, Equals, "10")
+	c.Assert(styles.Fonts[0].Name.Val, Equals, "Verdana")
+	c.Assert(len(styles.Fills), Equals, 1)
+	c.Assert(styles.Fills[0].PatternFill.PatternType, Equals, "solid")
+	c.Assert(styles.Fills[0].PatternFill.FgColor.RGB, Equals, "FFFFFFFF")
+	c.Assert(styles.Fills[0].PatternFill.BgColor.RGB, Equals, "00000000")
+	c.Assert(len(styles.Borders), Equals, 1)
+	c.Assert(styles.Borders[0].Left.Style, Equals, "none")
+	c.Assert(styles.Borders[0].Right.Style, Equals, "thin")
+	c.Assert(styles.Borders[0].Top.Style, Equals, "none")
+	c.Assert(styles.Borders[0].Bottom.Style, Equals, "thin")
+}
+
 func (s *SheetSuite) TestMarshalSheet(c *C) {
 	file := NewFile()
 	sheet := file.AddSheet("Sheet1")
@@ -54,7 +87,8 @@ func (s *SheetSuite) TestMarshalSheet(c *C) {
 	cell := row.AddCell()
 	cell.Value = "A cell!"
 	refTable := NewSharedStringRefTable()
-	xSheet := sheet.makeXLSXSheet(refTable)
+	styles := &xlsxStyles{}
+	xSheet := sheet.makeXLSXSheet(refTable, styles)
 
 	output := bytes.NewBufferString(xml.Header)
 	body, err := xml.MarshalIndent(xSheet, "  ", "  ")

+ 1 - 1
xmlStyle.go

@@ -191,7 +191,7 @@ func (styles *xlsxStyles) addBorder(xBorder xlsxBorder) {
 	styles.Borders = append(styles.Borders, xBorder)
 }
 
-func (styles *xlsxStyles) addCellStyleXfs(xCellStyleXf xlsxXf) {
+func (styles *xlsxStyles) addCellStyleXf(xCellStyleXf xlsxXf) {
 	styles.CellStyleXfs = append(styles.CellStyleXfs, xCellStyleXf)
 }