Jelajahi Sumber

Merge branch 'nazo-fix-styleindex-0-origin'

Geoffrey J. Teale 11 tahun lalu
induk
melakukan
ec222ad780
2 mengubah file dengan 34 tambahan dan 8 penghapusan
  1. 5 5
      lib.go
  2. 29 3
      lib_test.go

+ 5 - 5
lib.go

@@ -47,9 +47,9 @@ func (c *Cell) String() string {
 func (c *Cell) GetStyle() *Style {
 func (c *Cell) GetStyle() *Style {
 	style := &Style{}
 	style := &Style{}
 
 
-	if c.styleIndex > 0 && c.styleIndex <= len(c.styles.CellXfs) {
-		xf := c.styles.CellXfs[c.styleIndex-1]
-		if xf.ApplyBorder {
+	if c.styleIndex >= 0 && c.styleIndex < len(c.styles.CellXfs) {
+		xf := c.styles.CellXfs[c.styleIndex]
+		if xf.BorderId >= 0 && xf.BorderId < len(c.styles.Borders) {
 			var border Border
 			var border Border
 			border.Left = c.styles.Borders[xf.BorderId].Left.Style
 			border.Left = c.styles.Borders[xf.BorderId].Left.Style
 			border.Right = c.styles.Borders[xf.BorderId].Right.Style
 			border.Right = c.styles.Borders[xf.BorderId].Right.Style
@@ -57,14 +57,14 @@ func (c *Cell) GetStyle() *Style {
 			border.Bottom = c.styles.Borders[xf.BorderId].Bottom.Style
 			border.Bottom = c.styles.Borders[xf.BorderId].Bottom.Style
 			style.Border = border
 			style.Border = border
 		}
 		}
-		if xf.ApplyFill {
+		if xf.FillId >= 0 && xf.FillId < len(c.styles.Fills) {
 			var fill Fill
 			var fill Fill
 			fill.PatternType = c.styles.Fills[xf.FillId].PatternFill.PatternType
 			fill.PatternType = c.styles.Fills[xf.FillId].PatternFill.PatternType
 			fill.BgColor = c.styles.Fills[xf.FillId].PatternFill.BgColor.RGB
 			fill.BgColor = c.styles.Fills[xf.FillId].PatternFill.BgColor.RGB
 			fill.FgColor = c.styles.Fills[xf.FillId].PatternFill.FgColor.RGB
 			fill.FgColor = c.styles.Fills[xf.FillId].PatternFill.FgColor.RGB
 			style.Fill = fill
 			style.Fill = fill
 		}
 		}
-		if xf.ApplyFont {
+		if xf.FontId >= 0 && xf.FontId < len(c.styles.Fonts) {
 			font := c.styles.Fonts[xf.FontId]
 			font := c.styles.Fonts[xf.FontId]
 			style.Font = Font{}
 			style.Font = Font{}
 			style.Font.Size, _ = strconv.Atoi(font.Sz.Val)
 			style.Font.Size, _ = strconv.Atoi(font.Sz.Val)

+ 29 - 3
lib_test.go

@@ -258,7 +258,7 @@ func (l *LibSuite) TestGetStyleWithFonts(c *C) {
 
 
 	xStyles = &xlsxStyles{Fonts: fonts, CellXfs: cellXfs}
 	xStyles = &xlsxStyles{Fonts: fonts, CellXfs: cellXfs}
 
 
-	cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
+	cell = &Cell{Value: "123", styleIndex: 0, styles: xStyles}
 	style = cell.GetStyle()
 	style = cell.GetStyle()
 	c.Assert(style, NotNil)
 	c.Assert(style, NotNil)
 	c.Assert(style.Font.Size, Equals, 10)
 	c.Assert(style.Font.Size, Equals, 10)
@@ -284,7 +284,7 @@ func (l *LibSuite) TestGetStyleWithFills(c *C) {
 
 
 	xStyles = &xlsxStyles{Fills: fills, CellXfs: cellXfs}
 	xStyles = &xlsxStyles{Fills: fills, CellXfs: cellXfs}
 
 
-	cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
+	cell = &Cell{Value: "123", styleIndex: 0, styles: xStyles}
 	style = cell.GetStyle()
 	style = cell.GetStyle()
 	fill := style.Fill
 	fill := style.Fill
 	c.Assert(fill.PatternType, Equals, "solid")
 	c.Assert(fill.PatternType, Equals, "solid")
@@ -312,7 +312,7 @@ func (l *LibSuite) TestGetStyleWithBorders(c *C) {
 
 
 	xStyles = &xlsxStyles{Borders: borders, CellXfs: cellXfs}
 	xStyles = &xlsxStyles{Borders: borders, CellXfs: cellXfs}
 
 
-	cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
+	cell = &Cell{Value: "123", styleIndex: 0, styles: xStyles}
 	style = cell.GetStyle()
 	style = cell.GetStyle()
 	border := style.Border
 	border := style.Border
 	c.Assert(border.Left, Equals, "thin")
 	c.Assert(border.Left, Equals, "thin")
@@ -444,6 +444,32 @@ func (l *LibSuite) TestReadWorkbookRelationsFromZipFileWithFunnyNames(c *C) {
 	c.Assert(cell1.String(), Equals, "I am Bob")
 	c.Assert(cell1.String(), Equals, "I am Bob")
 }
 }
 
 
+func (l *LibSuite) TestGetStyleFromZipFile(c *C) {
+	var xlsxFile *File
+	var err error
+
+	xlsxFile, err = OpenFile("testfile.xlsx")
+	c.Assert(err, IsNil)
+	sheetCount := len(xlsxFile.Sheet)
+	c.Assert(sheetCount, Equals, 3)
+
+	tabelle1 := xlsxFile.Sheet["Tabelle1"]
+
+	row0 := tabelle1.Rows[0]
+	cellFoo := row0.Cells[0]
+	c.Assert(cellFoo.String(), Equals, "Foo")
+	c.Assert(cellFoo.GetStyle().Fill.BgColor, Equals, "FF33CCCC")
+
+	row1 := tabelle1.Rows[1]
+	cellQuuk := row1.Cells[1]
+	c.Assert(cellQuuk.String(), Equals, "Quuk")
+	c.Assert(cellQuuk.GetStyle().Border.Left, Equals, "thin")
+
+	cellBar := row0.Cells[1]
+	c.Assert(cellBar.String(), Equals, "Bar")
+	c.Assert(cellBar.GetStyle().Fill.BgColor, Equals, "")
+}
+
 func (l *LibSuite) TestLettersToNumeric(c *C) {
 func (l *LibSuite) TestLettersToNumeric(c *C) {
 	cases := map[string]int{"A": 0, "G": 6, "z": 25, "AA": 26, "Az": 51,
 	cases := map[string]int{"A": 0, "G": 6, "z": 25, "AA": 26, "Az": 51,
 		"BA": 52, "Bz": 77, "ZA": 26*26 + 0, "ZZ": 26*26 + 25,
 		"BA": 52, "Bz": 77, "ZA": 26*26 + 0, "ZZ": 26*26 + 25,