Просмотр исходного кода

Added cell H & V alignment support

Colin Fox 10 лет назад
Родитель
Сommit
8c24427672
4 измененных файлов с 105 добавлено и 8 удалено
  1. 6 0
      sheet.go
  2. 80 0
      sheet_test.go
  3. 13 7
      style.go
  4. 6 1
      xmlStyle.go

+ 6 - 0
sheet.go

@@ -141,6 +141,12 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 				if xCellXf.NumFmtId > 0 {
 					xCellXf.ApplyNumberFormat = true
 				}
+
+				xCellStyleXf.Alignment.Horizontal = style.Alignment.Horizontal
+				xCellStyleXf.Alignment.Vertical = style.Alignment.Vertical
+				xCellXf.Alignment.Horizontal = style.Alignment.Horizontal
+				xCellXf.Alignment.Vertical = style.Alignment.Vertical
+
 				styles.addCellStyleXf(xCellStyleXf)
 				XfId = styles.addCellXf(xCellXf)
 			}

+ 80 - 0
sheet_test.go

@@ -3,6 +3,7 @@ package xlsx
 import (
 	"bytes"
 	"encoding/xml"
+	"fmt"
 
 	. "gopkg.in/check.v1"
 )
@@ -213,3 +214,82 @@ func (s *SheetSuite) TestSetColWidth(c *C) {
 	c.Assert(sheet.Cols[1].Max, Equals, 6)
 	c.Assert(sheet.Cols[1].Min, Equals, 2)
 }
+
+func (s *SheetSuite) TestSetRowHeightCM(c *C) {
+	file := NewFile()
+	sheet := file.AddSheet("Sheet1")
+	row := sheet.AddRow()
+	row.SetHeightCM(1.5)
+	c.Assert(row.Height, Equals, 42.51968505)
+}
+
+func (s *SheetSuite) TestAlignment(c *C) {
+	leftalign := Alignment{Horizontal: "left"}
+	centerHalign := Alignment{Horizontal: "center"}
+	rightalign := Alignment{Horizontal: "right"}
+
+	file := NewFile()
+	sheet := file.AddSheet("Sheet1")
+
+	style := NewStyle()
+
+	hrow := sheet.AddRow()
+
+	// Horizontals
+	cell := hrow.AddCell()
+	cell.Value = "left"
+	style.Alignment = leftalign
+	style.ApplyAlignment = true
+	cell.SetStyle(style)
+
+	style = NewStyle()
+	cell = hrow.AddCell()
+	cell.Value = "centerH"
+	style.Alignment = centerHalign
+	style.ApplyAlignment = true
+	cell.SetStyle(style)
+
+	style = NewStyle()
+	cell = hrow.AddCell()
+	cell.Value = "right"
+	style.Alignment = rightalign
+	style.ApplyAlignment = true
+	cell.SetStyle(style)
+
+	// Verticals
+	topalign := Alignment{Vertical: "top"}
+	centerValign := Alignment{Vertical: "center"}
+	bottomalign := Alignment{Vertical: "bottom"}
+
+	style = NewStyle()
+	vrow := sheet.AddRow()
+	cell = vrow.AddCell()
+	cell.Value = "top"
+	style.Alignment = topalign
+	style.ApplyAlignment = true
+	cell.SetStyle(style)
+
+	style = NewStyle()
+	cell = vrow.AddCell()
+	cell.Value = "centerV"
+	style.Alignment = centerValign
+	style.ApplyAlignment = true
+	cell.SetStyle(style)
+
+	style = NewStyle()
+	cell = vrow.AddCell()
+	cell.Value = "bottom"
+	style.Alignment = bottomalign
+	style.ApplyAlignment = true
+	cell.SetStyle(style)
+
+	refTable := NewSharedStringRefTable()
+	styles := newXlsxStyleSheet(nil)
+	xSheet := sheet.makeXLSXSheet(refTable, styles)
+
+	//output := bytes.NewBufferString(xml.Header)
+	body, err := xml.Marshal(xSheet)
+	c.Assert(err, IsNil)
+	fmt.Println("body:", string(body))
+
+}

+ 13 - 7
style.go

@@ -5,13 +5,14 @@ import "strconv"
 // Style is a high level structure intended to provide user access to
 // the contents of Style within an XLSX file.
 type Style struct {
-	Border      Border
-	Fill        Fill
-	Font        Font
-	ApplyBorder bool
-	ApplyFill   bool
-	ApplyFont   bool
-	Alignment   Alignment
+	Border         Border
+	Fill           Fill
+	Font           Font
+	ApplyBorder    bool
+	ApplyFill      bool
+	ApplyFont      bool
+	ApplyAlignment bool
+	Alignment      Alignment
 }
 
 // Return a new Style structure initialised with the default values.
@@ -62,11 +63,15 @@ func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBo
 	xCellXf.ApplyBorder = style.ApplyBorder
 	xCellXf.ApplyFill = style.ApplyFill
 	xCellXf.ApplyFont = style.ApplyFont
+	xCellXf.ApplyAlignment = style.ApplyAlignment
 	xCellXf.NumFmtId = 0
 	xCellStyleXf.ApplyBorder = style.ApplyBorder
 	xCellStyleXf.ApplyFill = style.ApplyFill
 	xCellStyleXf.ApplyFont = style.ApplyFont
+	xCellStyleXf.ApplyAlignment = style.ApplyAlignment
 	xCellStyleXf.NumFmtId = 0
+
+	xCellStyleXf.Alignment = xlsxAlignment{Horizontal: style.Alignment.Horizontal, Vertical: style.Alignment.Vertical}
 	return
 }
 
@@ -112,6 +117,7 @@ func NewFont(size int, name string) *Font {
 
 type Alignment struct {
 	Horizontal string
+	Vertical   string
 }
 
 func DefaulFont() *Font {

+ 6 - 1
xmlStyle.go

@@ -123,6 +123,7 @@ func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style *Style) {
 		style.ApplyBorder = xf.ApplyBorder || styleXf.ApplyBorder
 		style.ApplyFill = xf.ApplyFill || styleXf.ApplyFill
 		style.ApplyFont = xf.ApplyFont || styleXf.ApplyFont
+		style.ApplyAlignment = xf.ApplyAlignment || styleXf.ApplyAlignment
 
 		if xf.BorderId > -1 && xf.BorderId < styles.Borders.Count {
 			var border xlsxBorder
@@ -154,13 +155,17 @@ func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style *Style) {
 			if italic := xfont.I; italic != nil && italic.Val != "0" {
 				style.Font.Italic = true
 			}
-			if underline := xfont.U; underline != nil && underline.Val != "0"  {
+			if underline := xfont.U; underline != nil && underline.Val != "0" {
 				style.Font.Underline = true
 			}
 		}
 		if xf.Alignment.Horizontal != "" {
 			style.Alignment.Horizontal = xf.Alignment.Horizontal
 		}
+
+		if xf.Alignment.Vertical != "" {
+			style.Alignment.Vertical = xf.Alignment.Vertical
+		}
 		styles.lock.Lock()
 		styles.styleCache[styleIndex] = style
 		styles.lock.Unlock()