提交 5d5e0179 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

export data to excel

上级 8652aa9a
......@@ -2,7 +2,7 @@ package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/360EntSecGroup-Skylar/excelize/v1"
"github.com/easysoft/zendata/cmd/test/comm"
"github.com/easysoft/zendata/cmd/test/model"
fileUtils "github.com/easysoft/zendata/pkg/utils/file"
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/name/cn.family.v2.xlsx"
filePath := "data/name/cn.family.v1.xlsx"
sheetName := "chinese_family"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/name/cn.given.v2.xlsx"
filePath := "data/name/cn.given.v1.xlsx"
sheetName := "chinese_given"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/chronology/v2.xlsx"
filePath := "data/chronology/v1.xlsx"
sheetName := "chronology"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/city/v2.xlsx"
filePath := "data/city/v1.xlsx"
sheetName := "city"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/color/v2.xlsx"
filePath := "data/color/v1.xlsx"
sheetName := "color"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/company/v2.xlsx"
filePath := "data/company/v1.xlsx"
sheetName1 := "company"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/easysoft/zendata/cmd/test/comm"
"github.com/easysoft/zendata/cmd/test/model"
fileUtils "github.com/easysoft/zendata/pkg/utils/file"
"path/filepath"
"reflect"
)
func main() {
filePath := "data/company_abbreviation/v2.xlsx"
sheetName := "company_abbreviation"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
db := comm.GetDB()
db.AutoMigrate(
&model.DataCompanyAbbreviation{},
)
pos := make([]model.DataCompanyAbbreviation, 0)
db.Order("id ASC").Find(&pos)
f := excelize.NewFile()
index := f.NewSheet(sheetName)
f.SetActiveSheet(index)
sheet1 := f.GetSheetName(0)
f.DeleteSheet(sheet1)
var infos []model.TableInfo
db.Raw("desc " + model.DataCompanyAbbreviation{}.TableName()).Scan(&infos)
excelColNameArr, excelColNameHeader := comm.GetExcelColsByTableDef(infos)
fieldNames := comm.GetStructFields(model.DataCompanyAbbreviation{})
// gen headers
for index, name := range excelColNameHeader {
excelColName := excelColNameArr[index]
excelColId := fmt.Sprintf("%s%d", excelColName, 1)
f.SetCellValue(sheetName, excelColId, name)
}
// gen rows
for rowIndex, po := range pos {
for fieldIndex, fieldName := range fieldNames {
val := ""
if fieldName == "Id" {
val = fmt.Sprintf("%d", reflect.ValueOf(po).FieldByName(fieldName).Uint())
} else {
val = reflect.ValueOf(po).FieldByName(fieldName).String()
}
excelColName := excelColNameArr[fieldIndex]
excelColId := fmt.Sprintf("%s%d", excelColName, rowIndex+2)
f.SetCellValue(sheetName, excelColId, val)
}
}
f.SaveAs(filePath)
}
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/country/v2.xlsx"
filePath := "data/country/v1.xlsx"
sheetName := "country"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/earthly_branches/v2.xlsx"
filePath := "data/earthly_branches/v1.xlsx"
sheetName := "earthly_branches"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/name/en.family.v2.xlsx"
filePath := "data/name/en.family.v1.xlsx"
sheetName := "english_family"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/name/en.given.v2.xlsx"
filePath := "data/name/en.given.v1.xlsx"
sheetName := "english_given"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/five_elements/v2.xlsx"
filePath := "data/five_elements/v1.xlsx"
sheetName := "five_elements"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/heavenly_stems/v2.xlsx"
filePath := "data/heavenly_stems/v1.xlsx"
sheetName := "heavenly_stems"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/idiom/v2.xlsx"
filePath := "data/idiom/v1.xlsx"
sheetName := "idiom"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/occupation/v2.xlsx"
filePath := "data/occupation/v1.xlsx"
sheetName := "occupation"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/planet/v2.xlsx"
filePath := "data/planet/v1.xlsx"
sheetName := "planet"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/season/v2.xlsx"
filePath := "data/season/v1.xlsx"
sheetName := "season"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -23,7 +23,7 @@ func main() {
// gen sheet by tag group
for _, group := range groups {
sheetName := group.Name
filePath := fmt.Sprintf("data/words/v2/%s.xlsx", sheetName)
filePath := fmt.Sprintf("data/words/v1/%s.xlsx", sheetName)
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -11,7 +11,7 @@ import (
)
func main() {
filePath := "data/xiehouyu/v2.xlsx"
filePath := "data/xiehouyu/v1.xlsx"
sheetName := "xiehouyu"
fileUtils.MkDirIfNeeded(filepath.Dir(filePath))
......
......@@ -55,7 +55,7 @@ fields:
postfix: "\t"
- field: field_use_ranges_medium
from: zentao.number.v1.yaml
from: zentao.number.v1.yaml
use: medium{4} # 使用该文件中定义的medium分组。
postfix: "\t"
......
......@@ -4,7 +4,7 @@ fields:
froms:
- from: ip.v1.yaml
use: privateB{10}
- from: ip.v2.yaml
- from: ip.v1.yaml
use: ipA{10}
......@@ -19,7 +19,7 @@ fields:
postfix: "\t"
- field: field_use_excel # 从excel数据源里面取数据。
from: city.cn.v2.city # 从data/address/v1.xlsx文件中读取名为china的工作簿。
from: city.cn.v1.city # 从data/address/v1.xlsx文件中读取名为china的工作簿。
select: content # 查询city字段。
where: state like '%山东%' # 条件是省份包含山东。
rand: true # 随机取数据
......
fields:
- field: f1
from: uuid.v2.yaml
from: uuid.v1.yaml
use: by_format
postfix: "\t"
......
......@@ -6,12 +6,12 @@ version: 1.0
fields:
- field: f1
from: chronology.v2.yaml
from: chronology.v1.yaml
use: chronology
postfix: ", "
postfix: "\t"
- field: f2
from: city.v2.yaml
from: city.v1.yaml
use: name
postfix: "\t"
......@@ -21,6 +21,57 @@ fields:
postfix: "\t"
- field: f4
from: company.v2.yaml
from: company.v1.yaml
use: name
postfix: "\t"
- field: f5
from: company.v1.yaml
use: abbreviation
postfix: "\t"
- field: f6
from: country.v1.yaml
use: cnfull
postfix: "\t"
- field: f7
from: earthly_branches.v1.yaml
use: earthly_branches
postfix: "\t"
- field: f8
from: five_elements.v1.yaml
use: five_elements
postfix: "\t"
- field: f9
from: heavenly_stems.v1.yaml
use: heavenly_stems
postfix: "\t"
- field: f10
from: idiom.v1.yaml
use: idiom
postfix: "\t"
- field: f11
from: occupation.v1.yaml
use: occupation
postfix: "\t"
- field: f12
from: planet.v1.yaml
use: planet
postfix: "\t"
- field: f13
from: season.v1.yaml
use: season
postfix: "\t"
- field: f14
from: xiehouyu.v1.yaml
use: xiehouyu
postfix: "\t"
......@@ -123,7 +123,7 @@ fields:
postfix: "\t"
- field: field_use_excel # 从excel数据源里面取数据。
from: city.v2.city # 从data/city/v2.xlsx文件中读取名为city的工作簿。
from: city.v1.city # 从data/city/v2.xlsx文件中读取名为city的工作簿。
select: name # 查询city字段。
where: state like '%山东%' # 条件是省份包含山东。
rand: true # 随机取数据
......
......@@ -6,7 +6,7 @@ version: 1.0
fields:
- field: f1
from: uuid.v2.yaml
from: uuid.v1.yaml
use: by_format
postfix: "\t"
......
......@@ -132,6 +132,7 @@ func ConvertSingleExcelToSQLiteIfNeeded(dbName string, path string) (firstSheet
}
valList = valList + "("
dataColCount := 0
for colIndex, colCell := range row {
if colIndex >= colCount {
break
......@@ -142,7 +143,15 @@ func ConvertSingleExcelToSQLiteIfNeeded(dbName string, path string) (firstSheet
}
colCell = strings.Replace(colCell, "'", "''", -1)
valList = valList + "'" + colCell + "'"
dataColCount++
}
for dataColCount < colCount {
valList = valList + ", ''"
dataColCount++
}
valList = valList + ")"
}
......
......@@ -219,7 +219,7 @@ func GenerateForFieldRecursive(field *model.DefField, withFix bool) (values []st
} else if field.From != "" && field.Type != constant.FieldTypeArticle { // refer to res
if field.Use != "" { // refer to ranges or instance
groupValues := vari.Res[field.From]
groupValues := vari.Res[getFromKey(field)]
uses := strings.TrimSpace(field.Use) // like group{limit:repeat}
use, numLimit, repeat := getNum(uses)
......@@ -232,7 +232,7 @@ func GenerateForFieldRecursive(field *model.DefField, withFix bool) (values []st
values = append(values, valuesForAdd...)
}
} else if field.Select != "" { // refer to excel
groupValues := vari.Res[field.From]
groupValues := vari.Res[getFromKey(field)]
resKey := field.Select
// deal with the key
......@@ -727,3 +727,7 @@ exit:
}
return
}
func getFromKey(field *model.DefField) string {
return fmt.Sprintf("%s-%s-%s", field.From, field.Use, field.Select)
}
......@@ -55,8 +55,8 @@ func loadResForFieldRecursive(field *model.DefField, res *map[string]map[string]
resFile, resType, sheet := fileUtils.GetResProp(field.From, field.FileDir) // relate to current file
valueMap, _ = getResValue(resFile, resType, sheet, field)
if (*res)[field.From] == nil {
(*res)[field.From] = map[string][]string{}
if (*res)[getFromKey(field)] == nil {
(*res)[getFromKey(field)] = map[string][]string{}
}
for key, val := range valueMap {
resKey := key
......@@ -64,7 +64,7 @@ func loadResForFieldRecursive(field *model.DefField, res *map[string]map[string]
if vari.Def.Type == constant.ConfigTypeArticle {
resKey = resKey + "_" + field.Field
}
(*res)[field.From][resKey] = val
(*res)[getFromKey(field)][resKey] = val
}
} else if field.Config != "" { // from a config
......@@ -177,7 +177,7 @@ func prepareNestedInstanceRes(insts model.ResInstances, inst model.ResInstancesI
instField.FileDir = insts.FileDir
if instField.Use != "" { // refer to another instances or ranges
if vari.Res[instField.From] == nil {
if vari.Res[getFromKey(&instField)] == nil {
referencedRanges, referencedInstants := getReferencedRangeOrInstant(instField)
groupedValueReferenced := map[string][]string{}
......@@ -198,12 +198,12 @@ func prepareNestedInstanceRes(insts model.ResInstances, inst model.ResInstancesI
}
}
vari.Res[instField.From] = groupedValueReferenced
vari.Res[getFromKey(&instField)] = groupedValueReferenced
}
} else if instField.Select != "" { // refer to excel
resFile, resType, sheet := fileUtils.GetResProp(instField.From, instField.FileDir)
values, _ := getResValue(resFile, resType, sheet, &instField)
vari.Res[instField.From] = values
vari.Res[getFromKey(&instField)] = values
}
}
......
无法预览此类型文件
......@@ -9,5 +9,5 @@ instances:
note:
fields:
- field: content
from: chronology.v2.chronology
from: chronology.v1.chronology
select: content
......@@ -8,35 +8,35 @@ instances:
- instance: name
fields:
- field: part1
from: city.v2.city
from: city.v1.city
select: name
- instance: code
fields:
- field: part1
from: city.v2.city
from: city.v1.city
select: code
- instance: zip_code
fields:
- field: part1
from: city.v2.city
from: city.v1.city
select: name
- instance: state
fields:
- field: part1
from: city.v2.city
from: city.v1.city
select: zip_code
- instance: state_short
fields:
- field: part1
from: city.v2.city
from: city.v1.city
select: state_short
- instance: state_short2
fields:
- field: part1
from: city.v2.city
from: city.v1.city
select: state_short2
......@@ -8,5 +8,11 @@ instances:
- instance: name
fields:
- field: part1
from: company.v2.company
from: company.v1.company
select: content
- instance: abbreviation
fields:
- field: part1
from: company_abbreviation.v1.company_abbreviation
select: content
\ No newline at end of file
title: 地支
desc:
author: wwccss
version: 2.0
field: field1
instances:
- instance: earthly_branches
note:
fields:
- field: content
from: earthly_branches.v1.earthly_branches
select: content
title: 五行
desc:
author: wwccss
version: 2.0
field: field1
instances:
- instance: five_elements
note:
fields:
- field: content
from: five_elements.v1.five_elements
select: content
title: 天干
desc:
author: wwccss
version: 2.0
field: field1
instances:
- instance: heavenly_stems
note:
fields:
- field: content
from: heavenly_stems.v1.heavenly_stems
select: content
title: 歇后语
desc:
author: wwccss
version: 2.0
field: field1
instances:
- instance: idiom
note:
fields:
- field: word
from: idiom.v1.idiom
select: word
- field: explanation
from: idiom.v1.idiom
select: explanation
......@@ -8,7 +8,7 @@ instances:
note: log
fields:
- field: host
from: ip.v2.yaml
from: ip.v1.yaml
use: ipC{10},ipB{10},ipA{10}
postfix: " - - "
......
title: 职业
desc:
author: wwccss
version: 2.0
field: field1
instances:
- instance: occupation
note:
fields:
- field: content
from: occupation.v1.occupation
select: content
title: 行星
desc:
author: wwccss
version: 2.0
field: field1
instances:
- instance: planet
note:
fields:
- field: content
from: planet.v1.planet
select: content
title: 季节
desc:
author: wwccss
version: 2.0
field: field1
instances:
- instance: season
note:
fields:
- field: content
from: season.v1.season
select: content
title: 歇后语
desc:
author: wwccss
version: 2.0
field: xiehouyu
instances:
- instance: xiehouyu
note:
fields:
- field: riddle
from: xiehouyu.v1.xiehouyu
select: riddle
postfix: "-"
- field: answer
from: xiehouyu.v1.xiehouyu
select: answer
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册