Procházet zdrojové kódy

Test unmarshalling fills from testfile.xlsx

Geoffrey J. Teale před 12 roky
rodič
revize
872b35ec62
4 změnil soubory, kde provedl 50 přidání a 6 odebrání
  1. 7 3
      lib.go
  2. 32 1
      lib_test.go
  3. 11 2
      style.go
  4. binární
      testfile.xlsx

+ 7 - 3
lib.go

@@ -39,6 +39,7 @@ func (c *Cell) String() string {
 	return c.Value
 }
 
+// TODO: TestMe!
 func (c *Cell) GetStyle() *Style {
 	style := new(Style)
 	if c.styleIndex > 0 && c.styleIndex < len(c.styles.CellXfs) {
@@ -53,7 +54,9 @@ func (c *Cell) GetStyle() *Style {
 		}
 		if xf.ApplyFill != "0" {
 			var fill Fill
-			fill.BgColorIndex = c.styles.Fills[xf.FillId].BgColorIndex
+			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.Fills = fill
 		}
 	}
@@ -93,8 +96,9 @@ type Border struct {
 // Fill is a high level structure intended to provide user access to
 // the contents of background and foreground color index within an Sheet.
 type Fill struct {
-	BgColorIndex string
-	FgColorIndex string
+	PatternType string
+	BgColor string
+	FgColor string
 }
 
 // File is a high level structure providing a slice of Sheet structs

+ 32 - 1
lib_test.go

@@ -3,6 +3,7 @@ package xlsx
 import (
 	"bytes"
 	"encoding/xml"
+	"fmt"
 	"strconv"
 	"strings"
 	"testing"
@@ -87,15 +88,45 @@ func TestReadSharedStringsFromZipFile(t *testing.T) {
 func TestReadStylesFromZipFile(t *testing.T) {
 	var xlsxFile *File
 	var error error
+	var fontCount, fillCount int
+	var font xlsxFont
+	var fill xlsxFill
+
 	xlsxFile, error = OpenFile("testfile.xlsx")
 	if error != nil {
 		t.Error(error.Error())
 		return
 	}
 	if xlsxFile.styles == nil {
-		t.Error("expected non nil xlsx.styles")
+		t.Error("expected non nil xlsxFile.styles")
+		return
+	}
+	fontCount = len(xlsxFile.styles.Fonts)
+	if fontCount != 4 {
+		t.Error("expected exactly 4 xslxFonts, got ", fontCount)
+		return
+	}
+	font = xlsxFile.styles.Fonts[0]
+	if font.Sz.Val != "11" {
+		t.Error("expected font.Sz.Val == 11, got ", font.Sz.Val)
 		return
 	}
+	if font.Name.Val != "Calibri" {
+		t.Error("expected font.Name.Val == 'Calibri', got ", font.Name.Val)
+		return
+	}
+	fillCount = len(xlsxFile.styles.Fills)
+	if fillCount != 3 {
+		t.Error("Expected exactly 3 xlsxFills, got ", fillCount)
+		return
+	}
+	fill = xlsxFile.styles.Fills[0]
+	fmt.Printf("%v\n", fill)
+	fill = xlsxFile.styles.Fills[1]
+	fmt.Printf("%v\n", fill)
+	fill = xlsxFile.styles.Fills[2]
+	fmt.Printf("%v\n", fill)
+
 }
 
 func TestLettersToNumeric(t *testing.T) {

+ 11 - 2
style.go

@@ -50,8 +50,17 @@ type xlsxVal struct {
 // ...............
 // ...............
 type xlsxFill struct {
-	FgColorIndex string
-	BgColorIndex string
+	PatternFill xlsxPatternFill `xml:"patternFill"`
+}
+
+type xlsxPatternFill struct {
+	PatternType string `xml:"patternType,attr"`
+	FgColor xlsxColor `xml:"fgColor"`
+	BgColor xlsxColor `xml:"bgColor"`
+}
+
+type xlsxColor struct {
+	RGB string `xml:"rgb,attr"`
 }
 
 // xlsxBorder directly maps the border element in the namespace

binární
testfile.xlsx