aboutsummaryrefslogtreecommitdiffstats
path: root/reader
diff options
context:
space:
mode:
authorMatthew Lemon <lemon@matthewlemon.com>2019-11-12 09:06:59 +0000
committerMatthew Lemon <lemon@matthewlemon.com>2019-11-12 09:06:59 +0000
commita1aae604cf128826ec7a5485c197c05528edc800 (patch)
treedffa4410a6202a520fb5afcc1b94fdcf316cd29e /reader
parent6e4a1c902f58e0a057fd23ab2276115f6d3a8f46 (diff)
first passing test after parsing XLSX data into decent data structure
Diffstat (limited to '')
-rw-r--r--reader/reader.go30
-rw-r--r--reader/reader_test.go4
2 files changed, 18 insertions, 16 deletions
diff --git a/reader/reader.go b/reader/reader.go
index be9b5bc..c24adc2 100644
--- a/reader/reader.go
+++ b/reader/reader.go
@@ -116,45 +116,45 @@ func cols(n int) []string {
return out
}
-// cellrefVal is a map of the cellref to the cell data
-// as provided by the xlsx package
-type cellrefVal map[string]ExtractedCell
+type (
+ SheetData map[string]ExtractedCell
+ FileData map[string]SheetData
+)
-func ReadXLSToMap(dm string, tm string) map[string][]cellrefVal {
- var out map[string][]cellrefVal
+func ReadXLSToMap(dm string, tm string) FileData {
// open the files
excelData, err := xlsx.OpenFile(tm)
if err != nil {
- fmt.Printf("Cannot open %v", excelData)
+ log.Fatalf("Cannot open %v", excelData)
}
dmlData, err := ReadDML(dm)
if err != nil {
- fmt.Errorf("Cannot read datamap file %v", dm)
+ log.Fatalf("Cannot read datamap file %v", dm)
}
+ sheetNames := getSheetNames(dmlData)
+
+ output := make(FileData, len(sheetNames))
+
// get the data
for _, sheet := range excelData.Sheets {
- sheetNames := getSheetNames(dmlData)
- sheetData := make(map[string][]cellrefVal, len(sheetNames))
- var cellDataList []map[string]ExtractedCell
+ data := make(SheetData)
for rowLidx, row := range sheet.Rows {
for colLidx, cell := range row.Cells {
- cellData := make(map[string]ExtractedCell)
ex := ExtractedCell{
cell: cell,
colL: colstream[colLidx],
rowLidx: rowLidx + 1,
value: cell.Value}
cellref := fmt.Sprintf("%s%d", ex.colL, ex.rowLidx)
- cellData[cellref] = ex
- cellDataList = append(cellDataList, cellData)
- sheetData[sheet.Name] = append(sheetData[sheet.Name], cellData)
+ data[cellref] = ex
}
+ output[sheet.Name] = data
}
}
- return out
+ return output
}
//ReadXLSX reads an XLSX file
diff --git a/reader/reader_test.go b/reader/reader_test.go
index 63d9a3e..6f768b5 100644
--- a/reader/reader_test.go
+++ b/reader/reader_test.go
@@ -103,5 +103,7 @@ func TestGetSheetsFromDM(t *testing.T) {
func TestReadXLSXToMap(t *testing.T) {
d := ReadXLSToMap("testdata/datamap.csv", "testdata/test_template.xlsx")
- _ = d
+ if d["Summary"]["A2"].value != "Date:" {
+ t.Errorf("Expected A2 in Summary sheet to be 'Date:' - instead it is %s", d["Summary"]["A2"].value)
+ }
}