Skip to content

Commit

Permalink
Merge pull request tealeg#89 from achernyak/master
Browse files Browse the repository at this point in the history
Added SetColWidth Function
  • Loading branch information
tealeg committed Feb 3, 2015
2 parents 7da0f38 + 0c30215 commit de9cc02
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
47 changes: 33 additions & 14 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ func (sh *Sheet) Cell(row, col int) *Cell {
return new(Cell)
}

//Set the width of a single column or multipel columns.
func (s *Sheet) SetColWidth(startcol, endcol int, width float64) error {
if startcol > endcol {
return fmt.Errorf("Could not set width for range %g-%g: startcol must be less than endcol.", startcol, endcol)
}
col := &Col{
Min: startcol + 1,
Max: endcol + 1,
Hidden: false,
Collapsed: false,
// Style: 0,
Width: width}
s.Cols = append(s.Cols, col)
if endcol+1 > s.MaxCol {
s.MaxCol = endcol + 1
}
return nil
}

// Dump sheet to it's XML representation, intended for internal use only
func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxWorksheet {
worksheet := newXlsxWorksheet()
Expand All @@ -75,20 +94,20 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
for c, cell := range row.Cells {
style := cell.GetStyle()
if style != nil {
xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
fontId := styles.addFont(xFont)
fillId := styles.addFill(xFill)
borderId := styles.addBorder(xBorder)
xCellStyleXf.FontId = fontId
xCellStyleXf.FillId = fillId
xCellStyleXf.BorderId = borderId
xCellStyleXf.NumFmtId = 0 // General
xCellXf.FontId = fontId
xCellXf.FillId = fillId
xCellXf.BorderId = borderId
xCellXf.NumFmtId = 0 // General
styles.addCellStyleXf(xCellStyleXf)
XfId = styles.addCellXf(xCellXf)
xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
fontId := styles.addFont(xFont)
fillId := styles.addFill(xFill)
borderId := styles.addBorder(xBorder)
xCellStyleXf.FontId = fontId
xCellStyleXf.FillId = fillId
xCellStyleXf.BorderId = borderId
xCellStyleXf.NumFmtId = 0 // General
xCellXf.FontId = fontId
xCellXf.FillId = fillId
xCellXf.BorderId = borderId
xCellXf.NumFmtId = 0 // General
styles.addCellStyleXf(xCellStyleXf)
XfId = styles.addCellXf(xCellXf)
}
if c > maxCell {
maxCell = c
Expand Down
14 changes: 14 additions & 0 deletions sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,17 @@ func (s *SheetSuite) TestMarshalSheetWithMultipleCells(c *C) {
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetPr filterMode="false"><pageSetUpPr fitToPage="false"></pageSetUpPr></sheetPr><dimension ref="A1:B1"></dimension><sheetViews><sheetView windowProtection="false" showFormulas="false" showGridLines="true" showRowColHeaders="true" showZeros="true" rightToLeft="false" tabSelected="true" showOutlineSymbols="true" defaultGridColor="true" view="normal" topLeftCell="A1" colorId="64" zoomScale="100" zoomScaleNormal="100" zoomScalePageLayoutView="100" workbookViewId="0"><selection pane="topLeft" activeCell="A1" activeCellId="0" sqref="A1"></selection></sheetView></sheetViews><sheetFormatPr defaultRowHeight="12.85"></sheetFormatPr><cols><col collapsed="false" hidden="false" max="1" min="1" width="9.5"></col><col collapsed="false" hidden="false" max="2" min="2" width="9.5"></col></cols><sheetData><row r="1"><c r="A1" s="0" t="s"><v>0</v></c><c r="B1" s="0" t="s"><v>1</v></c></row></sheetData><printOptions headings="false" gridLines="false" gridLinesSet="true" horizontalCentered="false" verticalCentered="false"></printOptions><pageMargins left="0.7875" right="0.7875" top="1.05277777777778" bottom="1.05277777777778" header="0.7875" footer="0.7875"></pageMargins><pageSetup paperSize="9" scale="100" firstPageNumber="1" fitToWidth="1" fitToHeight="1" pageOrder="downThenOver" orientation="portrait" usePrinterDefaults="false" blackAndWhite="false" draft="false" cellComments="none" useFirstPageNumber="true" horizontalDpi="300" verticalDpi="300" copies="1"></pageSetup><headerFooter differentFirst="false" differentOddEven="false"><oddHeader>&amp;C&amp;&#34;Times New Roman,Regular&#34;&amp;12&amp;A</oddHeader><oddFooter>&amp;C&amp;&#34;Times New Roman,Regular&#34;&amp;12Page &amp;P</oddFooter></headerFooter></worksheet>`
c.Assert(output.String(), Equals, expectedXLSXSheet)
}

func (s *SheetSuite) TestSetColWidth(c *C) {
file := NewFile()
sheet := file.AddSheet("Sheet1")
_ = sheet.SetColWidth(0, 0, 10.5)
_ = sheet.SetColWidth(1, 5, 11)

c.Assert(sheet.Cols[0].Width, Equals, 10.5)
c.Assert(sheet.Cols[0].Max, Equals, 1)
c.Assert(sheet.Cols[0].Min, Equals, 1)
c.Assert(sheet.Cols[1].Width, Equals, float64(11))
c.Assert(sheet.Cols[1].Max, Equals, 6)
c.Assert(sheet.Cols[1].Min, Equals, 2)
}

0 comments on commit de9cc02

Please sign in to comment.