Repository navigation
Expand file tree
/
Copy pathcell.go
More file actions
475 lines (400 loc) · 11.3 KB
/
Copy pathcell.go
File metadata and controls
475 lines (400 loc) · 11.3 KB
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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/xuri/excelize/v2"
)
const styleOnlyCellScanLimit = 100_000
// CellKind describes the semantic kind Excelize reports for a loaded cell.
type CellKind string
const (
// CellKindUnset marks cells with no concrete Excel cell kind, such as styled blanks.
CellKindUnset CellKind = "unset"
// CellKindBool marks boolean cells.
CellKindBool CellKind = "bool"
// CellKindDate marks date cells.
CellKindDate CellKind = "date"
// CellKindError marks formula/error cells.
CellKindError CellKind = "error"
// CellKindFormula marks formula cells.
CellKindFormula CellKind = "formula"
// CellKindInlineString marks inline string cells.
CellKindInlineString CellKind = "inlineString"
// CellKindNumber marks numeric cells.
CellKindNumber CellKind = "number"
// CellKindString marks shared-string and literal edit cells.
CellKindString CellKind = "string"
)
// AllCellKinds lists CellKind values for Wails enum binding.
var AllCellKinds = []struct {
Value CellKind
TSName string
}{
{CellKindUnset, "Unset"},
{CellKindBool, "Bool"},
{CellKindDate, "Date"},
{CellKindError, "Error"},
{CellKindFormula, "Formula"},
{CellKindInlineString, "InlineString"},
{CellKindNumber, "Number"},
{CellKindString, "String"},
}
func parseCellAddress(ref string) (CellAddress, bool) {
trimmed := strings.TrimSpace(ref)
if trimmed == "" {
return CellAddress{}, false
}
// Excel refs are column letters followed by row digits, with no separator.
split := strings.IndexFunc(trimmed, func(r rune) bool {
return r >= '0' && r <= '9'
})
if split <= 0 || split == len(trimmed) {
return CellAddress{}, false
}
letters := strings.ToUpper(trimmed[:split])
digits := trimmed[split:]
if !isColumnLetters(letters) || !isRowDigits(digits) {
return CellAddress{}, false
}
column, ok := columnLettersToNumber(letters)
if !ok {
return CellAddress{}, false
}
row, err := strconv.Atoi(digits)
if err != nil || row < minExcelRow || row > maxExcelRow {
return CellAddress{}, false
}
return CellAddress{Ref: letters + strconv.Itoa(row), Row: row, Column: column}, true
}
func loadSheetCells(
file *excelize.File,
sheetName string,
formattedRows [][]string,
rawRows [][]string,
) ([]CellData, map[int]struct{}, cellBounds, error) {
styleIDs := map[int]struct{}{}
bounds := cellBounds{}
rowCount := max(len(formattedRows), len(rawRows))
cells := make([]CellData, 0)
for rowIndex := range rowCount {
columnCount := max(len(rowValues(formattedRows, rowIndex)), len(rowValues(rawRows, rowIndex)))
for columnIndex := range columnCount {
formattedValue := valueAt(formattedRows, rowIndex, columnIndex)
rawValue := valueAt(rawRows, rowIndex, columnIndex)
cell, include, err := loadCell(file, sheetName, rowIndex+1, columnIndex+1, formattedValue, rawValue)
if err != nil {
return nil, nil, cellBounds{}, err
}
if !include {
continue
}
if cell.StyleID > 0 {
styleIDs[cell.StyleID] = struct{}{}
}
bounds.addAddress(CellAddress{Ref: cell.Ref, Row: cell.Row, Column: cell.Column})
cells = append(cells, cell)
}
}
return cells, styleIDs, bounds, nil
}
func loadStyleOnlyCells(
file *excelize.File,
sheetName string,
cells []CellData,
bounds CellRange,
) ([]CellData, map[int]struct{}, error) {
styleIDs := map[int]struct{}{}
rowCount := bounds.End.Row - bounds.Start.Row + 1
columnCount := bounds.End.Column - bounds.Start.Column + 1
// Style-only cells require probing; cap this to avoid huge blank-sheet scans.
if rowCount <= 0 || columnCount <= 0 || rowCount*columnCount > styleOnlyCellScanLimit {
return cells, styleIDs, nil
}
seen := make(map[string]struct{}, len(cells))
for _, cell := range cells {
seen[cell.Ref] = struct{}{}
}
for row := bounds.Start.Row; row <= bounds.End.Row; row++ {
for column := bounds.Start.Column; column <= bounds.End.Column; column++ {
ref, err := excelize.CoordinatesToCellName(column, row)
if err != nil {
return nil, nil, err
}
if _, ok := seen[ref]; ok {
continue
}
styleID, err := file.GetCellStyle(sheetName, ref)
if err != nil {
return nil, nil, err
}
if styleID == 0 {
continue
}
formattedValue, err := file.GetCellValue(sheetName, ref)
if err != nil {
return nil, nil, err
}
rawValue, err := file.GetCellValue(sheetName, ref, excelize.Options{RawCellValue: true})
if err != nil {
return nil, nil, err
}
cell, include, err := loadCell(file, sheetName, row, column, formattedValue, rawValue)
if err != nil {
return nil, nil, err
}
if !include {
continue
}
styleIDs[cell.StyleID] = struct{}{}
seen[ref] = struct{}{}
cells = append(cells, cell)
}
}
return cells, styleIDs, nil
}
func loadCell(
file *excelize.File,
sheetName string,
row int,
column int,
value string,
rawValue string,
) (CellData, bool, error) {
ref, err := excelize.CoordinatesToCellName(column, row)
if err != nil {
return CellData{}, false, err
}
formula, err := file.GetCellFormula(sheetName, ref)
if err != nil {
return CellData{}, false, err
}
styleID, err := file.GetCellStyle(sheetName, ref)
if err != nil {
return CellData{}, false, err
}
cellType, err := file.GetCellType(sheetName, ref)
if err != nil {
return CellData{}, false, err
}
hasFormula := formula != ""
kind, hasConcreteKind := cellKind(cellType, hasFormula)
// Preserve styled and formula cells even when their displayed value is empty.
include := value != "" || rawValue != "" || hasFormula || styleID != 0 || hasConcreteKind
if !include {
return CellData{}, false, nil
}
return CellData{
Ref: ref,
Row: row,
Column: column,
Value: value,
RawValue: rawValue,
Formula: formula,
HasFormula: hasFormula,
Kind: kind,
StyleID: styleID,
}, true, nil
}
func expandedSheetBounds(current CellRange, address CellAddress) (CellRange, error) {
bounds := cellBounds{}
if current.valid() {
bounds.addRange(current)
} else {
bounds.addRange(a1Range())
}
bounds.addAddress(address)
return bounds.rangeValue()
}
func (c CellRange) valid() bool {
return c.Start.valid() &&
c.End.valid() &&
c.Start.Row <= c.End.Row &&
c.Start.Column <= c.End.Column
}
func (c CellAddress) valid() bool {
return c.Row >= minExcelRow && c.Row <= maxExcelRow &&
c.Column >= minExcelColumn && c.Column <= maxExcelColumn
}
func parseCellRangeRef(ref string) (CellRange, error) {
parts := strings.Split(strings.TrimSpace(ref), ":")
if len(parts) == 0 || len(parts) > 2 {
return CellRange{}, fmt.Errorf("invalid cell range %q", ref)
}
start, err := parseExcelCellAddress(parts[0])
if err != nil {
return CellRange{}, err
}
end := start
if len(parts) == 2 {
end, err = parseExcelCellAddress(parts[1])
if err != nil {
return CellRange{}, err
}
}
if end.Row < start.Row {
start.Row, end.Row = end.Row, start.Row
}
if end.Column < start.Column {
start.Column, end.Column = end.Column, start.Column
}
return cellRangeFromCoordinates(start.Row, start.Column, end.Row, end.Column)
}
func parseExcelCellAddress(ref string) (CellAddress, error) {
// Excelize coordinate helpers expect relative references, not $A$1 syntax.
cleanRef := strings.ReplaceAll(strings.TrimSpace(ref), "$", "")
column, row, err := excelize.CellNameToCoordinates(cleanRef)
if err != nil {
return CellAddress{}, err
}
if row < minExcelRow || row > maxExcelRow || column < minExcelColumn || column > maxExcelColumn {
return CellAddress{}, fmt.Errorf("cell reference %q is outside supported Excel bounds", ref)
}
cellRef, err := excelize.CoordinatesToCellName(column, row)
if err != nil {
return CellAddress{}, err
}
return CellAddress{Ref: cellRef, Row: row, Column: column}, nil
}
func cellRangeFromCoordinates(startRow int, startColumn int, endRow int, endColumn int) (CellRange, error) {
startRef, err := excelize.CoordinatesToCellName(startColumn, startRow)
if err != nil {
return CellRange{}, err
}
endRef, err := excelize.CoordinatesToCellName(endColumn, endRow)
if err != nil {
return CellRange{}, err
}
ref := startRef
if startRef != endRef {
ref = startRef + ":" + endRef
}
return CellRange{
Ref: ref,
Start: CellAddress{Ref: startRef, Row: startRow, Column: startColumn},
End: CellAddress{Ref: endRef, Row: endRow, Column: endColumn},
}, nil
}
func a1Range() CellRange {
address := CellAddress{Ref: "A1", Row: minExcelRow, Column: minExcelColumn}
return CellRange{Ref: address.Ref, Start: address, End: address}
}
func cellKind(cellType excelize.CellType, hasFormula bool) (CellKind, bool) {
if hasFormula {
return CellKindFormula, true
}
switch cellType {
case excelize.CellTypeBool:
return CellKindBool, true
case excelize.CellTypeDate:
return CellKindDate, true
case excelize.CellTypeError:
return CellKindError, true
case excelize.CellTypeFormula:
return CellKindFormula, true
case excelize.CellTypeInlineString:
return CellKindInlineString, true
case excelize.CellTypeNumber:
return CellKindNumber, true
case excelize.CellTypeSharedString:
return CellKindString, true
case excelize.CellTypeUnset:
return CellKindUnset, false
default:
return CellKindUnset, false
}
}
func rowValues(rows [][]string, rowIndex int) []string {
if rowIndex < 0 || rowIndex >= len(rows) {
return nil
}
return rows[rowIndex]
}
func valueAt(rows [][]string, rowIndex int, columnIndex int) string {
row := rowValues(rows, rowIndex)
if columnIndex < 0 || columnIndex >= len(row) {
return ""
}
return row[columnIndex]
}
func isColumnLetters(value string) bool {
for i := range value {
if value[i] < 'A' || value[i] > 'Z' {
return false
}
}
return true
}
func isRowDigits(value string) bool {
if value[0] == '0' {
return false
}
for i := range value {
if value[i] < '0' || value[i] > '9' {
return false
}
}
return true
}
func columnLettersToNumber(letters string) (int, bool) {
column := 0
// Excel columns are base-26 letters without a zero digit: A=1, Z=26, AA=27.
for i := range letters {
column = column*26 + int(letters[i]-'A'+1)
if column > maxExcelColumn {
return 0, false
}
}
if column < minExcelColumn {
return 0, false
}
return column, true
}
func clampZoomPercent(percent int) int {
if percent < minZoomPercent {
return minZoomPercent
}
if percent > maxZoomPercent {
return maxZoomPercent
}
return percent
}
type cellBounds struct {
minRow int
minColumn int
maxRow int
maxColumn int
hasValues bool
}
func (c *cellBounds) addAddress(address CellAddress) {
if !c.hasValues {
c.minRow = address.Row
c.maxRow = address.Row
c.minColumn = address.Column
c.maxColumn = address.Column
c.hasValues = true
return
}
c.minRow = min(c.minRow, address.Row)
c.maxRow = max(c.maxRow, address.Row)
c.minColumn = min(c.minColumn, address.Column)
c.maxColumn = max(c.maxColumn, address.Column)
}
func (c *cellBounds) addRange(cellRange CellRange) {
c.addAddress(cellRange.Start)
c.addAddress(cellRange.End)
}
func (c *cellBounds) addBounds(other cellBounds) {
if !other.hasValues {
return
}
// Bounds merging only needs coordinates; refs are rebuilt for final output.
c.addAddress(CellAddress{Row: other.minRow, Column: other.minColumn})
c.addAddress(CellAddress{Row: other.maxRow, Column: other.maxColumn})
}
func (c *cellBounds) rangeValue() (CellRange, error) {
if !c.hasValues {
return a1Range(), nil
}
return cellRangeFromCoordinates(c.minRow, c.minColumn, c.maxRow, c.maxColumn)
}