/
reader.go
150 lines (115 loc) · 3.13 KB
/
reader.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package services
import (
"mime/multipart"
"strings"
"github.com/Los-Crackitos/Excelante/models"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
// Output is a generic type used by Excel reading functions
type Output map[string]map[int]interface{}
// ReadLines read all lines of a given Excel file
// Returns all values of the file using the Output type or an error
func ReadLines(file multipart.File, readerOptions models.ReaderOption) (Output, error) {
output := make(Output)
f, err := excelize.OpenReader(file)
if err != nil {
return nil, err
}
initialColIndex := 1
initialRowIndex := 1
sheetFound := false
for _, sheetName := range f.GetSheetMap() {
if len(readerOptions.Options) > 0 {
sheetFound, initialColIndex, initialRowIndex = sheetFinder(sheetName, readerOptions)
if !sheetFound {
continue
}
}
output[sheetName] = make(map[int]interface{})
rows, err := f.Rows(sheetName)
if err != nil {
return nil, err
}
currentRowIndex := 1
for rows.Next() {
row, err := rows.Columns()
if err != nil {
return nil, err
}
if currentRowIndex < initialRowIndex {
currentRowIndex++
continue
}
rowValue := make([]interface{}, 0)
for i := (initialColIndex - 1); i < len(row); i++ {
cellValue := row[i]
if cellValue == "" {
cellValue = "N/A"
}
rowValue = append(rowValue, strings.TrimLeft(cellValue, " "))
}
output[sheetName][currentRowIndex] = rowValue
currentRowIndex++
}
}
return output, nil
}
// ReadColumns read all columns of a given Excel file
// Returns all values of the file using the Output type or an error
func ReadColumns(file multipart.File, readerOptions models.ReaderOption) (Output, error) {
output := make(Output)
f, err := excelize.OpenReader(file)
if err != nil {
return nil, err
}
initialColIndex := 1
initialRowIndex := 1
sheetFound := false
for _, sheetName := range f.GetSheetMap() {
if len(readerOptions.Options) > 0 {
sheetFound, initialColIndex, initialRowIndex = sheetFinder(sheetName, readerOptions)
if !sheetFound {
continue
}
}
output[sheetName] = make(map[int]interface{})
cols, err := f.Cols(sheetName)
if err != nil {
return nil, err
}
currentColIndex := 1
for cols.Next() {
col, err := cols.Rows()
if err != nil {
return nil, err
}
if currentColIndex < initialColIndex {
currentColIndex++
continue
}
rowValue := make([]interface{}, 0)
for i := (initialRowIndex - 1); i < len(col); i++ {
cellValue := col[i]
if cellValue == "" {
cellValue = "N/A"
}
rowValue = append(rowValue, strings.TrimLeft(cellValue, " "))
}
output[sheetName][currentColIndex] = rowValue
currentColIndex++
}
}
return output, nil
}
func sheetFinder(sheetName string, readerOptions models.ReaderOption) (bool, int, int) {
for _, option := range readerOptions.Options {
if option.SheetName == sheetName {
if option.StartingCoordinates != "" {
initialColIndex, initialRowIndex, _ := excelize.CellNameToCoordinates(option.StartingCoordinates)
return true, initialColIndex, initialRowIndex
}
return true, 1, 1
}
}
return false, 1, 1
}