-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxls2lua.go
163 lines (136 loc) · 3.38 KB
/
xls2lua.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
package main
import (
"fmt"
//"path/filepath"
"strings"
"github.com/Luxurioust/excelize"
"os"
"sync"
)
var (
excelPath string
luaPath string
workResultLock sync.WaitGroup
)
func main() {
//读取配置文件
configExcel, configErr := excelize.OpenFile("config.xlsx")
if configErr != nil {
fmt.Println("read config.xlsx err==========")
return
}
rows := configExcel.GetRows("sheet1")
if len(rows) < 3 {
fmt.Println("config.xlsx格式要求至少要三行数据,否则认为无效")
return
}
excelPath = rows[0][1]
luaPath = rows[1][1]
fmt.Println("excelPath is ", excelPath, " luaPath is ", luaPath)
fmt.Println("begin to work ............")
for k, row := range rows {
fmt.Println("loop =============1 ", k)
if k < 2 {
fmt.Println("loop =============2 ", k)
continue
}
fmt.Println("loop =============3 ", k)
if row[0] == "" || row[1] == "" {
fmt.Println("config break at row ", k)
break
}
fmt.Println("loop =============4 ", k, row[0], row[1])
go writeLuaFile(row[0], row[1])
}
workResultLock.Wait()
fmt.Println("work finished ............")
// 枚举配置目录的所有文件
//filepath.Walk("./", walkCallback)
}
/*func walkCallback(path string, f os.FileInfo, err error) error {
go writeLuaFile(f, err)
return err
}*/
func writeLuaFile(excelFileNnme string, luaFilename string) error{
workResultLock.Add(1)
if excelFileNnme == "" || luaFilename == "" {
fmt.Println("excelFileNnme luaFilename =", excelFileNnme, luaFilename)
return nil
}
fmt.Println("file name is " + excelFileNnme)
var luaStr string = ""
xlsx, openErr := excelize.OpenFile(excelPath + excelFileNnme)
if openErr != nil {
fmt.Println(openErr)
return openErr
}
rows := xlsx.GetRows("sheet1")
if len(rows) < 5 {
fmt.Println("数据表格式要求至少要五行数据,否则认为是空表")
return nil
}
// 写注释 标明每个字段的含义
luaStr += "-- "
for i := 0; i < len(rows[1]); i++ {
if rows[1][i] == "" {
break
}
luaStr += rows[1][i] + ":" + rows[0][i] + ", "
}
luaStr += "\n"
//开始写lua配置表
luaStr += "local " + luaFilename + " = {\n"
for k, row := range rows {
if k < 4 {
continue
}
// 遇到第一个元素为空的行 直接跳出循环
if row[0] == "" {
break
}
luaStr += "\t{"
// 可以考虑给string类型加上 ""
for cellKey, colCell := range row {
if rows[1][cellKey] == "" || colCell == "" {
continue
}
// 判断是否是string类型
cellStr := ""
if (strings.Contains(rows[2][cellKey], "string")) {
if strings.Contains(rows[2][cellKey], "[]") {
strArray := strings.Split(colCell, ",")
for _, tempStr := range strArray {
cellStr += "'" + tempStr + "'" + ","
}
}else {
cellStr = "'" + colCell + "'"
}
}else {
cellStr = colCell
}
if strings.Contains(rows[2][cellKey], "[]") {
luaStr += rows[1][cellKey] + " = {" + cellStr + "}, "
}else {
luaStr += rows[1][cellKey] + " = " + cellStr + ", "
}
}
luaStr += "};\n"
}
luaStr += "}\n"
luaStr += "return " + luaFilename
// 写入到配置的目录中
luaFile, fileErr := os.Create(luaPath + luaFilename + ".lua" )
if fileErr != nil {
fmt.Println(fileErr)
return fileErr
}
defer luaFile.Close()
_, writeErr := luaFile.WriteString(luaStr)
if writeErr != nil {
fmt.Println(writeErr)
return writeErr
}
luaFile.Sync()
workResultLock.Done()
return nil
}