Browse Source

add color style

add color style
zhcy 13 years ago
parent
commit
412405ef96
2 changed files with 32 additions and 10 deletions
  1. 21 10
      lib.go
  2. 11 0
      style.go

+ 21 - 10
lib.go

@@ -41,24 +41,24 @@ func (c *Cell) String() string {
 }
 
 func (c *Cell) GetStyle() *Style {
+	style := new(Style)
 	if c.styleIndex > 0 && c.styleIndex < len(c.styles.CellXfs) {
 		xf := c.styles.CellXfs[c.styleIndex]
 		if xf.ApplyBorder != "0" {
 			var border Border
-			style := new(Style)
 			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.Boders = border
-			return style
-		} else {
-			return new(Style)
 		}
-	} else {
-		return new(Style)
+		if xf.ApplyFill != "0" {
+			var fill Fill
+			fill.BgColorIndex = c.styles.Fills[xf.FillId].BgColorIndex
+			style.Fills = fill
+		}
 	}
-	return new(Style)
+	return style
 }
 
 // Row is a high level structure indended to provide user access to a
@@ -79,6 +79,7 @@ type Sheet struct {
 // the contents of Style within an XLSX file.
 type Style struct {
 	Boders Border
+	Fills  Fill
 }
 
 // Border is a high level structure intended to provide user access to
@@ -90,6 +91,13 @@ type Border struct {
 	Bottom string
 }
 
+// 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
+}
+
 // File is a high level structure providing a slice of Sheet structs
 // to the user.
 type File struct {
@@ -327,14 +335,17 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 		} else {
 			row = makeRowFromRaw(rawrow)
 		}
-		_, y, _ := getCoordsFromCellIDString(rawrow.C[0].R)
+		rowno := 0
 		for _, rawcell := range rawrow.C {
-			x, _, _ := getCoordsFromCellIDString(rawcell.R)
+			x, y, _ := getCoordsFromCellIDString(rawcell.R)
+			if y != 0 && rowno == 0{
+				rowno = y
+			}
 			row.Cells[x].Value = getValueFromCellData(rawcell, reftable)
 			row.Cells[x].styleIndex = rawcell.S
 			row.Cells[x].styles = file.styles
 		}
-		rows[y] = row
+		rows[rowno] = row
 	}
 	return rows, maxCol, maxRow
 }

+ 11 - 0
style.go

@@ -42,7 +42,16 @@ type xlsxVal struct {
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
 // currently I have not checked it for completeness - it does as much
 // as I need.
+// ColorIndex ARGBValue
+// 0          00000000
+// 1          00FFFFFF
+// 2          00FF0000
+// 3          0000FF00
+// ...............
+// ...............
 type xlsxFill struct {
+	FgColorIndex string  `xml:"patternFill>fgColor>indexed,attr"`
+	BgColorIndex string  `xml:"patternFill>bgColor>indexed,attr"`
 }
 
 // xlsxBorder directly maps the border element in the namespace
@@ -71,4 +80,6 @@ type xlsxLine struct {
 type xlsxXf struct {
 	ApplyBorder string `xml:"applyBorder,attr"`
 	BorderId    int    `xml:"borderId,attr"`
+	ApplyFill string `xml:"applyFill,attr"`
+	FillId int `xml:"fillId,attr"`
 }