aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <lemon@matthewlemon.com>2019-11-03 17:04:06 +0000
committerMatthew Lemon <lemon@matthewlemon.com>2019-11-03 17:04:06 +0000
commitb86942c3eaf0ad7670b03b432f17c721c836adef (patch)
treee4f4c958c4e397b72482701754a9dce20b05041a
parentaa709070486848586937efe339ab6fd985b2c0bf (diff)
lots of stuff
-rw-r--r--reader/reader.go53
-rw-r--r--reader/reader_test.go23
-rw-r--r--reader/testdata/test_template.xlsxbin0 -> 7964 bytes
3 files changed, 70 insertions, 6 deletions
diff --git a/reader/reader.go b/reader/reader.go
index e9eabac..465f242 100644
--- a/reader/reader.go
+++ b/reader/reader.go
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
+ "log"
"strings"
"github.com/tealeg/xlsx"
@@ -61,18 +62,62 @@ func ReadDML(path string) ([]DatamapLine, error) {
return s, nil
}
+//ExtractedCell is Data pulled from a cell
+type ExtractedCell struct {
+ cell *xlsx.Cell
+ colLidx string
+ rowLidx int
+ value string
+}
+
+var lowerAlpha [2]string = [2]string{"A", "B"}
+
+// two alphabet lengths
+const ablen int = 52
+
+func alphas() []string {
+ acount := 0
+ lets := make([]string, ablen)
+ for i, _ := range lets {
+ if i == 0 {
+ lets[i] = "A"
+ continue
+ }
+ if i%26 != 0 {
+ lets[i] = string('A' + byte(i))
+ } else {
+ acount++
+ lets[i] = string(64+acount) + string('A'+byte(i))
+ }
+ }
+ return lets
+}
+
//ReadXLSX reads an XLSX file
-func ReadXLSX(excelFileName string) {
+func ReadXLSX(excelFileName string) []ExtractedCell {
+ var out []ExtractedCell
+ alphs := alphas()
+ for _, l := range alphs {
+ fmt.Printf("Letter: %s", string(l))
+ }
xlFile, err := xlsx.OpenFile(excelFileName)
if err != nil {
fmt.Printf("Cannot open %s", excelFileName)
}
for _, sheet := range xlFile.Sheets {
- for _, row := range sheet.Rows {
- for _, cell := range row.Cells {
+ for rowLidx, row := range sheet.Rows {
+ for colLidx, cell := range row.Cells {
+ ex := ExtractedCell{
+ cell: cell,
+ colLidx: string(alphs[colLidx]),
+ rowLidx: rowLidx + 1,
+ value: cell.Value}
+ out = append(out, ex)
text := cell.String()
- fmt.Printf("Sheet: %s\nValue: %s\n", sheet.Name, text)
+ log.Printf("Sheet: %s Row: %d Col: %q Value: %s\n",
+ sheet.Name, rowLidx, alphs[colLidx], text)
}
}
}
+ return out
}
diff --git a/reader/reader_test.go b/reader/reader_test.go
index 0d0dec2..563f2fc 100644
--- a/reader/reader_test.go
+++ b/reader/reader_test.go
@@ -23,6 +23,7 @@ func TestReadDML(t *testing.T) {
}
func TestNoFileReturnsError(t *testing.T) {
+ // this file does not exist
_, err := ReadDML("/home/bobbins.csv")
// if we get no error, something has gone wrong
if err == nil {
@@ -32,7 +33,25 @@ func TestNoFileReturnsError(t *testing.T) {
func TestBadDMLLine(t *testing.T) {
_, err := ReadDML("/home/lemon/code/python/bcompiler-engine/tests/resources/datamap_empty_cols.csv")
- if err != nil {
- t.Errorf("This will trigger")
+ if err == nil {
+ t.Errorf("No error so test failed.")
}
}
+
+func TestReadXLSX(t *testing.T) {
+ _ = ReadXLSX("testdata/test_template.xlsx") // TODO: remove temp blank
+}
+
+func TestAlphas(t *testing.T) {
+ as := alphas()
+ if as[0] != "A" {
+ t.Errorf("Expected A, got %v", as[0])
+ }
+ if as[25] != "Z" {
+ t.Errorf("Expected Z, got %v", as[25])
+ }
+ if as[26] != "AA" {
+ t.Errorf("Expected AA, got %v", as[26])
+ }
+
+}
diff --git a/reader/testdata/test_template.xlsx b/reader/testdata/test_template.xlsx
new file mode 100644
index 0000000..d1561e5
--- /dev/null
+++ b/reader/testdata/test_template.xlsx
Binary files differ