Skip to content

Commit

Permalink
add cell type (#26)
Browse files Browse the repository at this point in the history
Co-authored-by: Douglas Parsons <dglsparsons@users.noreply.github.com>
  • Loading branch information
alicebob and dglsparsons committed Jan 20, 2021
1 parent ded8371 commit 87507f9
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 11 deletions.
31 changes: 31 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -169,6 +199,7 @@ func (x *XlsxFile) parseRawCells(rawCells []rawCell, index int) ([]Cell, error)
Column: column,
Row: index,
Value: val,
Type: x.getCellType(rawCell),
})
}

Expand Down
84 changes: 73 additions & 11 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand All @@ -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
Expand Down Expand Up @@ -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},
},
},
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 87507f9

Please sign in to comment.