Browse Source

Merge branch 'mafredi-width-patch'

Geoffrey J. Teale 10 years ago
parent
commit
54ad5afc62
3 changed files with 49 additions and 13 deletions
  1. 15 7
      sheet.go
  2. 27 0
      sheet_test.go
  3. 7 6
      xmlWorksheet.go

+ 15 - 7
sheet.go

@@ -54,8 +54,7 @@ func (s *Sheet) maybeAddCol(cellCount int) {
 			Min:       cellCount,
 			Max:       cellCount,
 			Hidden:    false,
-			Collapsed: false,
-			Width:     ColWidth}
+			Collapsed: false}
 		s.Cols = append(s.Cols, col)
 		s.MaxCol = cellCount
 	}
@@ -110,6 +109,11 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 	maxRow := 0
 	maxCell := 0
 
+	if s.SheetFormat.DefaultRowHeight != 0 {
+		worksheet.SheetFormatPr.DefaultRowHeight = s.SheetFormat.DefaultRowHeight
+	}
+	worksheet.SheetFormatPr.DefaultColWidth = s.SheetFormat.DefaultColWidth
+
 	colsXfIdList := make([]int, len(s.Cols))
 	worksheet.Cols = xlsxCols{Col: []xlsxCol{}}
 	for c, col := range s.Cols {
@@ -123,16 +127,20 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 		}
 		colsXfIdList[c] = XfId
 
+		var customWidth int
 		if col.Width == 0 {
 			col.Width = ColWidth
+		} else {
+			customWidth = 1
 		}
 		worksheet.Cols.Col = append(worksheet.Cols.Col,
 			xlsxCol{Min: col.Min,
-				Max:       col.Max,
-				Hidden:    col.Hidden,
-				Width:     col.Width,
-				Collapsed: col.Collapsed,
-				Style:     XfId,
+				Max:         col.Max,
+				Hidden:      col.Hidden,
+				Width:       col.Width,
+				CustomWidth: customWidth,
+				Collapsed:   col.Collapsed,
+				Style:       XfId,
 			})
 	}
 

+ 27 - 0
sheet_test.go

@@ -158,6 +158,33 @@ func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
 	c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
 }
 
+// If the column width is not customised, the xslxCol.CustomWidth field is set to 0.
+func (s *SheetSuite) TestMakeXLSXSheetDefaultsCustomColWidth(c *C) {
+	file := NewFile()
+	sheet := file.AddSheet("Sheet1")
+	row := sheet.AddRow()
+	cell1 := row.AddCell()
+	cell1.Value = "A cell!"
+
+	refTable := NewSharedStringRefTable()
+	styles := newXlsxStyleSheet(nil)
+	worksheet := sheet.makeXLSXSheet(refTable, styles)
+	c.Assert(worksheet.Cols.Col[0].CustomWidth, Equals, 0)
+}
+
+// If the column width is customised, the xslxCol.CustomWidth field is set to 1.
+func (s *SheetSuite) TestMakeXLSXSheetSetsCustomColWidth(c *C) {
+	file := NewFile()
+	sheet := file.AddSheet("Sheet1")
+	err := sheet.SetColWidth(0, 0, 10.5)
+	c.Assert(err, IsNil)
+
+	refTable := NewSharedStringRefTable()
+	styles := newXlsxStyleSheet(nil)
+	worksheet := sheet.makeXLSXSheet(refTable, styles)
+	c.Assert(worksheet.Cols.Col[0].CustomWidth, Equals, 1)
+}
+
 func (s *SheetSuite) TestMarshalSheet(c *C) {
 	file := NewFile()
 	sheet := file.AddSheet("Sheet1")

+ 7 - 6
xmlWorksheet.go

@@ -192,12 +192,13 @@ type xlsxCols struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxCol struct {
-	Collapsed bool    `xml:"collapsed,attr"`
-	Hidden    bool    `xml:"hidden,attr"`
-	Max       int     `xml:"max,attr"`
-	Min       int     `xml:"min,attr"`
-	Style     int     `xml:"style,attr"`
-	Width     float64 `xml:"width,attr"`
+	Collapsed   bool    `xml:"collapsed,attr"`
+	Hidden      bool    `xml:"hidden,attr"`
+	Max         int     `xml:"max,attr"`
+	Min         int     `xml:"min,attr"`
+	Style       int     `xml:"style,attr"`
+	Width       float64 `xml:"width,attr"`
+	CustomWidth int     `xml:"customWidth,attr,omitempty"`
 }
 
 // xlsxDimension directly maps the dimension element in the namespace