数据读取: 支持将单行数据转换成map结构
This commit is contained in:
parent
0701313fa7
commit
5e136c1749
@ -63,3 +63,40 @@ func (r *Read) GetAllData() (map[string][][]string, error) {
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetAllDataToMap 读取全部数据,并返回map结构
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:20 下午 2021/11/19
|
||||
func (r *Read) GetAllDataToMap(fieldList []string) (map[string][]map[string]string, error) {
|
||||
var (
|
||||
allData map[string][][]string
|
||||
err error
|
||||
formatResult map[string][]map[string]string
|
||||
)
|
||||
formatResult = make(map[string][]map[string]string)
|
||||
if allData, err = r.GetAllData(); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
for sheetName, sheetData := range allData {
|
||||
formatResult[sheetName] = make([]map[string]string, 0)
|
||||
for _, lineData := range sheetData {
|
||||
tmpResult := make(map[string]string)
|
||||
for idx, colData := range lineData {
|
||||
if idx >= len(fieldList) {
|
||||
// 指定的字段列表较短, 自动剔除后面的字段
|
||||
break
|
||||
}
|
||||
tmpResult[fieldList[idx]] = colData
|
||||
}
|
||||
// 字段列表较长, 单元格不足, 自动补齐空字符串
|
||||
for i := len(lineData); i < len(fieldList); i++ {
|
||||
tmpResult[fieldList[i]] = ""
|
||||
}
|
||||
formatResult[sheetName] = append(formatResult[sheetName], tmpResult)
|
||||
}
|
||||
}
|
||||
// 格式化数据
|
||||
return formatResult, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user