From 35d03d67eb00f56fc9df604085dbe743d606e498 Mon Sep 17 00:00:00 2001 From: Jeremy Jay Date: Mon, 22 Feb 2021 00:01:17 -0500 Subject: [PATCH] expose data types through interface --- cmd/grater/main.go | 2 ++ commonxl/cell.go | 29 +++++++++++++++++++++++++++-- commonxl/sheet.go | 13 ++++++++++++- grate.go | 5 +++++ simple/simple.go | 15 +++++++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/cmd/grater/main.go b/cmd/grater/main.go index 6042cbc..3f3e814 100644 --- a/cmd/grater/main.go +++ b/cmd/grater/main.go @@ -40,7 +40,9 @@ func main() { } for sheet.Next() { + //dtypes := sheet.Types() row := sheet.Strings() + //fmt.Println(strings.Join(dtypes, "\t")) fmt.Println(strings.Join(row, "\t")) } } diff --git a/commonxl/cell.go b/commonxl/cell.go index 3dc54f0..7e81d71 100644 --- a/commonxl/cell.go +++ b/commonxl/cell.go @@ -25,6 +25,26 @@ const ( StaticCell // placeholder, internal use only ) +// String returns a string description of the cell data type. +func (c CellType) String() string { + switch c { + case BlankCell: + return "blank" + case IntegerCell: + return "integer" + case FloatCell: + return "float" + case BooleanCell: + return "boolean" + case DateCell: + return "date" + case HyperlinkStringCell: + return "hyperlink" + default: // StringCell, StaticCell + return "string" + } +} + // Cell represents a single cell value. type Cell []interface{} @@ -85,7 +105,7 @@ var boolStrings = map[string]bool{ } // NewCellWithType creates a new cell value with the given type, coercing as necessary. -func NewCellWithType(value interface{}, t CellType) Cell { +func NewCellWithType(value interface{}, t CellType, f *Formatter) Cell { c := NewCell(value) if c[1] == t { // fast path if it was already typed correctly @@ -154,7 +174,12 @@ func NewCellWithType(value interface{}, t CellType) Cell { c[1] = StringCell } if t == DateCell { - /// DO THE MAGIC CONVERSION HERE + if c[1] == FloatCell { + c[0] = f.ConvertToDate(c[0].(float64)) + } else if c[1] == IntegerCell { + c[0] = f.ConvertToDate(float64(c[0].(int64))) + } + c[1] = DateCell } return c } diff --git a/commonxl/sheet.go b/commonxl/sheet.go index f8e728e..5b4c81d 100644 --- a/commonxl/sheet.go +++ b/commonxl/sheet.go @@ -55,7 +55,7 @@ func (s *Sheet) Put(row, col int, value interface{}, fmtNum uint16) { if !ok || fmtNum == 0 { s.Rows[row][col] = NewCell(value) } else { - s.Rows[row][col] = NewCellWithType(value, ct) + s.Rows[row][col] = NewCellWithType(value, ct, s.Formatter) } s.Rows[row][col].SetFormatNumber(fmtNum) } @@ -110,6 +110,17 @@ func (s *Sheet) Strings() []string { return res } +// Types extracts the data types from the current record into a list. +// options: "boolean", "integer", "float", "string", "date", +// and special cases: "blank", "hyperlink" which are string types +func (s *Sheet) Types() []string { + res := make([]string, s.NumCols) + for i, cell := range s.Rows[s.CurRow] { + res[i] = cell.Type().String() + } + return res +} + // Scan extracts values from the current record into the provided arguments // Arguments must be pointers to one of 5 supported types: // bool, int64, float64, string, or time.Time diff --git a/grate.go b/grate.go index 6ad421a..06d0cc9 100644 --- a/grate.go +++ b/grate.go @@ -29,6 +29,11 @@ type Collection interface { // Strings extracts values from the current record into a list of strings. Strings() []string + // Types extracts the data types from the current record into a list. + // options: "boolean", "integer", "float", "string", "date", + // and special cases: "blank", "hyperlink" which are string types + Types() []string + // Scan extracts values from the current record into the provided arguments // Arguments must be pointers to one of 5 supported types: // bool, int64, float64, string, or time.Time diff --git a/simple/simple.go b/simple/simple.go index ec45898..0b2149c 100644 --- a/simple/simple.go +++ b/simple/simple.go @@ -44,6 +44,21 @@ func (t *simpleFile) Strings() []string { return t.rows[t.iterRow] } +// Types extracts the data types from the current record into a list. +// options: "boolean", "integer", "float", "string", "date", +// and special cases: "blank", "hyperlink" which are string types +func (t *simpleFile) Types() []string { + res := make([]string, len(t.rows[t.iterRow])) + for i, v := range t.rows[t.iterRow] { + if v == "" { + res[i] = "blank" + } else { + res[i] = "string" + } + } + return res +} + // Scan extracts values from the current record into the provided arguments // Arguments must be pointers to one of 5 supported types: // bool, int, float64, string, or time.Time