0 Star 1 Fork 0

有点心急 / gotls

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
json.go 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
有点心急 提交于 2024-05-13 10:39 . 24.05.13
package excelT
import (
"fmt"
"strconv"
"gitee.com/Jiudan0905/go-tools/baseT/collection/sliceT"
)
func RowsToJson(rows []*Row) []map[string]string {
// 默认第一列是标题栏
var keyList []string
for _, cell := range rows[0].CellList {
if sliceT.ContainStr(cell.Value, keyList) {
panic("标题栏重复")
} else {
keyList = append(keyList, cell.Value)
}
}
// 默认第二列开始是数据
var dataList []map[string]string
for rowIndex, row := range rows[1:] {
if rowIndex%400 == 0 {
fmt.Println(fmt.Sprintf("ToJson: Row Just : %d", rowIndex))
}
tempMap := make(map[string]string)
tempMap["data.id"] = strconv.Itoa(rowIndex)
for i, cell := range row.CellList {
if i >= len(keyList) {
continue
}
tempMap[keyList[i]] = cell.Value
}
dataList = append(dataList, tempMap)
fmt.Println(tempMap)
}
return dataList
}
func RowsToJsonByKey(rows []*Row, keys ...string) map[string]map[string]string {
// 默认第一列是标题栏
var keyList []string
for _, cell := range rows[0].CellList {
if sliceT.ContainStr(cell.Value, keyList) {
panic(fmt.Sprintf("标题栏重复 %s", cell.Value))
} else {
keyList = append(keyList, cell.Value)
}
}
// 默认第二列开始是数据
dataList := make(map[string]map[string]string)
for rowIndex, row := range rows[1:] {
tempMap := make(map[string]string)
tempMap["data.id"] = strconv.Itoa(rowIndex)
for i, cell := range row.CellList {
if i >= len(keyList) {
continue
}
tempMap[keyList[i]] = cell.Value
}
for _, key := range keys {
if _, ok := tempMap[key]; !ok {
panic("Key不存在")
}
dataList[tempMap[key]] = tempMap
}
}
return dataList
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ydxj/gotls.git
git@gitee.com:ydxj/gotls.git
ydxj
gotls
gotls
v0.0.7

搜索帮助

344bd9b3 5694891 D2dac590 5694891