From 73f4af74f98308251bf449678849d64f3dd77e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 19 Nov 2021 17:16:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8A=BD=E5=8F=96?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E5=88=97=E7=9A=84=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- excel/read.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/excel/read.go b/excel/read.go index 7c4a462..f49f408 100644 --- a/excel/read.go +++ b/excel/read.go @@ -100,3 +100,45 @@ 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) { + return nil, nil +} -- 2.36.6 From 802c7959144c2c74f0c446e80c87e7164435d029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 19 Nov 2021 17:29:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8F=90=E5=8F=96?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E5=88=97=E6=95=B0=E6=8D=AE,=E5=B9=B6?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E6=88=90map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- excel/read.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/excel/read.go b/excel/read.go index f49f408..d12094b 100644 --- a/excel/read.go +++ b/excel/read.go @@ -140,5 +140,35 @@ func (r *Read) ExtractAssignCol(colIndexList []int) (map[string][][]string, erro // // Date : 4:53 下午 2021/11/19 func (r *Read) ExtractAssignColToMap(ruleTable map[int]string) (map[string][]map[string]string, error) { - return nil, nil + 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 } -- 2.36.6