rts-sim-testing-service/example/balise_1023_830/read_excel.go

157 lines
3.9 KiB
Go
Raw Normal View History

package main
import (
"log/slog"
"runtime/debug"
"strconv"
"strings"
"github.com/xuri/excelize/v2"
)
const (
fileName = "北岗子-应答器报文清单.xlsx"
sheetName = "应答器报文清单"
codeColumn = "应答器编号"
nameColumn = "应答器名称"
typeColumn = "类型"
checkSumColumn = "校验码"
msg830strColumn = "用户报文830bits"
msg1023strColumn = "报文1023bits"
)
var heads []string
func init() {
initHeads()
}
func initHeads() {
heads = append(heads, codeColumn)
heads = append(heads, nameColumn)
heads = append(heads, typeColumn)
heads = append(heads, checkSumColumn)
heads = append(heads, msg830strColumn)
heads = append(heads, msg1023strColumn)
}
func buildHeadIndex(row []string) map[string]int {
headIdx := make(map[string]int)
for i, column := range row {
column = HandleStringSpace(column)
if column == codeColumn {
headIdx[column] = i
} else if column == nameColumn {
headIdx[column] = i
} else if column == typeColumn {
headIdx[column] = i
} else if column == checkSumColumn {
headIdx[column] = i
} else if column == msg830strColumn {
headIdx[column] = i
} else if column == msg1023strColumn {
headIdx[column] = i
}
}
// 检查headIndex是否完整
if len(headIdx) <= 0 {
return nil
}
checkHeadsIndex(headIdx, fileName, sheetName, heads)
return headIdx
}
func checkHeadsIndex(headIdx map[string]int, fileName, sheetName string, heads []string) {
// 检查headIndex是否完整
for _, v := range heads {
if _, ok := headIdx[v]; !ok {
slog.Error("表头缺失", "文件名", fileName, "SheetName", sheetName, "表头", v)
panic("课时表头缺失")
}
}
}
func ReadTransponders() map[string]Transponder {
return readExcel(fileName, sheetName, readRows)
}
func readExcel[T any](fileName, sheetName string, handle func(rows [][]string) map[string]T) map[string]T {
f, err := excelize.OpenFile(fileName)
if err != nil {
slog.Error("打开表文件异常", "表名", fileName, "error", err)
debug.PrintStack()
panic("打开表文件异常")
}
defer func() {
if err := f.Close(); err != nil {
slog.Error("文件关闭异常", "error", err)
}
}()
rows, err := f.GetRows(sheetName)
if err != nil {
slog.Error("读取Sheet异常", "SheetName", sheetName, "error", err)
panic(err)
}
// fmt.Println(rows)
return handle(rows)
}
func readRows(rows [][]string) map[string]Transponder {
dataMap := make(map[string]Transponder)
var headIdx map[string]int
for _, row := range rows {
if headIdx == nil {
headIdx = buildHeadIndex(row)
// if headIdx != nil {
// slog.Info("读取到表头索引", "文件名", fileName, "表名", sheetName, "索引", headIdx)
// }
} else {
rowSize := len(row)
if rowSize <= 0 {
continue
}
if rowSize <= headIdx[msg1023strColumn] {
// slog.Info("非数据行", "row", row, "rowIndex", i)
continue
}
codeStr := row[headIdx[codeColumn]]
if codeStr == "" {
continue
}
codeStr = HandleStringSpace(codeStr)
code, err := strconv.ParseInt(codeStr, 10, 32)
if err != nil {
slog.Error("应答器编号错误", "编号", codeStr, "error", err)
panic("应答器编号错误")
}
name := row[headIdx[nameColumn]]
if name == "" {
continue
}
name = HandleStringSpace(name)
tp := row[headIdx[typeColumn]]
checkSum := row[headIdx[checkSumColumn]]
msg830 := row[headIdx[msg830strColumn]]
msg1023 := row[headIdx[msg1023strColumn]]
dataMap[name] = Transponder{
Code: int(code),
Name: name,
Type: tp,
CheckSum: checkSum,
Msg830: msg830,
Msg1023: msg1023,
}
}
}
slog.Info("读取结果", "文件名", fileName, "SheetName", sheetName, "总数", len(dataMap))
// fmt.Println(dataMap)
return dataMap
}
func HandleStringSpace(s string) string {
s = strings.ReplaceAll(s, " ", "")
s = strings.ReplaceAll(s, "\n", "")
return s
}