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
151
152
153
154
155
156
157
158
159
160
161
162
|
package main
import (
"archive/zip"
"fmt"
"github.com/tealeg/xlsx/v3"
"path/filepath"
)
type ReturnLine struct {
Sheet string
CellRef string
Value string
}
type Return struct {
Name string
ReturnLines []ReturnLine
}
// NewReturnLine creates a new ReturnLine object
func NewReturnLine(sheet, cellRef, value string) (*ReturnLine, error) {
if err := validateInputs(sheet, cellRef, value); err != nil {
return nil, err
}
if !validateSpreadsheetCell(cellRef) {
return nil, fmt.Errorf("cellRef must be A1 format")
}
return &ReturnLine{
Sheet: sheet,
CellRef: cellRef,
Value: value,
}, nil
}
func validateInputs(sheet, cellRef, value string) error {
if sheet == "" {
return fmt.Errorf("sheet parameter is required")
}
if cellRef == "" {
return fmt.Errorf("cellRef parameter is required")
}
if value == "" {
return fmt.Errorf("value parameter is required")
}
return nil
}
func NewReturn(name string, dm *Datamap, returnLines []ReturnLine) (*Return, error) {
if len(returnLines) == 0 {
return nil, fmt.Errorf("ReturnLines must contain at least one ReturnLine")
}
return &Return{
Name: name,
ReturnLines: returnLines,
}, nil
}
func ParseXLSX(filePath string, dm *Datamap) (*Return, error) {
// Use tealeg/xlsx to parse the Excel file
wb, err := xlsx.OpenFile(filePath)
if err != nil {
return nil, err
}
// Get the set of sheets from the Datamap
sheets := GetSheetsFromDM(*dm)
// Loop through all DatamapLines
returnLines := []ReturnLine{}
for _, dml := range dm.DMLs {
// Check if the sheet for this DatamapLine is in the set of sheets
if !contains(sheets, dml.Sheet) {
continue
}
sh, ok := wb.Sheet[dml.Sheet]
if !ok {
return nil, fmt.Errorf("sheet %s not found in Excel file", dml.Sheet)
}
col, row, err := xlsx.GetCoordsFromCellIDString(dml.CellRef)
if err != nil {
return nil, err
}
cell, err := sh.Cell(row, col)
if err != nil {
return nil, err
}
returnLines = append(returnLines, ReturnLine{
Sheet: dml.Sheet,
CellRef: dml.CellRef,
Value: cell.Value, // or cell.FormattedValue() if you need formatted values
})
}
// Here we create a new Return object with the name of the Excel file and the ReturnLines slice
// that we just populated
rtn, err := NewReturn(filepath.Base(filePath), dm, returnLines)
if err != nil {
return nil, err
}
return rtn, nil
}
// contains checks if a slice contains a given string
func contains(slice []string, str string) bool {
for _, s := range slice {
if s == str {
return true
}
}
return false
}
type FilePreparer interface {
Prepare(filePath string) error
}
type DirectoryFilePackage struct {
FilePath string
}
func (fp *DirectoryFilePackage) Prepare() ([]string, error) {
// return a slice of the files inside the directory pointed to by fh.FilePath
// and an error if any
files, err := filepath.Glob(fp.FilePath + "/*")
if err != nil {
return nil, err
}
return files, nil
}
type ZipFilePackage struct {
FilePath string
}
func (fp *ZipFilePackage) Prepare() ([]string, error) {
// return a slice of the files from inside the zip file pointed to by fh.FilePath
// and an error if any
files, err := zip.OpenReader(fp.FilePath)
if err != nil {
return nil, err
}
defer files.Close()
out := []string{}
for _, file := range files.File {
out = append(out, file.Name)
}
return out, nil
}
func NewDirectoryFilePackage(filePath string) *DirectoryFilePackage {
return &DirectoryFilePackage{FilePath: filePath}
}
func NewZipFilePackage(filePath string) *ZipFilePackage {
return &ZipFilePackage{FilePath: filePath}
}
|