-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.data.diff.go
152 lines (137 loc) · 3.87 KB
/
db.data.diff.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
package markdown
import (
"fmt"
"reflect"
"sort"
"strings"
"github.com/micro-plat/hicli/markdown/const/enums"
"github.com/micro-plat/hicli/markdown/tmpl"
"github.com/micro-plat/lib4go/types"
"github.com/urfave/cli"
)
//createDataDiff 比较差异文件
func createDataDiff(c *cli.Context) (err error) {
if len(c.Args()) < 2 {
return fmt.Errorf("未指定需要对比的源xlsx文件和目标xlsx文件")
}
//读取excel
sheet := types.GetString(c.String("sheet"), "Sheet1")
sourcePath := c.Args().First()
sourceFile, err := tmpl.GetExcel(sourcePath, sheet)
if err != nil {
return fmt.Errorf("读取%s出错:%+v", sourcePath, err)
}
sourceRows := sourceFile.GetRows(sheet)
if len(sourceRows) < 1 {
return fmt.Errorf("%s为空", sourcePath)
}
targetPath := c.Args().Get(1)
targetFile, err := tmpl.GetExcel(targetPath, sheet)
if err != nil {
return fmt.Errorf("读取%s出错:%+v", targetPath, err)
}
targetRows := targetFile.GetRows(sheet)
if len(targetRows) < 1 {
return fmt.Errorf("%s为空", targetPath)
}
//处理比较列
titles := make([]string, 0)
if c.String("titles") != "" {
titles = strings.Split(c.String("titles"), ",")
}
sourceTitle := sourceRows[0]
targetTitle := targetRows[0]
if len(titles) == 0 { //全部列比较
if !reflect.DeepEqual(sourceTitle, targetTitle) {
return fmt.Errorf("%s和%s标题或者标题顺序不一致", sourcePath, targetPath)
}
}
sourceDataMap, err := getDataMap(sourceRows, titles)
if err != nil {
return fmt.Errorf("%s:%+v", sourcePath, err)
}
targetDataMap, err := getDataMap(targetRows, titles)
if err != nil {
return fmt.Errorf("%s:%+v", targetPath, err)
}
//比较并将不一样的数据样式填充为红色
diff := dataDiff(sourceDataMap, targetDataMap)
style, err := sourceFile.NewStyle(`{"fill":{"type":"gradient","color":["#FF0000","#FF0000"],"shading":1}}`)
if err != nil {
return fmt.Errorf("设置样式错误:%+v", err)
}
for _, v := range diff { //修改excel
sourceFile.SetCellStyle(sheet, fmt.Sprintf("A%d", v.index), fmt.Sprintf("%s%d", getEnd(len(sourceTitle)), v.index), style)
}
//保存样式
err = sourceFile.Save()
if err != nil {
return fmt.Errorf("保存样式错误:%+v", err)
}
return nil
}
//getDataMap 获取excel对应map,以_拼接对比列的数据
func getDataMap(rows [][]string, titles []string) (dataMap map[string]int, err error) {
dataMap = make(map[string]int, len(rows))
if len(titles) == 0 { //全部列
for index, row := range rows[1:] {
key := strings.Join(row, "_")
dataMap[key] = index
}
return
}
compareTitlesMap := make(map[string]int, len(titles))
for k, title := range titles {
compareTitlesMap[title] = k
}
dataTitlesMap := make(map[string]int, len(rows[0]))
for k, title := range rows[0] {
dataTitlesMap[title] = k
}
indexs := make([]int, 0, len(titles)) //对比例所在的顺序位置
for k := range compareTitlesMap {
v, ok := dataTitlesMap[k]
if !ok {
err = fmt.Errorf("不存在比较的列%s", k)
return
}
indexs = append(indexs, v)
}
sort.Ints(indexs)
for index, row := range rows[1:] {
tempRow := make([]string, 0, len(indexs))
for _, v := range indexs { //对比列的数据
tempRow = append(tempRow, row[v])
}
key := strings.Join(tempRow, "_")
dataMap[key] = index
}
return
}
type rowDiff struct {
key string
index int
operation enums.Operation
}
func dataDiff(sourceMap, targetMap map[string]int) []*rowDiff {
r := make([]*rowDiff, 0)
//新增
for name, index := range sourceMap {
if _, ok := targetMap[name]; !ok {
diff := &rowDiff{
key: name,
index: index + 2, //加上标题
operation: enums.DiffInsert,
}
r = append(r, diff)
}
}
return r
}
func getEnd(count int) string {
count = count - 1
if count < 26 {
return string(rune(65 + count))
}
return fmt.Sprintf("%s%s", string(rune(64+count/26)), string(rune(65+count%26)))
}