From 87507f9937bde61cb5fd5ae0bedef2cdfe4d176e Mon Sep 17 00:00:00 2001 From: Harmen Date: Wed, 20 Jan 2021 11:46:56 +0100 Subject: [PATCH] add cell type (#26) Co-authored-by: Douglas Parsons --- rows.go | 31 +++++++++++++++++++ rows_test.go | 84 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 11 deletions(-) diff --git a/rows.go b/rows.go index 1b0a51b..a4c3368 100644 --- a/rows.go +++ b/rows.go @@ -34,8 +34,18 @@ type Cell struct { Column string // E.G A, B, C Row int Value string + Type CellType } +type CellType string + +const ( + TypeString CellType = "string" + TypeNumerical CellType = "numerical" + TypeDateTime CellType = "datetime" + TypeBoolean CellType = "boolean" +) + func (c Cell) ColumnIndex() int { return asIndex(c.Column) } @@ -80,6 +90,26 @@ func (x *XlsxFile) getCellValue(r rawCell) (string, error) { return *r.Value, nil } +func (x *XlsxFile) getCellType(r rawCell) CellType { + if x.dateStyles[r.Style] { + return TypeDateTime + } + + switch r.Type { + case "b": + return TypeBoolean + case "d": + return TypeDateTime + case "n", "": + return TypeNumerical + case "s", + "inlineStr": + return TypeString + default: + return TypeString + } +} + // readSheetRows iterates over "row" elements within a worksheet, // pushing a parsed Row struct into a channel for each one. func (x *XlsxFile) readSheetRows(sheet string, ch chan<- Row) { @@ -169,6 +199,7 @@ func (x *XlsxFile) parseRawCells(rawCells []rawCell, index int) ([]Cell, error) Column: column, Row: index, Value: val, + Type: x.getCellType(rawCell), }) } diff --git a/rows_test.go b/rows_test.go index 15f3d5d..cbf9223 100644 --- a/rows_test.go +++ b/rows_test.go @@ -19,6 +19,7 @@ var invalidValue = "wat" var sharedString = "2" var offsetTooHighSharedString = "32" var dateString = "2005-06-04" +var boolString = "1" var cellValueTests = []struct { Name string @@ -81,6 +82,11 @@ var cellValueTests = []struct { Cell: rawCell{Type: "d", Style: 1, Value: &dateString}, Expected: dateString, }, + { + Name: "Boolean type", + Cell: rawCell{Type: "b", Value: &boolString}, + Expected: boolString, + }, { Name: "No Inline String or Value", Cell: rawCell{Type: "s", Reference: "C23"}, @@ -103,6 +109,62 @@ func TestGettingValueFromRawCell(t *testing.T) { } } +var cellTypeTests = []struct { + Name string + Cell rawCell + Expected CellType +}{ + { + Name: "Valid Inline String", + Cell: rawCell{Type: "inlineStr", InlineString: &inlineStr}, + Expected: TypeString, + }, + { + Name: "Valid Date", + Cell: rawCell{Type: "n", Value: &dateValue, Style: 1}, + Expected: TypeDateTime, + }, + { + Name: "Valid Date Without Type", + Cell: rawCell{Value: &dateValue, Style: 1}, + Expected: TypeDateTime, + }, + { + Name: "Valid Shared String", + Cell: rawCell{Type: "s", Value: &sharedString}, + Expected: TypeString, + }, + { + Name: "Unknown type", + Cell: rawCell{Type: "potato", Value: &inlineStr}, + Expected: TypeString, + }, + { + Name: "Date type", + Cell: rawCell{Type: "d", Style: 1, Value: &dateString}, + Expected: TypeDateTime, + }, + { + Name: "Boolean type", + Cell: rawCell{Type: "b", Value: &boolString}, + Expected: TypeBoolean, + }, + { + Name: "No type", + Cell: rawCell{Type: "", Value: &sharedString}, + Expected: TypeNumerical, + }, +} + +func TestGettingTypeFromRawCell(t *testing.T) { + for _, test := range cellTypeTests { + t.Run(test.Name, func(t *testing.T) { + typ := testFile.getCellType(test.Cell) + require.Equal(t, test.Expected, typ) + }) + } +} + var readSheetRowsTests = []struct { SheetName string Error string @@ -188,8 +250,8 @@ var parseRawCellsTests = []struct { {Reference: "E123", Type: "inlineStr", InlineString: &inlineStr}, }, Expected: []Cell{ - {Column: "D", Row: 123, Value: "The meaning of life"}, - {Column: "E", Row: 123, Value: "The meaning of life"}, + {Column: "D", Row: 123, Value: "The meaning of life", Type: TypeString}, + {Column: "E", Row: 123, Value: "The meaning of life", Type: TypeString}, }, }, } @@ -221,19 +283,19 @@ func TestReadingFileContents(t *testing.T) { require.Equal(t, []Row{ {Index: 1, Cells: []Cell{ - {Column: "A", Row: 1, Value: "rec_id"}, - {Column: "B", Row: 1, Value: "culture"}, - {Column: "C", Row: 1, Value: "sex"}, + {Column: "A", Row: 1, Value: "rec_id", Type: TypeString}, + {Column: "B", Row: 1, Value: "culture", Type: TypeString}, + {Column: "C", Row: 1, Value: "sex", Type: TypeString}, }}, {Index: 2, Cells: []Cell{ - {Column: "A", Row: 2, Value: "rec-67374-org"}, - {Column: "B", Row: 2, Value: "usa"}, - {Column: "C", Row: 2, Value: "f"}, + {Column: "A", Row: 2, Value: "rec-67374-org", Type: TypeString}, + {Column: "B", Row: 2, Value: "usa", Type: TypeString}, + {Column: "C", Row: 2, Value: "f", Type: TypeString}, }}, {Index: 3, Cells: []Cell{ - {Column: "A", Row: 3, Value: "rec-171273-org"}, - {Column: "B", Row: 3, Value: "ara"}, - {Column: "C", Row: 3, Value: "m"}, + {Column: "A", Row: 3, Value: "rec-171273-org", Type: TypeString}, + {Column: "B", Row: 3, Value: "ara", Type: TypeString}, + {Column: "C", Row: 3, Value: "m", Type: TypeString}, }}, }, rows) }