aboutsummaryrefslogtreecommitdiffstats
path: root/reader/reader.go
diff options
context:
space:
mode:
Diffstat (limited to 'reader/reader.go')
-rw-r--r--reader/reader.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/reader/reader.go b/reader/reader.go
new file mode 100644
index 0000000..46c7144
--- /dev/null
+++ b/reader/reader.go
@@ -0,0 +1,69 @@
+package reader
+
+import (
+ "encoding/csv"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "strings"
+
+ "github.com/tealeg/xlsx"
+)
+
+//DatamapLine - a line from the datamap.
+type DatamapLine struct {
+ Key string
+ Sheet string
+ Cellref string
+}
+
+//Keylens returns the length of a key
+func Keylens(dml DatamapLine) (int, int) {
+ return len(dml.Key), len(dml.Sheet)
+}
+
+//ReadDML returns a pointer to a slice of DatamapLine structs
+func ReadDML(path string) *[]DatamapLine {
+ var s []DatamapLine
+ data, err := ioutil.ReadFile(path)
+ if err != nil {
+ fmt.Printf("Error! Cannot open file %s", err)
+ }
+ r := csv.NewReader(strings.NewReader(string(data)))
+ for {
+ record, err := r.Read()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ fmt.Printf("Cannot read line %s, ", err)
+ }
+ if record[0] == "cell_key" {
+ // this must be the header
+ continue
+ }
+ dml := DatamapLine{Key: record[0], Sheet: record[1], Cellref: record[2]}
+ s = append(s, dml)
+ // fmt.Printf("Key: %s; sheet: %s cellref: %s\n", dml.Key, dml.Sheet, dml.Cellref)
+ // klen, slen := Keylens(dml)
+ // fmt.Printf("Key length: %d\n", klen)
+ // fmt.Printf("Sheet length: %d\n\n", slen)
+ }
+ return &s
+}
+
+//ReadXLSX reads an XLSX file
+func ReadXLSX(excelFileName string) {
+ 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 {
+ text := cell.String()
+ fmt.Printf("Sheet: %s\nValue: %s\n", sheet.Name, text)
+ }
+ }
+ }
+}