package reader import ( "encoding/csv" "errors" "fmt" "io" "io/ioutil" "strings" "github.com/tealeg/xlsx" ) //DatamapLine - a line from the datamap. type DatamapLine struct { Key string Sheet string Cellref string } type fileError struct { file string msg string } func (e *fileError) Error() string { return fmt.Sprintf("%s", e.msg) } //Keylens returns the length of a key func Keylens(dml DatamapLine) (int, int) { return len(dml.Key), len(dml.Sheet) } //ReadDML returns a slice of DatamapLine structs func ReadDML(path string) ([]DatamapLine, error) { var s []DatamapLine data, err := ioutil.ReadFile(path) if err != nil { return s, errors.New("Cannot find file") } r := csv.NewReader(strings.NewReader(string(data))) for { record, err := r.Read() if err == io.EOF { break } if err != nil { return s, errors.New("Cannot read line %s") } if record[0] == "cell_key" { // this must be the header continue } dml := DatamapLine{ Key: strings.Trim(record[0], " "), Sheet: strings.Trim(record[1], " "), Cellref: strings.Trim(record[2], " ")} s = append(s, dml) } return s, nil } //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) } } } }