Browse Source

Test copying Font information into user level cell style.

Geoffrey J. Teale 12 years ago
parent
commit
e20fec90df
3 changed files with 53 additions and 3 deletions
  1. 20 3
      lib.go
  2. 33 0
      lib_test.go
  3. BIN
      testfile.xlsx

+ 20 - 3
lib.go

@@ -41,9 +41,10 @@ func (c *Cell) String() string {
 
 
 // TODO: TestMe!
 // TODO: TestMe!
 func (c *Cell) GetStyle() *Style {
 func (c *Cell) GetStyle() *Style {
-	style := new(Style)
-	if c.styleIndex > 0 && c.styleIndex < len(c.styles.CellXfs) {
-		xf := c.styles.CellXfs[c.styleIndex]
+	style := &Style{}
+
+	if c.styleIndex > 0 && c.styleIndex <= len(c.styles.CellXfs) {
+		xf := c.styles.CellXfs[c.styleIndex - 1]
 		if xf.ApplyBorder {
 		if xf.ApplyBorder {
 			var border Border
 			var border Border
 			border.Left = c.styles.Borders[xf.BorderId].Left.Style
 			border.Left = c.styles.Borders[xf.BorderId].Left.Style
@@ -59,6 +60,14 @@ func (c *Cell) GetStyle() *Style {
 			fill.FgColor = c.styles.Fills[xf.FillId].PatternFill.FgColor.RGB
 			fill.FgColor = c.styles.Fills[xf.FillId].PatternFill.FgColor.RGB
 			style.Fills = fill
 			style.Fills = fill
 		}
 		}
+		if xf.ApplyFont {
+			font := c.styles.Fonts[xf.FontId]
+			style.Font = Font{}
+			style.Font.Size, _ = strconv.Atoi(font.Sz.Val)
+			style.Font.Name = font.Name.Val
+			style.Font.Family, _ = strconv.Atoi(font.Family.Val)
+			style.Font.Charset, _ = strconv.Atoi(font.Charset.Val)
+		}
 	}
 	}
 	return style
 	return style
 }
 }
@@ -82,6 +91,7 @@ type Sheet struct {
 type Style struct {
 type Style struct {
 	Boders Border
 	Boders Border
 	Fills  Fill
 	Fills  Fill
+	Font   Font
 }
 }
 
 
 // Border is a high level structure intended to provide user access to
 // Border is a high level structure intended to provide user access to
@@ -101,6 +111,13 @@ type Fill struct {
 	FgColor     string
 	FgColor     string
 }
 }
 
 
+type Font struct {
+	Size int
+	Name string
+	Family int
+	Charset int
+}
+
 // File is a high level structure providing a slice of Sheet structs
 // File is a high level structure providing a slice of Sheet structs
 // to the user.
 // to the user.
 type File struct {
 type File struct {

+ 33 - 0
lib_test.go

@@ -13,6 +13,7 @@ import (
 func TestOpenFile(t *testing.T) {
 func TestOpenFile(t *testing.T) {
 	var xlsxFile *File
 	var xlsxFile *File
 	var error error
 	var error error
+
 	xlsxFile, error = OpenFile("testfile.xlsx")
 	xlsxFile, error = OpenFile("testfile.xlsx")
 	if error != nil {
 	if error != nil {
 		t.Error(error.Error())
 		t.Error(error.Error())
@@ -65,6 +66,37 @@ func TestCreateSheet(t *testing.T) {
 	}
 	}
 }
 }
 
 
+// Test that GetStyle correctly converts the xlsxStyle.Fonts.
+func TestGetStyleWithFonts(t *testing.T) {
+	var cell *Cell
+	var style *Style
+	var xStyles *xlsxStyles
+	var fonts []xlsxFont
+	var cellXfs []xlsxXf
+
+	fonts = make([]xlsxFont, 1)
+	fonts[0] = xlsxFont{
+		Sz: xlsxVal{Val: "10"},
+		Name: xlsxVal{Val: "Calibra"}}
+
+	cellXfs = make([]xlsxXf, 1)
+	cellXfs[0] = xlsxXf{ApplyFont: true, FontId: 0}
+
+	xStyles = &xlsxStyles{Fonts: fonts, CellXfs: cellXfs}
+
+	cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
+	style = cell.GetStyle()
+	if style == nil {
+		t.Error("No style returned by cell.GetStyle()")
+	}
+	if style.Font.Size != 10 {
+		t.Error("Expected style.Font.Size == 10, but got ", style.Font.Size)
+	}
+	if style.Font.Name != "Calibra" {
+		t.Error("Expected style.Font.Name == 'Calibra', but got ", style.Font.Name)
+	}
+}
+
 // Test that we can correctly extract a reference table from the
 // Test that we can correctly extract a reference table from the
 // sharedStrings.xml file embedded in the XLSX file and return a
 // sharedStrings.xml file embedded in the XLSX file and return a
 // reference table of string values from it.
 // reference table of string values from it.
@@ -612,4 +644,5 @@ func TestReadRowsFromSheetWithTrailingEmptyCells(t *testing.T) {
 		t.Error("Expected cell4.String() == '', got ", cell4.String())
 		t.Error("Expected cell4.String() == '', got ", cell4.String())
 	}
 	}
 
 
+
 }
 }

BIN
testfile.xlsx