aboutsummaryrefslogtreecommitdiffstats
path: root/reader/reader.go
blob: 46c7144bf3fb32a19839d4738d1d2991d69a1ca0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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)
			}
		}
	}
}