diff --git a/excel/read.go b/excel/read.go index 7c4a462..d12094b 100644 --- a/excel/read.go +++ b/excel/read.go @@ -100,3 +100,75 @@ func (r *Read) GetAllDataToMap(fieldList []string) (map[string][]map[string]stri // 格式化数据 return formatResult, nil } + +// ExtractAssignCol 抽取指定的列,并返回list数据, 列的计数从 0 开始 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 4:53 下午 2021/11/19 +func (r *Read) ExtractAssignCol(colIndexList []int) (map[string][][]string, error) { + var ( + allData map[string][][]string + err error + formatResult map[string][][]string + ) + if allData, err = r.GetAllData(); nil != err { + return nil, err + } + formatResult = make(map[string][][]string) + for sheetName, sheetData := range allData { + formatResult[sheetName] = make([][]string, 0) + for _, lineData := range sheetData { + tmpData := make([]string, 0) + for _, colIdx := range colIndexList { + if len(lineData) <= colIdx { + // 索引越界,默认空值 + tmpData = append(tmpData, "") + continue + } + tmpData = append(tmpData, lineData[colIdx]) + } + formatResult[sheetName] = append(formatResult[sheetName], tmpData) + } + } + return formatResult, nil +} + +// ExtractAssignColToMap 抽取指定的列,并返回map数据 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 4:53 下午 2021/11/19 +func (r *Read) ExtractAssignColToMap(ruleTable map[int]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 { + colName, exist := ruleTable[idx] + if !exist { + // 当前字段不需要提取 + continue + } + tmpResult[colName] = colData + } + for _, colName := range ruleTable { + if _, exist := tmpResult[colName]; !exist { + tmpResult[colName] = "" + } + } + formatResult[sheetName] = append(formatResult[sheetName], tmpResult) + } + } + // 格式化数据 + return formatResult, nil +}