-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
547 lines (468 loc) · 13.2 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package main
import (
"encoding/csv"
"errors"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/xuri/excelize/v2"
"gopkg.in/ini.v1"
)
var (
version = "20221220"
//nolint:gochecknoglobals // TODO: уйти от глобальной переменной
DEBUG = false
)
type _replace struct {
from string
to string
}
type _action struct {
name string
value string
}
type _find struct {
text string
actions []_action
target string
style _style
styleID int
}
type _column struct {
id int
// name string
width int
replaces []_replace
finds []_find
deleted bool
horizontal string
size float64
}
type _defColParam struct {
width int
// horizontal string
}
type _style struct {
Alignment excelize.Alignment
Border []excelize.Border
Font excelize.Font
Fill excelize.Fill
}
type _title struct {
enable bool
text string
size float64
bold bool
color string
background string
}
type _header struct {
enable bool
row int
size float64
bold bool
color string
background string
horizontal string
}
type _cfg struct {
sheetName string
title _title
header _header
delimiter rune
cols []_column
style _style
defcol _defColParam
}
func usage() {
fmt.Println("Конвертирование csv в xlsx с форматированием.")
fmt.Printf("версия: %s\n\n", version)
fmt.Println("csv2xlsx [command] <args>")
fmt.Println("command:")
flag.PrintDefaults()
}
// Парсим проблемые поля из csv
func csvParseErrFieldCount(reads []string, fieldCount int) [][]string {
// Массив с распарсенными по полям строками
var parsedRows [][]string
// id поля которое парсится
var unCol = 0
// Текущая строка csv
var st []string
// Идем по полученным полям и ищем косяк c "":
// "" будет как "\n", при этом два поля объеденятся
for _, field := range reads {
f := strings.Split(field, "\n")
for i, r := range f {
// все что больше 0, исправленные. В начале надо убрать "
if i != 0 {
r = strings.Replace(r, "\"", "", 1)
}
if DEBUG {
fmt.Println("[DBG] ", r)
}
if len(r) != 0 {
st = append(st, r)
unCol++
}
if unCol >= fieldCount {
parsedRows = append(parsedRows, st)
if DEBUG {
fmt.Printf("[DBG] add rows, %d columns\n", len(st))
}
st = make([]string, 0)
unCol = 0
}
}
}
return parsedRows
}
// Генерация xlsx из csv
func generateXLSXFromCSV(csvPath string, delimiter rune) (*excelize.File, error) {
if DEBUG {
fmt.Println("[DBG] Открываем файл ", csvPath)
}
csvFile, err := os.Open(csvPath)
if err != nil {
return nil, err
}
defer csvFile.Close()
reader := csv.NewReader(csvFile)
// reader.Comma = '\t'
reader.Comma = delimiter
reader.LazyQuotes = true
// reader.ReuseRecord = true
xlsxFile := excelize.NewFile()
// sheet := "Sheet1"
sheet := xlsxFile.GetSheetName(0)
row := 1
for {
if DEBUG {
fmt.Printf("[DBG] Обрабатываем строку: %d\n", row)
}
// Считываем следующую строку
fields, err := reader.Read()
if errors.Is(err, io.EOF) {
break
}
//nolint:nestif // TODO: переписать
if err != nil {
switch {
// Пытаемся обработать заголовок ВНИИРА
case errors.Is(err, csv.ErrFieldCount) && row == 2:
if DEBUG {
fmt.Println("[DBG] не совпало кол-во полей с пред. записью.\nТ.к. срока 2, считаем заголовком.")
}
reader.FieldsPerRecord = 0
case errors.Is(err, csv.ErrFieldCount):
// TODO: переписать по человечески
if DEBUG {
fmt.Println("[ERR] не совпало кол-во полей с пред. записью.")
fmt.Println(fields)
fmt.Println("[DBG] пытаемся обработать")
}
// Пытаемся обработать ""
parsedRows := csvParseErrFieldCount(fields, reader.FieldsPerRecord-1)
// Добавляем полученные строки в файл
for _, fields := range parsedRows {
for i, field := range fields {
if DEBUG {
fmt.Printf("[DBG] строка: %d, столбец: %d\n", row, i)
}
column, err := excelize.ColumnNumberToName(i + 1)
if err != nil {
return nil, err
}
err = xlsxFile.SetCellStr(sheet, fmt.Sprintf("%s%d", column, row), field)
if err != nil {
return nil, err
}
}
row++
}
continue
default:
return nil, err
}
}
for i, field := range fields {
if DEBUG {
fmt.Printf("[DBG] строка: %d, столбец: %d\n", row, i)
}
column, err := excelize.ColumnNumberToName(i + 1)
if err != nil {
return nil, err
}
err = xlsxFile.SetCellStr(sheet, fmt.Sprintf("%s%d", column, row), field)
if err != nil {
return nil, err
}
}
row++
}
return xlsxFile, nil
}
func main() {
var xlsxPath = flag.String("o", "", "Path to the XLSX output file")
var csvPath = flag.String("f", "", "Path to the CSV input file")
var cfgPatch = flag.String("c", "", "Path to the config file (optional)")
var delimiter = flag.String("d", "\t", "Delimiter for felds in the CSV input (optional)")
flag.BoolVar(&DEBUG, "debug", false, "Debug (optional)")
if len(os.Args) < 2 {
usage()
return
}
flag.Usage = usage
flag.Parse()
if *xlsxPath == "" || *csvPath == "" {
flag.Usage()
return
}
cfg := new(_cfg)
// Загрузка настроек
if *cfgPatch != "" {
if DEBUG {
fmt.Println("Load config: ", *cfgPatch)
}
if err := loadCFG(*cfgPatch, cfg); err != nil {
fmt.Println(err.Error())
}
} else {
cfg.delimiter = delimiterToRune(*delimiter)
}
// Генерируем xlsx
xlsxFile, err := generateXLSXFromCSV(*csvPath, cfg.delimiter)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
// Если есть конфиг, применяем настройки
if *cfgPatch != "" {
if err := applyFormatting(xlsxFile, cfg); err != nil {
fmt.Println(err.Error())
}
}
// Сохраняем результат
if err := xlsxFile.SaveAs(*xlsxPath); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
os.Exit(0)
}
// Загрузка настроек из ini в переменную cfg
func loadCFG(iniFile string, cfg *_cfg) error {
inifile, err := ini.ShadowLoad(iniFile)
if err != nil {
return err
}
// Общие настройки
cfg.sheetName = inifile.Section("").Key("sheet").MustString("")
cfg.delimiter = delimiterToRune(inifile.Section("").Key("delimiter").MustString(`\t`))
// Добавить границу
if inifile.Section("").Key("border").MustBool(false) {
cfg.style.Border = []excelize.Border{
{
Type: "left",
Color: "#000000",
Style: 2,
}, {
Type: "top",
Color: "#000000",
Style: 2,
}, {
Type: "bottom",
Color: "#000000",
Style: 2,
}, {
Type: "right",
Color: "#000000",
Style: 2,
},
}
}
// Задать общий стиль
// Перенос текста
cfg.style.Alignment.WrapText = true
// Alignment
if err := setAlignment(&cfg.style.Alignment, "horizontal", inifile.Section("").Key("horizontal").MustString("")); err != nil {
fmt.Println("[WRN]\tloadCFG: horizontal: ", err.Error())
}
if err := setAlignment(&cfg.style.Alignment, "vertical", inifile.Section("").Key("vertical").MustString("")); err != nil {
fmt.Println("[WRN]\tloadCFG: vertical: ", err.Error())
}
// Font
if inifile.Section("").Key("size").MustFloat64(0) != 0 {
cfg.style.Font.Size = inifile.Section("").Key("size").MustFloat64(0)
}
if inifile.Section("").Key("family").MustString("") != "" {
cfg.style.Font.Family = inifile.Section("").Key("family").MustString("")
}
cfg.defcol.width = inifile.Section("").Key("width").MustInt(-1)
// Секции
for _, section := range inifile.SectionStrings() {
// Кроме default
if section == "DEFAULT" {
continue
}
// Настройки шапки
if section == "title" {
if inifile.Section(section).Key("enable").MustBool(false) {
cfg.title.enable = true
cfg.title.text = inifile.Section(section).Key("text").MustString("")
cfg.title.bold = inifile.Section(section).Key("bold").MustBool(false)
cfg.title.size = inifile.Section(section).Key("size").MustFloat64(0)
cfg.title.color = inifile.Section(section).Key("color").MustString("")
cfg.title.background = inifile.Section(section).Key("background").MustString("")
} else {
cfg.title.enable = false
}
}
// Настройки заголовка
if section == "header" {
if inifile.Section(section).Key("enable").MustBool(false) {
cfg.header.enable = true
cfg.header.row = inifile.Section(section).Key("row").MustInt(1)
cfg.header.bold = inifile.Section(section).Key("bold").MustBool(false)
cfg.header.size = inifile.Section(section).Key("size").MustFloat64(0)
cfg.header.color = inifile.Section(section).Key("color").MustString("")
cfg.header.background = inifile.Section(section).Key("background").MustString("")
cfg.header.horizontal = inifile.Section(section).Key("horizontal").MustString("center")
} else {
cfg.header.enable = false
}
}
// Настройки столбцов
// Добавляем данные секции (столбца)
col, err := loadColumnSettings(inifile, section)
if err == nil {
cfg.cols = append(cfg.cols, col)
}
} // конец цикла по секциям
return nil
}
// Загрузка настроек столбца
func loadColumnSettings(inifile *ini.File, section string) (_column, error) {
var col _column
var err error
col.id, err = strconv.Atoi(section)
// Если секция не цифровая, пропускаем
if err != nil {
return col, err
}
col.width = inifile.Section(section).Key("width").MustInt(-1)
col.horizontal = inifile.Section(section).Key("horizontal").MustString("")
col.deleted = inifile.Section(section).Key("delete").MustBool(false)
col.size = inifile.Section(section).Key("size").MustFloat64(0)
// Load replaces
allreplace := inifile.Section(section).Key("replace").ValueWithShadows()
for _, repl := range allreplace {
ss := strings.Split(repl, ",")
if len(ss) == 2 {
col.replaces = append(col.replaces, _replace{strings.Trim(ss[0], "\"'"), strings.Trim(ss[1], " \"'")})
}
}
// Load finds
allfinds := inifile.Section(section).Key("find").ValueWithShadows()
for _, find := range allfinds {
ss := strings.Split(find, ",")
if len(ss) > 2 {
var f _find
f.text = strings.Trim(ss[0], " \"'")
f.target = strings.Trim(ss[1], " \"'")
// Идем по всем actions
for i := 2; i < len(ss); i++ {
actionsl := strings.Split(strings.TrimSpace(ss[i]), "=")
var action _action
action.name = strings.Trim(actionsl[0], "\"'")
if len(actionsl) > 1 {
action.value = strings.Trim(actionsl[1], "\"'")
}
f.actions = append(f.actions, action)
}
col.finds = append(col.finds, f)
}
}
return col, nil
}
func delimiterToRune(delimiter string) rune {
switch delimiter {
case ` `:
return rune(' ')
case `:`:
return rune(':')
case `;`:
return rune(';')
case `,`:
return rune(',')
case `\t`:
return '\t'
default:
if len(delimiter) > 0 {
return rune(delimiter[0])
}
}
return rune('\t')
}
// Применяем настройки форматирования
func applyFormatting(xlsxFile *excelize.File, cfg *_cfg) error {
// Получаем название листа
sheetName := xlsxFile.GetSheetName(0)
if sheetName == "" {
return fmt.Errorf("sheet not found")
}
// задаем форматирование всей таблице
if err := xlsxSetTableStyle(xlsxFile, sheetName, cfg.style, cfg.defcol); err != nil {
return err
}
// обрабатываем параметры столбцов
if err := xlsxSetColumnFormat(xlsxFile, sheetName, cfg.cols, cfg.style); err != nil {
return err
}
// Стиль заголовка
if cfg.header.enable {
// Получаем последний столбец
cols, err := xlsxFile.GetCols(sheetName)
if err != nil {
return err
}
lastColumn, err := excelize.ColumnNumberToName(len(cols))
if err != nil {
return err
}
// Устанавливаем стиль заголовка
if err := xlsxSetHeader(xlsxFile, sheetName, fmt.Sprintf("A%d", cfg.header.row), fmt.Sprintf("%s%d", lastColumn, cfg.header.row), cfg.header, cfg.style); err != nil {
return err
}
}
// Добавить Title
if cfg.title.enable {
if err := xlsxAddTitle(xlsxFile, sheetName, cfg.title, cfg.style); err != nil {
return err
}
}
// переименовываем лист
if cfg.sheetName != "" {
if err := xlsxFile.SetSheetName(sheetName, cfg.sheetName); err != nil {
return err
}
}
return nil
}
// TO DO
// 1. Вынести xlsx в отделный модуль
// 2. Добавить условный формат:
// -eq
// равно
// -ne
// не равно
// -gt
// больше
// -ge
// больше или равно
// -lt
// меньше
// -le
// меньше или равно