瀏覽代碼

Make Cell.GetStyle handle missing xlsxCellXf.ApplyFill value.

Geoffrey J. Teale 11 年之前
父節點
當前提交
cc0077989c
共有 3 個文件被更改,包括 40 次插入50 次删除
  1. 30 35
      cell.go
  2. 2 12
      cell_test.go
  3. 8 3
      file_test.go

+ 30 - 35
cell.go

@@ -2,9 +2,9 @@ package xlsx
 
 import (
 	"fmt"
+	"math"
 	"strconv"
 	"strings"
-	"math"
 )
 
 // Style is a high level structure intended to provide user access to
@@ -19,18 +19,6 @@ func NewStyle() *Style {
 	return &Style{}
 }
 
-func (s *Style) SetFont(font Font) {
-	s.Font = font
-}
-
-func (s *Style) SetFill(fill Fill) {
-	s.Fill = fill
-}
-
-func (s *Style) SetBorder(border Border) {
-	s.Border = border
-}
-
 // Border is a high level structure intended to provide user access to
 // the contents of Border Style within an Sheet.
 type Border struct {
@@ -66,9 +54,9 @@ func NewFont(size int, name string) *Font {
 // Cell is a high level structure intended to provide user access to
 // the contents of Cell within an xlsx.Row.
 type Cell struct {
-	Value      string
-	styleIndex int
-	styles     *xlsxStyles
+	Value          string
+	styleIndex     int
+	styles         *xlsxStyles
 	numFmtRefTable map[int]xlsxNumFmt
 	date1904       bool
 }
@@ -87,34 +75,41 @@ func (c *Cell) String() string {
 func (c *Cell) GetStyle() Style {
 	var err error
 	style := Style{}
+	style.Border = Border{}
+	style.Fill = Fill{}
+	style.Font = Font{}
+
 	if c.styleIndex > -1 && c.styleIndex <= len(c.styles.CellXfs) {
 		xf := c.styles.CellXfs[c.styleIndex]
+
 		if xf.ApplyBorder {
-			var border Border = Border{}
-			border.Left = c.styles.Borders[xf.BorderId].Left.Style
-			border.Right = c.styles.Borders[xf.BorderId].Right.Style
-			border.Top = c.styles.Borders[xf.BorderId].Top.Style
-			border.Bottom = c.styles.Borders[xf.BorderId].Bottom.Style
-			style.SetBorder(border)
+			style.Border.Left = c.styles.Borders[xf.BorderId].Left.Style
+			style.Border.Right = c.styles.Borders[xf.BorderId].Right.Style
+			style.Border.Top = c.styles.Borders[xf.BorderId].Top.Style
+			style.Border.Bottom = c.styles.Borders[xf.BorderId].Bottom.Style
 		}
-		if xf.ApplyFill {
-			var fill Fill = Fill{}
-			fill.PatternType = c.styles.Fills[xf.FillId].PatternFill.PatternType
-			fill.BgColor = c.styles.Fills[xf.FillId].PatternFill.BgColor.RGB
-			fill.FgColor = c.styles.Fills[xf.FillId].PatternFill.FgColor.RGB
-			style.SetFill(fill)
+		// TODO - consider how to handle the fact that
+		// ApplyFill can be missing.  At the moment the XML
+		// unmarshaller simply sets it to false, which creates
+		// a bug.
+
+		// if xf.ApplyFill {
+		if xf.FillId > -1 && xf.FillId < len(c.styles.Fills) {
+			xFill := c.styles.Fills[xf.FillId]
+			style.Fill.PatternType = xFill.PatternFill.PatternType
+			style.Fill.FgColor = xFill.PatternFill.FgColor.RGB
+			style.Fill.BgColor = xFill.PatternFill.BgColor.RGB
 		}
+		// }
 		if xf.ApplyFont {
-			var xfont = c.styles.Fonts[xf.FontId]
-			var font = Font{}
-			font.Size, err = strconv.Atoi(xfont.Sz.Val)
+			xfont := c.styles.Fonts[xf.FontId]
+			style.Font.Size, err = strconv.Atoi(xfont.Sz.Val)
 			if err != nil {
 				panic(err.Error())
 			}
-			font.Name = xfont.Name.Val
-			font.Family, _ = strconv.Atoi(xfont.Family.Val)
-			font.Charset, _ = strconv.Atoi(xfont.Charset.Val)
-			style.SetFont(font)
+			style.Font.Name = xfont.Name.Val
+			style.Font.Family, _ = strconv.Atoi(xfont.Family.Val)
+			style.Font.Charset, _ = strconv.Atoi(xfont.Charset.Val)
 		}
 	}
 	return style

+ 2 - 12
cell_test.go

@@ -15,16 +15,6 @@ func (s *StyleSuite) TestNewStyle(c *C){
 }
 
 
-// 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{})
@@ -84,7 +74,7 @@ func (s *CellSuite) TestSetStyleWithFonts(c *C) {
 	cell := row.AddCell()
 	font := NewFont(12, "Calibra")
 	style := NewStyle()
-	style.SetFont(*font)
+	style.Font = *font
 	cell.SetStyle(style)
 	c.Assert(cell.styleIndex, Equals, 0)
 	c.Assert(cell.styles.Fonts, HasLen, 1)
@@ -129,7 +119,7 @@ func (s *CellSuite) TestSetStyleWithFills(c *C) {
 	cell := row.AddCell()
 	fill := NewFill("solid", "00FF0000", "FF000000")
 	style := NewStyle()
-	style.SetFill(*fill)
+	style.Fill = *fill
 	cell.SetStyle(style)
 	c.Assert(cell.styleIndex, Equals, 0)
 	c.Assert(cell.styles.Fills, HasLen, 1)

+ 8 - 3
file_test.go

@@ -141,9 +141,11 @@ func (l *FileSuite) TestReadWorkbookRelationsFromZipFile(c *C) {
 	c.Assert(sheet, NotNil)
 }
 
+// +build fudge
 func (l *FileSuite) TestGetStyleFromZipFile(c *C) {
 	var xlsxFile *File
 	var err error
+	var style Style
 
 	xlsxFile, err = OpenFile("testfile.xlsx")
 	c.Assert(err, IsNil)
@@ -154,14 +156,17 @@ func (l *FileSuite) TestGetStyleFromZipFile(c *C) {
 
 	row0 := tabelle1.Rows[0]
 	cellFoo := row0.Cells[0]
+	style = cellFoo.GetStyle()
 	c.Assert(cellFoo.String(), Equals, "Foo")
-	// style := cellFoo.GetStyle()
-	// c.Assert(style.Fill.BgColor, Equals, "FF33CCCC")
+	c.Assert(style.Fill.BgColor, Equals, "FF33CCCC")
+
 
 	row1 := tabelle1.Rows[1]
 	cellQuuk := row1.Cells[1]
+	style = cellQuuk.GetStyle()
 	c.Assert(cellQuuk.String(), Equals, "Quuk")
-	c.Assert(cellQuuk.GetStyle().Border.Left, Equals, "thin")
+	c.Assert(style.Border.Left, Equals, "thin")
+
 
 	cellBar := row0.Cells[1]
 	c.Assert(cellBar.String(), Equals, "Bar")