-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlsx.go
132 lines (125 loc) · 3.62 KB
/
xlsx.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
package utils
import (
"github.com/google/martian/log"
"github.com/spf13/cast"
"github.com/tealeg/xlsx/v3"
"strings"
)
const (
VLOOKUP = "VLOOKUP"
)
func SetCellValueToSheet(oldCell, newCell *xlsx.Cell, sheet *xlsx.Sheet) {
switch oldCell.Type() {
case xlsx.CellTypeString:
newCell.SetValue(oldCell.Value)
case xlsx.CellTypeStringFormula:
formula := handleFormula(oldCell.Formula())
switch formula.fType {
case VLOOKUP:
x, y, _ := xlsx.GetCoordsFromCellIDString(formula.vLookup.searchKey)
row, _ := sheet.File.Sheet[formula.vLookup.sheetName].Row(y)
searchValue := GetCellValueBySheet(row.GetCell(x), sheet)
foundRowIdx := -1
sheet.File.Sheet[formula.vLookup.sheetName].ForEachRow(func(r *xlsx.Row) error {
for _, colIdx := range formula.vLookup.colRange {
if GetCellValueBySheet(r.GetCell(colIdx), sheet) == searchValue {
foundRowIdx = r.GetCoordinate()
}
}
return nil
})
if foundRowIdx != -1 {
selectedRow, _ := sheet.File.Sheet[formula.vLookup.sheetName].Row(foundRowIdx)
SetCellValueToSheet(selectedRow.GetCell(formula.vLookup.resultCol), newCell, sheet)
}
default:
formula := oldCell.Formula()
if len(strings.Split(formula, "!")) >= 2 {
x, y, _ := xlsx.GetCoordsFromCellIDString(strings.Split(formula, "!")[1])
row, _ := sheet.File.Sheet[strings.Split(formula, "!")[0]].Row(y)
SetCellValueToSheet(row.GetCell(x), newCell, sheet)
} else {
panic(formula)
}
}
case xlsx.CellTypeNumeric:
formula := oldCell.Formula()
if len(strings.Split(formula, "!")) >= 2 {
x, y, _ := xlsx.GetCoordsFromCellIDString(strings.Split(formula, "!")[1])
row, _ := sheet.File.Sheet[strings.Split(formula, "!")[0]].Row(y)
SetCellValueToSheet(row.GetCell(x), newCell, sheet)
} else {
newCell.SetNumeric(oldCell.Value)
}
case xlsx.CellTypeBool:
newCell.SetBool(oldCell.Bool())
case xlsx.CellTypeInline:
fallthrough
case xlsx.CellTypeDate:
ct, err := oldCell.GetTime(false)
if err != nil {
panic(err)
}
newCell.SetDate(ct)
}
}
func GetCellValueBySheet(cell *xlsx.Cell, sheet *xlsx.Sheet) any {
switch cell.Type() {
case xlsx.CellTypeString:
return cell.Value
case xlsx.CellTypeNumeric:
if cell.Formula() == "" {
v, err := cell.GeneralNumericWithoutScientific()
if err != nil {
panic(err)
}
return v
}
fallthrough
case xlsx.CellTypeStringFormula:
formula := cell.Formula()
// TODO
if len(strings.Split(formula, "!")) >= 2 {
x, y, _ := xlsx.GetCoordsFromCellIDString(strings.Split(formula, "!")[1])
row, _ := sheet.File.Sheet[strings.Split(formula, "!")[0]].Row(y)
return GetCellValueBySheet(row.GetCell(x), sheet)
} else {
log.Errorf("%v", formula)
}
case xlsx.CellTypeBool:
return cell.Bool()
case xlsx.CellTypeInline:
fallthrough
case xlsx.CellTypeDate:
ct, err := cell.GetTime(false)
if err != nil {
panic(err)
}
return ct
}
//panic("empty type")
return ""
}
type formula struct {
fType string
vLookup vLookup
}
type vLookup struct {
searchKey string
sheetName string
colRange []int
resultCol int
}
func handleFormula(f string) formula {
var result vLookup
if strings.HasPrefix(f, VLOOKUP) {
fs := strings.Split(strings.TrimSuffix(strings.TrimPrefix(f, VLOOKUP+"("), ")"), ",")
result.searchKey = fs[0]
result.resultCol = cast.ToInt(fs[2]) - 1
ran := strings.Split(fs[1], "!")
result.sheetName = strings.Split(ran[0], "]")[len(strings.Split(ran[0], "]"))-1]
result.colRange = GetUniqueColRange(xlsx.ColLettersToIndex(strings.Split(ran[1], ":")[0]), xlsx.ColLettersToIndex(strings.Split(ran[1], ":")[1]))
return formula{fType: VLOOKUP, vLookup: result}
}
return formula{}
}