feat(excelutils): support read file data (#19795)

Co-authored-by: 马鸿飞 <mahongfei@yunion.cn>
This commit is contained in:
gouqi11
2024-03-26 14:53:56 +08:00
committed by GitHub
co-authored by 马鸿飞
parent d690f923ed
commit 9e10ad4e21
+25
View File
@@ -269,3 +269,28 @@ func SetCellStyleWithColumnKey(keys []string, sheet, style string, f *excelize.F
}
return f, nil
}
// 按行读取excel文件,注:第一行为json的key
func ReadDataWithRow(f *excelize.File, sheet string) (jsonutils.JSONObject, error) {
datas := jsonutils.NewArray()
keys := []string{}
rows, err := f.GetRows(sheet)
if err != nil {
return nil, errors.Wrap(err, "get rows")
}
for index, row := range rows {
data := jsonutils.NewDict()
if index == 0 {
keys = row
continue
}
for i, value := range row {
if i >= len(keys) {
continue
}
data.Set(keys[i], jsonutils.NewString(value))
}
datas.Add(data)
}
return jsonutils.Marshal(datas), nil
}