导表工具增加json格式

This commit is contained in:
ayflying
2025-03-04 14:10:32 +08:00
parent 74a746bc47
commit c281b4a30d

View File

@@ -3,6 +3,7 @@ package excel
import (
"context"
"github.com/ayflying/excel2json"
"github.com/goccy/go-json"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
@@ -27,6 +28,7 @@ type FileItem struct {
Items []string `json:"items" dc:"道具字段"`
ItemsMap []string `json:"items_map" dc:"道具字段map格式"`
Slice map[string]string `json:"slice" dc:"切片"`
Json []string `json:"json" dc:"json"`
}
type Excel struct {
@@ -78,6 +80,10 @@ func (s *Excel) ExcelLoad(ctx context.Context, fileItem *FileItem, mainPath stri
if len(fileItem.Slice) > 0 {
list = s.sliceFormat(list, fileItem.Slice)
}
//json格式转换
if len(fileItem.Json) > 0 {
list = s.jsonFormat(list, fileItem.Json)
}
//拼接json
tempJson = append(tempJson, list...)
@@ -177,3 +183,20 @@ func (s *Excel) sliceFormat(list []interface{}, Slice map[string]string) []inter
return list
}
func (s *Excel) jsonFormat(list []interface{}, Items []string) []interface{} {
for k2, v2 := range list {
for k3, v3 := range v2.(g.Map) {
if gstr.InArray(Items, k3) {
if _, ok := v3.(string); ok {
var get interface{}
json.Unmarshal([]byte(v3.(string)), &get)
list[k2].(g.Map)[k3] = get
} else {
g.Log().Errorf(gctx.New(), "当前类型断言失败:%v,list=%v", v3, v2)
}
}
}
}
return list
}