diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-31 15:30:16 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-07-31 15:30:16 +0100 |
commit | a39e4a5fde7ea3947b9c93149fda6c2833837e32 (patch) | |
tree | 956d27030bcd1f4fe7a27529690d2781d584ac40 | |
parent | a418aae9df25c7efc1536fd9dde01ece795d2156 (diff) |
upgrade to tealeg.xlsx v3.2.0 - goodbye to coords!
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | pkg/datamaps/reader.go | 63 |
2 files changed, 40 insertions, 25 deletions
@@ -4,6 +4,6 @@ go 1.14 require ( github.com/mattn/go-sqlite3 v1.14.0 - github.com/tealeg/xlsx v1.0.5 + github.com/tealeg/xlsx/v3 v3.2.0 github.com/yulqen/coords v0.1.0 ) diff --git a/pkg/datamaps/reader.go b/pkg/datamaps/reader.go index 7758822..f1d0681 100644 --- a/pkg/datamaps/reader.go +++ b/pkg/datamaps/reader.go @@ -17,8 +17,7 @@ import ( // Required for the sqlite3 driver _ "github.com/mattn/go-sqlite3" - "github.com/tealeg/xlsx" - "github.com/yulqen/coords" + "github.com/tealeg/xlsx/v3" ) type ( @@ -47,6 +46,11 @@ type extractedCell struct { Value string } +var ( + inner = make(sheetData) + exc extractedCell +) + // ExtractedDatamapFile is a slice of datamapLine structs, each of which encodes a single line // in the datamap file/database table. type ExtractedDatamapFile []datamapLine @@ -115,12 +119,37 @@ func ReadDML(path string) (ExtractedDatamapFile, error) { return s, nil } +// cellVisitor is used by datamaps.rowVisitor() and is called +// on every cell in the target xlsx file in order to extract +// the data. +func cellVisitor(c *xlsx.Cell) error { + x, y := c.GetCoordinates() + cellref := xlsx.GetCellIDStringFromCoords(x, y) + + ex := extractedCell{ + Cell: c, + Value: c.Value, + } + + inner[cellref] = ex + + return nil +} + +// rowVisitor is used as a callback by xlsx.sheet.ForEachRow(). It wraps +// a call to xlsx.Row.ForEachCell() which actually extracts the data. +func rowVisitor(r *xlsx.Row) error { + if err := r.ForEachCell(cellVisitor, xlsx.SkipEmptyCells); err != nil { + return err + } + return nil +} + // ReadXLSX returns a file at path's data as a map, // keyed on sheet name. All values are returned as strings. // Paths to a datamap and the spreadsheet file required. func ReadXLSX(path string) FileData { - // open the files - data, err := xlsx.OpenFile(path) + wb, err := xlsx.OpenFile(path) if err != nil { log.Fatal(err) } @@ -128,27 +157,13 @@ func ReadXLSX(path string) FileData { outer := make(FileData, 1) // get the data - for _, sheet := range data.Sheets { - inner := make(sheetData) - - for rowLidx, row := range sheet.Rows { - for colLidx, cell := range row.Cells { - colStr, err := coords.ColIndexToAlpha(colLidx) - if err != nil { - log.Fatal(err) - } - - ex := extractedCell{ - Cell: cell, - Col: colStr, - Row: rowLidx + 1, - Value: cell.Value} - cellref := fmt.Sprintf("%s%d", ex.Col, ex.Row) - inner[cellref] = ex - } - - outer[sheet.Name] = inner + for _, sheet := range wb.Sheets { + + if err := sheet.ForEachRow(rowVisitor); err != nil { + log.Fatal(err) } + outer[sheet.Name] = inner + inner = make(sheetData) } return outer |