// Code generated by xgo (XGo); DO NOT EDIT. package main import ( "encoding/json" "fmt" "github.com/go-yaml/yaml" "github.com/qiniu/x/errors" "github.com/qiniu/x/stringutil" "github.com/xuri/excelize/v2" "os" "strconv" ) const _ = true type Config struct { FileName string `yaml:"inputFile"` JsonFile string `yaml:"outputFile"` SheetName string `yaml:"sheet"` StartRow int `yaml:"startRow"` Columns map[string]string `yaml:"columns"` } var VERSION = "0.0.2" //line tsa.xgo:22:1 // Чтение конфига func readConfig(filename string) (Config, error) { //line tsa.xgo:24:1 var _autoGo_1 *os.File //line tsa.xgo:24:1 { //line tsa.xgo:24:1 var _xgo_err error //line tsa.xgo:24:1 _autoGo_1, _xgo_err = os.Open(filename) //line tsa.xgo:24:1 if _xgo_err != nil { //line tsa.xgo:24:1 _xgo_err = errors.NewFrame(_xgo_err, "os.Open(filename)", "tsa.xgo", 24, "main.readConfig") //line tsa.xgo:24:1 return struct { FileName string `yaml:"inputFile"` JsonFile string `yaml:"outputFile"` SheetName string `yaml:"sheet"` StartRow int `yaml:"startRow"` Columns map[string]string `yaml:"columns"` }{}, _xgo_err } //line tsa.xgo:24:1 goto _autoGo_2 _autoGo_2: //line tsa.xgo:24:1 } //line tsa.xgo:24:1 f := _autoGo_1 //line tsa.xgo:25:1 defer f.Close() //line tsa.xgo:27:1 cfg := Config{} //line tsa.xgo:28:1 decoder := yaml.NewDecoder(f) //line tsa.xgo:29:1 { //line tsa.xgo:29:1 var _xgo_err error //line tsa.xgo:29:1 _xgo_err = decoder.Decode(&cfg) //line tsa.xgo:29:1 if _xgo_err != nil { //line tsa.xgo:29:1 _xgo_err = errors.NewFrame(_xgo_err, "decoder.Decode(&cfg)", "tsa.xgo", 29, "main.readConfig") //line tsa.xgo:29:1 return struct { FileName string `yaml:"inputFile"` JsonFile string `yaml:"outputFile"` SheetName string `yaml:"sheet"` StartRow int `yaml:"startRow"` Columns map[string]string `yaml:"columns"` }{}, _xgo_err } //line tsa.xgo:29:1 goto _autoGo_3 _autoGo_3: //line tsa.xgo:29:1 } //line tsa.xgo:30:1 return cfg, nil } //line tsa.xgo:33:1 // Получаем все строки листа func readExcel(filename string, sheetname string) [][]string { //line tsa.xgo:35:1 f := func() (_xgo_ret *excelize.File) { //line tsa.xgo:35:1 var _xgo_err error //line tsa.xgo:35:1 _xgo_ret, _xgo_err = excelize.OpenFile(filename) //line tsa.xgo:35:1 if _xgo_err != nil { //line tsa.xgo:35:1 _xgo_err = errors.NewFrame(_xgo_err, "excelize.OpenFile(filename)", "tsa.xgo", 35, "main.readExcel") //line tsa.xgo:35:1 panic(_xgo_err) } //line tsa.xgo:35:1 return }() //line tsa.xgo:36:1 defer f.Close() //line tsa.xgo:38:1 rows := func() (_xgo_ret [][]string) { //line tsa.xgo:38:1 var _xgo_err error //line tsa.xgo:38:1 _xgo_ret, _xgo_err = f.GetRows(sheetname) //line tsa.xgo:38:1 if _xgo_err != nil { //line tsa.xgo:38:1 _xgo_err = errors.NewFrame(_xgo_err, "f.GetRows(sheetname)", "tsa.xgo", 38, "main.readExcel") //line tsa.xgo:38:1 panic(_xgo_err) } //line tsa.xgo:38:1 return }() //line tsa.xgo:39:1 return rows } //line tsa.xgo:42:1 func getColumnName(cols map[string]string, col int) string { //line tsa.xgo:43:1 if col < 1 || col > 26 { //line tsa.xgo:44:1 fmt.Println(stringutil.Concat(`Столбец: `, strconv.Itoa(col), ` вне диапазона столбцов: A-Z`)) //line tsa.xgo:45:1 os.Exit(0) } //line tsa.xgo:48:1 type ColsMap struct { num int str string } //line tsa.xgo:52:1 colsmap := []string{" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} //line tsa.xgo:56:1 return cols[colsmap[col]] } //line tsa.xgo:59:1 func convert2int(s string) int64 { //line tsa.xgo:60:1 f := func() (_xgo_ret float64) { //line tsa.xgo:60:1 var _xgo_err error //line tsa.xgo:60:1 _xgo_ret, _xgo_err = strconv.ParseFloat(s, 64) //line tsa.xgo:60:1 if _xgo_err != nil { //line tsa.xgo:60:1 _xgo_err = errors.NewFrame(_xgo_err, "strconv.ParseFloat(s, 64)", "tsa.xgo", 60, "main.convert2int") //line tsa.xgo:60:1 panic(_xgo_err) } //line tsa.xgo:60:1 return }() //line tsa.xgo:61:1 n := int64(f) //line tsa.xgo:62:1 return n } //line tsa.xgo:65:1 // выводит результат на экран func json2screen(dbValues []map[string]string) { //line tsa.xgo:70:1 pretty := func() (_xgo_ret []byte) { //line tsa.xgo:70:1 var _xgo_err error //line tsa.xgo:70:1 _xgo_ret, _xgo_err = json.MarshalIndent(dbValues, "", " ") //line tsa.xgo:70:1 if _xgo_err != nil { //line tsa.xgo:70:1 _xgo_err = errors.NewFrame(_xgo_err, "json.MarshalIndent(dbValues, \"\", \" \")", "tsa.xgo", 70, "main.json2screen") //line tsa.xgo:70:1 panic(_xgo_err) } //line tsa.xgo:70:1 return }() //line tsa.xgo:72:1 fmt.Println(string(pretty)) } //line tsa.xgo:75:1 func json2file(dbValues []map[string]string, outfile string) { //line tsa.xgo:76:1 f := func() (_xgo_ret *os.File) { //line tsa.xgo:76:1 var _xgo_err error //line tsa.xgo:76:1 _xgo_ret, _xgo_err = os.OpenFile(outfile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644) //line tsa.xgo:76:1 if _xgo_err != nil { //line tsa.xgo:76:1 _xgo_err = errors.NewFrame(_xgo_err, "os.OpenFile(outfile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)", "tsa.xgo", 76, "main.json2file") //line tsa.xgo:76:1 panic(_xgo_err) } //line tsa.xgo:76:1 return }() //line tsa.xgo:77:1 defer f.Close() //line tsa.xgo:79:1 enc := json.NewEncoder(f) //line tsa.xgo:80:1 enc.SetIndent("", " ") //line tsa.xgo:83:1 func() { //line tsa.xgo:83:1 var _xgo_err error //line tsa.xgo:83:1 _xgo_err = enc.Encode(dbValues) //line tsa.xgo:83:1 if _xgo_err != nil { //line tsa.xgo:83:1 _xgo_err = errors.NewFrame(_xgo_err, "enc.Encode(dbValues)", "tsa.xgo", 83, "main.json2file") //line tsa.xgo:83:1 panic(_xgo_err) } //line tsa.xgo:83:1 return }() //line tsa.xgo:84:1 fmt.Println(stringutil.Concat(`Файл `, outfile, ` сформирован`)) } //line tsa.xgo:87 // ====================== основная программа ==================== func main() { //line tsa.xgo:88:1 if len(os.Args) < 2 { //line tsa.xgo:89:1 panic("Используй: tsa.exe ") } //line tsa.xgo:91:1 cfg := func() (_xgo_ret Config) { //line tsa.xgo:91:1 var _xgo_err error //line tsa.xgo:91:1 _xgo_ret, _xgo_err = readConfig(os.Args[1]) //line tsa.xgo:91:1 if _xgo_err != nil { //line tsa.xgo:91:1 _xgo_err = errors.NewFrame(_xgo_err, "readConfig(os.Args[1])", "tsa.xgo", 91, "main.main") //line tsa.xgo:91:1 panic(_xgo_err) } //line tsa.xgo:91:1 return }() //line tsa.xgo:92:1 rows := readExcel(cfg.FileName, cfg.SheetName) //line tsa.xgo:93:1 fmt.Println(stringutil.Concat("Программа конвертации excel -> json. Версия ", VERSION)) //line tsa.xgo:94:1 fmt.Println(stringutil.Concat("Файл ", cfg.FileName, " содержит записей: ", strconv.Itoa(len(rows)))) //line tsa.xgo:96:1 var dbValues = []map[string]string{} for //line tsa.xgo:97:1 _, row := range rows[cfg.StartRow-1:] { //line tsa.xgo:98:1 rowValues := make(map[string]string) for //line tsa.xgo:99:1 col, val := range row { //line tsa.xgo:100:1 colname := getColumnName(cfg.Columns, col+1) //line tsa.xgo:103:1 if colname == "caseId" || colname == "policyNumber" { //line tsa.xgo:104:1 val = fmt.Sprintf("%d", convert2int(val)) } //line tsa.xgo:108:1 if colname != "" { //line tsa.xgo:109:1 rowValues[colname] = val } } //line tsa.xgo:114:1 dbValues = append(dbValues, rowValues) } //line tsa.xgo:119:1 json2file(dbValues, cfg.JsonFile) }