From 111567b74e05a091d01cd19bbe2fd9f01eef64d9 Mon Sep 17 00:00:00 2001 From: Todd Date: Thu, 14 Sep 2017 17:16:19 -0500 Subject: [PATCH] spreadsheet: default new cells to numeric with implicit zero value --- spreadsheet/cell.go | 4 +++- spreadsheet/row.go | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spreadsheet/cell.go b/spreadsheet/cell.go index 0729731b..74c8bed7 100644 --- a/spreadsheet/cell.go +++ b/spreadsheet/cell.go @@ -53,6 +53,7 @@ func (c Cell) clearValue() { c.x.F = nil c.x.Is = nil c.x.V = nil + c.x.TAttr = sml.ST_CellTypeUnset } // SetInlineString adds a string inline instead of in the shared strings table. @@ -113,7 +114,8 @@ func (c Cell) GetValueAsNumber() (float64, error) { return math.NaN(), errors.New("cell is not of number type") } if c.x.V == nil { - return math.NaN(), errors.New("cell has no value") + // empty cells have an implicit zero value + return 0, nil } return strconv.ParseFloat(*c.x.V, 64) } diff --git a/spreadsheet/row.go b/spreadsheet/row.go index 2a8c681b..e8eedd2f 100644 --- a/spreadsheet/row.go +++ b/spreadsheet/row.go @@ -62,6 +62,8 @@ func (r Row) SetHidden(hidden bool) { // AddCell adds a cell to a spreadsheet. func (r Row) AddCell() Cell { c := spreadsheetml.NewCT_Cell() + c.TAttr = spreadsheetml.ST_CellTypeN + r.x.C = append(r.x.C, c) nextIdx := uint32(0) for _, c := range r.x.C { @@ -92,6 +94,8 @@ func (r Row) Cells() []Cell { // invaild spreadsheet. func (r Row) AddNamedCell(col string) Cell { c := spreadsheetml.NewCT_Cell() + c.TAttr = spreadsheetml.ST_CellTypeN + r.x.C = append(r.x.C, c) c.RAttr = gooxml.Stringf("%s%d", col, r.RowNumber()) return Cell{r.w, r.s, r.x, c}