Prechádzať zdrojové kódy

Set font, fill and border ids on CellXf.

Geoffrey J. Teale 11 rokov pred
rodič
commit
bcaad4995a
2 zmenil súbory, kde vykonal 44 pridanie a 11 odobranie
  1. 3 0
      sheet.go
  2. 41 11
      sheet_test.go

+ 3 - 0
sheet.go

@@ -62,6 +62,9 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyles) *xlsxWorks
 			xCellStyleXf.FontId = fontId
 			xCellStyleXf.FillId = fillId
 			xCellStyleXf.BorderId = borderId
+			xCellXf.FontId = fontId
+			xCellXf.FillId = fillId
+			xCellXf.BorderId = borderId
 			styles.addCellStyleXf(xCellStyleXf)
 			styles.addCellXf(xCellXf)
 			if c > maxCell {

+ 41 - 11
sheet_test.go

@@ -55,32 +55,62 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	file := NewFile()
 	sheet := file.AddSheet("Sheet1")
 	row := sheet.AddRow()
-	cell := row.AddCell()
-	cell.Value = "A cell!"
-	style := *NewStyle()
-	style.Font = *NewFont(10, "Verdana")
-	style.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
-	style.Border = *NewBorder("none", "thin", "none", "thin")
-	cell.SetStyle(style)
+
+	cell1 := row.AddCell()
+	cell1.Value = "A cell!"
+	style1 := *NewStyle()
+	style1.Font = *NewFont(10, "Verdana")
+	style1.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
+	style1.Border = *NewBorder("none", "thin", "none", "thin")
+	cell1.SetStyle(style1)
+
+	// We need a second style to check that Xfs are populated correctly.
+	cell2 := row.AddCell()
+	cell2.Value = "Another cell!"
+	style2 := *NewStyle()
+	style2.Font = *NewFont(10, "Verdana")
+	style2.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
+	style2.Border = *NewBorder("none", "thin", "none", "thin")
+	cell2.SetStyle(style2)
+
 	refTable := NewSharedStringRefTable()
 	styles := &xlsxStyles{}
 	_ = sheet.makeXLSXSheet(refTable, styles)
-	c.Assert(len(styles.Fonts), Equals, 1)
+
+	c.Assert(len(styles.Fonts), Equals, 2)
 	c.Assert(styles.Fonts[0].Sz.Val, Equals, "10")
 	c.Assert(styles.Fonts[0].Name.Val, Equals, "Verdana")
-	c.Assert(len(styles.Fills), Equals, 1)
+
+	c.Assert(len(styles.Fills), Equals, 2)
 	c.Assert(styles.Fills[0].PatternFill.PatternType, Equals, "solid")
 	c.Assert(styles.Fills[0].PatternFill.FgColor.RGB, Equals, "FFFFFFFF")
 	c.Assert(styles.Fills[0].PatternFill.BgColor.RGB, Equals, "00000000")
-	c.Assert(len(styles.Borders), Equals, 1)
+
+	c.Assert(len(styles.Borders), Equals, 2)
 	c.Assert(styles.Borders[0].Left.Style, Equals, "none")
 	c.Assert(styles.Borders[0].Right.Style, Equals, "thin")
 	c.Assert(styles.Borders[0].Top.Style, Equals, "none")
 	c.Assert(styles.Borders[0].Bottom.Style, Equals, "thin")
-	c.Assert(len(styles.CellStyleXfs), Equals, 1)
+
+	c.Assert(len(styles.CellStyleXfs), Equals, 2)
+	// The 0th CellStyleXf could just be getting the zero values by default
 	c.Assert(styles.CellStyleXfs[0].FontId, Equals, 0)
 	c.Assert(styles.CellStyleXfs[0].FillId, Equals, 0)
 	c.Assert(styles.CellStyleXfs[0].BorderId, Equals, 0)
+	// The 1st element cannot get initialised this way by accident.
+	c.Assert(styles.CellStyleXfs[1].FontId, Equals, 1)
+	c.Assert(styles.CellStyleXfs[1].FillId, Equals, 1)
+	c.Assert(styles.CellStyleXfs[1].BorderId, Equals, 1)
+
+	c.Assert(len(styles.CellXfs), Equals, 2)
+	c.Assert(styles.CellXfs[0].FontId, Equals, 0)
+	c.Assert(styles.CellXfs[0].FillId, Equals, 0)
+	c.Assert(styles.CellXfs[0].BorderId, Equals, 0)
+	// As above, we need the 1st element to make the test fail
+	// when it should.
+	c.Assert(styles.CellXfs[1].FontId, Equals, 1)
+	c.Assert(styles.CellXfs[1].FillId, Equals, 1)
+	c.Assert(styles.CellXfs[1].BorderId, Equals, 1)
 }
 
 func (s *SheetSuite) TestMarshalSheet(c *C) {