forked from mentax/csv2xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
207 lines (163 loc) · 3.67 KB
/
write.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
package main
import (
"fmt"
"io"
"slices"
"unicode"
"github.com/tealeg/xlsx/v3"
)
func writeAllSheets(xlFile *xlsx.File, dataFiles []string, sheetNames []string, exampleRowNumber int, delimiter rune) (err error) {
for i, dataFileName := range dataFiles {
sheet, err := getSheet(xlFile, sheetNames, i)
if err != nil {
return err
}
var exampleRow *xlsx.Row
if exampleRowNumber != 0 && exampleRowNumber <= sheet.MaxRow {
// example row counting from 1
exampleRow, _ = sheet.Row(exampleRowNumber - 1)
err = sheet.RemoveRowAtIndex(exampleRowNumber - 1)
if err != nil {
return err
}
}
err = writeSheet(dataFileName, sheet, exampleRow, delimiter)
if err != nil {
return err
}
if sheet.DataValidations != nil {
for _, item := range sheet.DataValidations {
x, _, err := xlsx.GetCoordsFromCellIDString(item.Sqref)
if err == nil {
item.Sqref = item.Sqref + ":" + xlsx.GetCellIDStringFromCoords(x, sheet.MaxRow-1)
}
}
}
}
return nil
}
func writeSheet(dataFileName string, sheet *xlsx.Sheet, exampleRow *xlsx.Row, delimiter rune) error {
data, err := getCsvData(dataFileName)
if err != nil {
return err
}
data.Comma = delimiter
var i int
for {
record, err := data.Read()
if err == io.EOF || record == nil {
break
} else if err != nil {
return err
}
// if i > 5000 {
// break
// }
// if i%500 == 0 {
// fmt.Println(i)
// }
i++
writeRowToXls(sheet, record, exampleRow)
}
return nil
}
func buildXls(p *params) (err error) {
var xlFile *xlsx.File
if p.xlsxTemplate == "" {
xlFile = xlsx.NewFile()
} else {
xlFile, err = xlsx.OpenFile(p.xlsxTemplate)
if err != nil {
return err
}
}
err = writeAllSheets(xlFile, p.input, p.sheets, p.exampleRow, p.delimiter)
if err != nil {
return err
}
if p.deleteUnused {
var toDelete []int
for i, sheet := range xlFile.Sheets {
if !slices.Contains(p.sheets, sheet.Name) || sheet.MaxRow < p.exampleRow {
toDelete = append(toDelete, i)
}
}
for i, indexToDelete := range toDelete {
index := indexToDelete - i
delete(xlFile.Sheet, xlFile.Sheets[index].Name)
xlFile.Sheets = append(xlFile.Sheets[:index], xlFile.Sheets[index+1:]...)
}
}
return xlFile.Save(p.output)
}
func writeRowToXls(sheet *xlsx.Sheet, record []string, exampleRow *xlsx.Row) {
var row *xlsx.Row
var cell *xlsx.Cell
row = sheet.AddRow()
var cellsLen int
if exampleRow != nil {
cellsLen = exampleRow.Sheet.MaxCol
}
for k, v := range record {
cell = row.AddCell()
setCellValue(cell, v)
if exampleRow != nil && cellsLen > k {
style := exampleRow.GetCell(k).GetStyle()
format := exampleRow.GetCell(k).GetNumberFormat()
cell.SetStyle(style)
cell.SetFormat(format)
}
}
}
// setCellValue set data in correct format.
func setCellValue(cell *xlsx.Cell, v string) {
// fast path
if v == "" {
cell.SetString("")
return
}
// field marked as string
if v[0] == '\'' {
cell.SetString(v[1:])
return
}
if isNumeric(v) {
cell.SetNumeric(v)
return
}
cell.SetString(v)
}
func getSheet(xlFile *xlsx.File, sheetNames []string, i int) (sheet *xlsx.Sheet, err error) {
var sheetName string
if len(sheetNames) > i {
sheetName = sheetNames[i]
} else {
sheetName = fmt.Sprintf(SheetNamesTemplate, i+1)
}
sheet, ok := xlFile.Sheet[sheetName]
if ok != true {
sheet, err = xlFile.AddSheet(sheetName)
if err != nil {
return nil, err
}
}
return sheet, nil
}
func isNumeric(s string) bool {
for idx, c := range s {
if c == '.' {
continue
}
if c == '-' { // minus
if idx == 0 { // at the beginning
continue
} else {
return false
}
}
if !unicode.IsDigit(c) {
return false
}
}
return true
}