Skip to content

Commit

Permalink
Merge pull request #1 from tealeg/master
Browse files Browse the repository at this point in the history
Adding setColWidth Function
  • Loading branch information
hackeryarn committed Feb 3, 2015
2 parents ffa2755 + 73beb03 commit 6c7150d
Show file tree
Hide file tree
Showing 25 changed files with 2,049 additions and 439 deletions.
4 changes: 2 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (

func main() {
excelFileName := "/home/tealeg/foo.xlsx"
xlFile, error := xlsx.OpenFile(excelFileName)
if error != nil {
xlFile, err := xlsx.OpenFile(excelFileName)
if err != nil {
...
}
for _, sheet := range xlFile.Sheets {
Expand Down
131 changes: 126 additions & 5 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@ import (
"strconv"
)

type CellType int

const (
CellTypeString CellType = iota
CellTypeFormula
CellTypeNumeric
CellTypeBool
CellTypeInline
CellTypeError
)

// Cell is a high level structure intended to provide user access to
// the contents of Cell within an xlsx.Row.
type Cell struct {
Row *Row
Value string
style Style
formula string
style *Style
numFmt string
date1904 bool
Hidden bool
cellType CellType
}

// CellInterface defines the public API of the Cell.
Expand All @@ -22,22 +36,129 @@ type CellInterface interface {
FormattedValue() string
}

func NewCell() *Cell {
return &Cell{style: *NewStyle()}
func NewCell(r *Row) *Cell {
return &Cell{style: NewStyle(), Row: r}
}

func (c *Cell) Type() CellType {
return c.cellType
}

// Set string
func (c *Cell) SetString(s string) {
c.Value = s
c.formula = ""
c.cellType = CellTypeString
}

// String returns the value of a Cell as a string.
func (c *Cell) String() string {
return c.FormattedValue()
}

// Set float
func (c *Cell) SetFloat(n float64) {
c.SetFloatWithFormat(n, "0.00e+00")
}

/*
Set float with format. The followings are samples of format samples.
* "0.00e+00"
* "0", "#,##0"
* "0.00", "#,##0.00", "@"
* "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)"
* "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)"
* "0%", "0.00%"
* "0.00e+00", "##0.0e+0"
*/
func (c *Cell) SetFloatWithFormat(n float64, format string) {
// tmp value. final value is formatted by FormattedValue() method
c.Value = fmt.Sprintf("%e", n)
c.numFmt = format
c.Value = c.FormattedValue()
c.formula = ""
c.cellType = CellTypeNumeric
}

// Returns the value of cell as a number
func (c *Cell) Float() (float64, error) {
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return math.NaN(), err
}
return f, nil
}

// Set a 64-bit integer
func (c *Cell) SetInt64(n int64) {
c.Value = fmt.Sprintf("%d", n)
c.numFmt = "0"
c.formula = ""
c.cellType = CellTypeNumeric
}

// Returns the value of cell as 64-bit integer
func (c *Cell) Int64() (int64, error) {
f, err := strconv.ParseInt(c.Value, 10, 64)
if err != nil {
return -1, err
}
return f, nil
}

// Set integer
func (c *Cell) SetInt(n int) {
c.Value = fmt.Sprintf("%d", n)
c.numFmt = "0"
c.formula = ""
c.cellType = CellTypeNumeric
}

// Returns the value of cell as integer
// Has max 53 bits of precision
// See: float64(int64(math.MaxInt))
func (c *Cell) Int() (int, error) {
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return -1, err
}
return int(f), nil
}

// Set boolean
func (c *Cell) SetBool(b bool) {
if b {
c.Value = "1"
} else {
c.Value = "0"
}
c.cellType = CellTypeBool
}

// Get boolean
func (c *Cell) Bool() bool {
return c.Value == "1"
}

// Set formula
func (c *Cell) SetFormula(formula string) {
c.formula = formula
c.cellType = CellTypeFormula
}

// Returns formula
func (c *Cell) Formula() string {
return c.formula
}

// GetStyle returns the Style associated with a Cell
func (c *Cell) GetStyle() Style {
func (c *Cell) GetStyle() *Style {
return c.style
}

// SetStyle sets the style of a cell.
func (c *Cell) SetStyle(style Style) {
func (c *Cell) SetStyle(style *Style) {
c.style = style
}

Expand Down
35 changes: 30 additions & 5 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *CellSuite) TestValueSet(c *C) {
// Test that GetStyle correctly converts the xlsxStyle.Fonts.
func (s *CellSuite) TestGetStyleWithFonts(c *C) {
font := NewFont(10, "Calibra")
style := *NewStyle()
style := NewStyle()
style.Font = *font

cell := &Cell{Value: "123", style: style}
Expand All @@ -39,7 +39,7 @@ func (s *CellSuite) TestSetStyleWithFonts(c *C) {
row := sheet.AddRow()
cell := row.AddCell()
font := NewFont(12, "Calibra")
style := *NewStyle()
style := NewStyle()
style.Font = *font
cell.SetStyle(style)
style = cell.GetStyle()
Expand All @@ -51,7 +51,7 @@ func (s *CellSuite) TestSetStyleWithFonts(c *C) {
// Test that GetStyle correctly converts the xlsxStyle.Fills.
func (s *CellSuite) TestGetStyleWithFills(c *C) {
fill := *NewFill("solid", "FF000000", "00FF0000")
style := *NewStyle()
style := NewStyle()
style.Fill = fill
cell := &Cell{Value: "123", style: style}
style = cell.GetStyle()
Expand All @@ -68,7 +68,7 @@ func (s *CellSuite) TestSetStyleWithFills(c *C) {
row := sheet.AddRow()
cell := row.AddCell()
fill := NewFill("solid", "00FF0000", "FF000000")
style := *NewStyle()
style := NewStyle()
style.Fill = *fill
cell.SetStyle(style)
style = cell.GetStyle()
Expand All @@ -82,7 +82,7 @@ func (s *CellSuite) TestSetStyleWithFills(c *C) {
// Test that GetStyle correctly converts the xlsxStyle.Borders.
func (s *CellSuite) TestGetStyleWithBorders(c *C) {
border := *NewBorder("thin", "thin", "thin", "thin")
style := *NewStyle()
style := NewStyle()
style.Border = border
cell := Cell{Value: "123", style: style}
style = cell.GetStyle()
Expand Down Expand Up @@ -255,3 +255,28 @@ func (l *CellSuite) TestFormattedValue(c *C) {
smallCell.numFmt = "yyyy-mm-dd hh:mm:ss"
c.Assert(smallCell.FormattedValue(), Equals, "1899-12-30 00:14:47")
}

// test setters and getters
func (s *CellSuite) TestSetterGetters(c *C) {
cell := Cell{}

cell.SetString("hello world")
c.Assert(cell.String(), Equals, "hello world")
c.Assert(cell.Type(), Equals, CellTypeString)

cell.SetInt(1024)
intValue, _ := cell.Int()
c.Assert(intValue, Equals, 1024)
c.Assert(cell.Type(), Equals, CellTypeNumeric)

cell.SetFloat(1.024)
float, _ := cell.Float()
intValue, _ = cell.Int() // convert
c.Assert(float, Equals, 1.024)
c.Assert(intValue, Equals, 1)
c.Assert(cell.Type(), Equals, CellTypeNumeric)

cell.SetFormula("10+20")
c.Assert(cell.Formula(), Equals, "10+20")
c.Assert(cell.Type(), Equals, CellTypeFormula)
}
13 changes: 9 additions & 4 deletions col.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package xlsx

// Default column width in excel
const ColWidth = 9.5

type Col struct {
Min int
Max int
Hidden bool
Width float64
Min int
Max int
Hidden bool
Width float64
Collapsed bool
// Style int
}
Loading

0 comments on commit 6c7150d

Please sign in to comment.