-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
210 lines (201 loc) · 4.82 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"encoding/csv"
"fmt"
"io"
"os"
"path"
"strings"
"time"
"github.com/xuri/excelize/v2"
"golang.org/x/exp/slices"
)
type csvInfos struct {
path string
sheetName string
}
// create map to change keys
var (
mapKeys = map[string]string{
"created_at": "date",
"id": "reference",
}
)
func main() {
start := time.Now()
// get all args
commandLineArgs := os.Args[1:]
// check if path exists
if len(commandLineArgs) == 0 {
panic("Please provide a path")
}
// data folder path
dataFolderPath := commandLineArgs[0]
// read files
files, err := os.ReadDir(dataFolderPath)
if err != nil {
panic("error reading the folder")
}
// declar
var csvPathsAndNames []csvInfos
// iterate over files
for _, file := range files {
if !file.IsDir() {
if strings.Contains(file.Name(), ".csv") {
fileNameWoExt := strings.Split(file.Name(), ".")[0]
// collect needed data
csvPathsAndNames = append(csvPathsAndNames, csvInfos{
sheetName: toPascaleCase(fileNameWoExt),
path: path.Join(dataFolderPath, file.Name()),
})
}
}
}
// create excel file
excelFile := excelize.NewFile(excelize.Options{})
//
for _, tableInfos := range csvPathsAndNames {
_ = printTableInExcel(excelFile, tableInfos)
// err := excelFile.AddTable(tableInfos.sheetName, &excelize.Table{
// Range: rangeCells,
// Name: "Table" + tableInfos.sheetName,
// StyleName: "TableStyleMedium9",
// })
// if err != nil {
// fmt.Println(err)
// panic("error creating table")
// }
}
// create excel
year, month, day := time.Now().Date()
excelFile.SaveAs(fmt.Sprintf("%s/%d-%d-%d-%d.xlsx", strings.TrimSuffix(dataFolderPath, "/"), year, month, day, time.Now().UnixMilli()))
end := time.Now()
fmt.Print(end.Sub(start))
}
// inventory_mouvements -> InventoryMouvement
func toPascaleCase(name string) string {
if name == "" {
return ""
}
var result string = ""
if strings.Contains(name, "_") {
for _, substring := range strings.Split(name, "_") {
result = result + toPascaleCase(substring)
}
} else {
firstChart := name[0:1]
result = strings.ToUpper(firstChart) + name[1:]
}
return result
}
// rearrange an array
func rearrangeArray(array []string, from int, to int) []string {
if from == to {
return array
}
// get first element
firstElement := array[from]
// remove first element
array = slices.Delete(array, from, from+1)
// insert first element
array = slices.Insert(array, to, firstElement)
return array
}
// csv file to excel sheet
func printTableInExcel(excelFile *excelize.File, infos csvInfos) string {
// re arrange headers
var rearrangeInfos struct {
image struct {
exists bool
mouvements struct {
from int
to int
}
}
}
//
err := excelFile.DeleteSheet("Sheet1")
if err != nil {
panic("error deleting sheet")
}
// get file data
csvFile, err := os.Open(infos.path)
if err != nil {
panic("error opening file")
}
defer csvFile.Close()
// get reader
reader := csv.NewReader(csvFile)
// get sheet
_, err = excelFile.NewSheet(infos.sheetName)
if err != nil {
panic("error creating sheet")
}
// iterate over rows
row := 1
column := 0
for {
// get data
record, err := reader.Read()
if err == io.EOF {
// panic("no more input to read")
break
}
if err != nil {
panic("error reading record 2:")
}
// get cell name from cords
cell, err := excelize.CoordinatesToCellName(1, row)
if err != nil {
panic("error getting cell name :")
}
// write row
if row == 1 {
// change image to first col
// check for images
imageExists := slices.Contains(record, "image")
rearrangeInfos.image.exists = true
if imageExists {
// get image cell
imageIndex := slices.Index[[]string, string](record, "image")
rearrangeInfos.image.mouvements.from = imageIndex
rearrangeInfos.image.mouvements.to = 0
record = rearrangeArray(record, imageIndex, 0)
}
// get columns
column = len(record)
// update headers
for i, header := range record {
// get if it exists
updatedKey, ok := mapKeys[header]
if ok {
record[i] = toPascaleCase(updatedKey)
continue
}
record[i] = toPascaleCase(header)
}
// write headers
if err := excelFile.SetSheetRow(infos.sheetName, cell, &record); err != nil {
panic("coudnt write first row : ")
}
row++
continue
}
if rearrangeInfos.image.exists {
// check for images
record = rearrangeArray(record, rearrangeInfos.image.mouvements.from, rearrangeInfos.image.mouvements.to)
}
// write headers
if err := excelFile.SetSheetRow(infos.sheetName, cell, &record); err != nil {
panic("coudnt write row : ")
}
row++
// write other records
}
cell, err := excelize.CoordinatesToCellName(column, row-1)
if err != nil {
panic("error getting cell name :")
}
rangeCells := fmt.Sprintf("A1:%s", cell)
return rangeCells
}