From 1aba1e78cd7ce3d4fc5c5f4003e1f10ea49a92d2 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 18 Jan 2022 13:05:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=9D=9E=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- importer/from/source.go | 6 ++---- importer/importer.go | 15 +++++++-------- importer/types.go | 1 - importer/xlsx/xlsx.go | 16 +++++++--------- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/importer/from/source.go b/importer/from/source.go index b5a56e76..85afee8c 100644 --- a/importer/from/source.go +++ b/importer/from/source.go @@ -17,9 +17,9 @@ const ( // Source 导入文件接口 type Source interface { - Data(row int, size int, cols []int) [][]interface{} + Data(row int, size int, axises []string) [][]interface{} Columns() []Column - Chunk(size int, cols []int, cb func(line int, data [][]interface{})) + Chunk(size int, axises []string, cb func(line int, data [][]interface{})) Inspect() Inspect Close() error } @@ -28,8 +28,6 @@ type Source interface { type Column struct { Name string Type byte - Col int - Row int Axis string } diff --git a/importer/importer.go b/importer/importer.go index 559690c3..c0678271 100644 --- a/importer/importer.go +++ b/importer/importer.go @@ -87,7 +87,7 @@ func (imp *Importer) AutoMapping(src from.Source) *Mapping { if !ok { continue } - binding := Binding{Name: "", Axis: "", Col: -1, Rules: []string{}, Field: name, Label: imp.Columns[i].Label} + binding := Binding{Name: "", Axis: "", Rules: []string{}, Field: name, Label: imp.Columns[i].Label} for _, suggest := range imp.Columns[i].Match { if srcCol, has := sourceColumns[suggest]; has { binding = Binding{ @@ -95,7 +95,6 @@ func (imp *Importer) AutoMapping(src from.Source) *Mapping { Field: name, Name: srcCol.Name, Axis: srcCol.Axis, - Col: srcCol.Col, Rules: imp.Columns[i].Rules, } continue @@ -114,21 +113,21 @@ func (imp *Importer) DataGet(src from.Source, page int, size int, mapping *Mappi if row < 0 { row = mapping.RowStart } - cols := []int{} + axises := []string{} for _, d := range mapping.Columns { - cols = append(cols, d.Col) + axises = append(axises, d.Axis) } - data := src.Data(row, size, cols) + data := src.Data(row, size, axises) return imp.DataClean(data, mapping.Columns) } // Chunk 遍历数据 func (imp *Importer) Chunk(src from.Source, mapping *Mapping, cb func(line int, data [][]interface{})) { - cols := []int{} + axises := []string{} for _, d := range mapping.Columns { - cols = append(cols, d.Col) + axises = append(axises, d.Axis) } - src.Chunk(imp.Option.ChunkSize, cols, cb) + src.Chunk(imp.Option.ChunkSize, axises, cb) } // DataClean 清洗数据 diff --git a/importer/types.go b/importer/types.go index 434aa1a1..250e49aa 100644 --- a/importer/types.go +++ b/importer/types.go @@ -57,6 +57,5 @@ type Binding struct { Field string `json:"field"` // 目标字段名称 Name string `json:"name"` // 源关联字段名称 Axis string `json:"axis"` // 源关联字段坐标 - Col int `json:"col"` // 源关联字段列号 Rules []string `json:"rules"` // 清洗规则 } diff --git a/importer/xlsx/xlsx.go b/importer/xlsx/xlsx.go index dfe0db2c..ee8c8468 100644 --- a/importer/xlsx/xlsx.go +++ b/importer/xlsx/xlsx.go @@ -71,10 +71,10 @@ func (xlsx *Xlsx) Inspect() from.Inspect { } // Data 读取数据 -func (xlsx *Xlsx) Data(row int, size int, cols []int) [][]interface{} { +func (xlsx *Xlsx) Data(row int, size int, axises []string) [][]interface{} { data := [][]interface{}{} for line := row; line < row+size; line++ { - row, end := xlsx.readLine(line, cols) + row, end := xlsx.readLine(line, axises) if end { break } @@ -84,7 +84,7 @@ func (xlsx *Xlsx) Data(row int, size int, cols []int) [][]interface{} { } // Chunk 遍历数据 -func (xlsx *Xlsx) Chunk(size int, cols []int, cb func(line int, data [][]interface{})) { +func (xlsx *Xlsx) Chunk(size int, axises []string, cb func(line int, data [][]interface{})) { line := 0 data := [][]interface{}{} for xlsx.Rows.Next() { @@ -92,7 +92,7 @@ func (xlsx *Xlsx) Chunk(size int, cols []int, cb func(line int, data [][]interfa if line < xlsx.RowStart { continue } - row, end := xlsx.readLine(line, cols) + row, end := xlsx.readLine(line, axises) if end { cb(line, data) break @@ -112,12 +112,12 @@ func (xlsx *Xlsx) Chunk(size int, cols []int, cb func(line int, data [][]interfa } // readLine 读取给定行信息 -func (xlsx *Xlsx) readLine(line int, cols []int) ([]interface{}, bool) { +func (xlsx *Xlsx) readLine(line int, axises []string) ([]interface{}, bool) { row := []interface{}{} end := true - for _, c := range cols { + for _, axis := range axises { + _, c, err := axisToPosition(axis) var value = "" - var err error if c >= 0 { axis := positionToAxis(line, c) value, err = xlsx.File.GetCellValue(xlsx.SheetName, axis) @@ -164,8 +164,6 @@ func (xlsx *Xlsx) Columns() []from.Column { } columns = append(columns, from.Column{ Name: cell, - Col: i, - Row: line, Axis: axis, Type: byte(cellType), })