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

looking for excels in a dir

上级 34b67d9b
“秋天的天气”+(形容词-天气-秋季-好)、(形容词-天气-秋季-好),(形容词-外貌-女性)+(名词-姓+名词-名字=F)+(副词-程度副词)+(形容词做谓语-情绪-快乐),“因为”+(名词-称呼=F)+(动词-及物+名词-意愿)+“带她去”+(名词-地点-名山)+(动词-及物+名词-动作-眼部-中性)+(形容词-颜色-植物-树木)+(名词-植物-树木)+(连词-并列关系)+(形容词-颜色-植物-花卉)+(名词-植物-花卉)。(S:形容词-外貌-女性)+(S:名词-姓+名词-名字=F)+(副词-程度副词)+(形容词做谓语-情绪-快乐),(副词-形容词做副词-情绪-快乐)+(动词-及物+名词-动作-手臂-亲切)+(形容词-性格-积极)+(S:名词-称呼=F)。
\ No newline at end of file
......@@ -6,6 +6,7 @@ import (
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
fileUtils "github.com/easysoft/zendata/src/utils/file"
i118Utils "github.com/easysoft/zendata/src/utils/i118"
logUtils "github.com/easysoft/zendata/src/utils/log"
"github.com/easysoft/zendata/src/utils/vari"
......@@ -15,19 +16,21 @@ import (
"time"
)
func GenerateFieldValuesFromExcel(path, sheet string, field *model.DefField) (map[string][]string, string) {
func GenerateFieldValuesFromExcel(filePath, sheet string, field *model.DefField) (map[string][]string, string) {
values := map[string][]string{}
dbName := getDbName(path)
dbName := getDbName(filePath)
list := make([]string, 0)
selectCol := ""
firstSheet := ConvertExcelToSQLiteIfNeeded(dbName, path)
if sheet == "" {
sheet = firstSheet
if !fileUtils.IsDir(filePath) { // file
firstSheet := ConvertSingleExcelToSQLiteIfNeeded(dbName, filePath)
if sheet == "" {
sheet = firstSheet
}
} else { // dir
ConvertExcelsToSQLiteIfNeeded(dbName, filePath)
}
list, selectCol = ReadDataFromSQLite(*field, dbName, sheet)
list, selectCol := ReadDataFromSQLite(*field, dbName, sheet)
// get index for data retrieve
numbs := GenerateIntItems(0, (int64)(len(list)-1), 1, false, 1)
// get data by index
......@@ -57,7 +60,7 @@ func getDbName(path string) (dbName string) {
return
}
func ConvertExcelToSQLiteIfNeeded(dbName string, path string) (firstSheet string) {
func ConvertSingleExcelToSQLiteIfNeeded(dbName string, path string) (firstSheet string) {
excel, err := excelize.OpenFile(path)
if err != nil {
logUtils.PrintTo(i118Utils.I118Prt.Sprintf("fail_to_read_file", path))
......@@ -160,6 +163,12 @@ func ConvertExcelToSQLiteIfNeeded(dbName string, path string) (firstSheet string
return
}
func ConvertExcelsToSQLiteIfNeeded(dbName string, dir string) {
return
}
func ReadDataFromSQLite(field model.DefField, dbName string, tableName string) ([]string, string) {
list := make([]string, 0)
......
......@@ -120,6 +120,7 @@ type DefFieldExport struct {
Prefix string `yaml:"prefix"`
Postfix string `yaml:"postfix"`
From string `yaml:"from"`
Select string `yaml:"select"`
Where string `yaml:"where"`
Rand bool `yaml:"rand"`
......
......@@ -221,21 +221,22 @@ func convertResExcelPath(from string) (ret, sheet string) {
path2 := from[:index] // address.cn.v1.china
paths := [2]string{path1, path2}
for index, path := range paths {
for index, filePath := range paths {
arr := strings.Split(path, ".")
arr := strings.Split(filePath, ".")
for i := 0; i < len(arr); i++ {
dir := ""
if i > 0 {
dir = strings.Join(arr[:i], constant.PthSep)
}
file := strings.Join(arr[i:], ".") + ".xlsx"
tagFile := strings.Join(arr[i:], ".") + ".xlsx"
relatPath := ""
if dir != "" {
relatPath = dir + constant.PthSep + file
relatPath = dir + constant.PthSep + tagFile
} else {
relatPath = file
relatPath = tagFile
}
realPth := vari.WorkDir + constant.ResDirData + constant.PthSep + relatPath
......@@ -245,6 +246,12 @@ func convertResExcelPath(from string) (ret, sheet string) {
}
ret = realPth
return
} else {
dir := path.Dir(realPth)
if FileExist(dir) {
ret = dir
return
}
}
}
}
......
......@@ -7,6 +7,7 @@ import (
fileUtils "github.com/easysoft/zendata/src/utils/file"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v3"
"path"
"strconv"
"strings"
"testing"
......@@ -18,17 +19,20 @@ const (
expLeft = "("
expRight = ")"
src = "data/words/v1"
dist = "demo"
)
func TestGenerate(ts *testing.T) {
files := make([]string, 0)
getFilesInDir("xdoc/words-9.3", ".txt", &files)
getFilesInDir(src, ".txt", &files)
for _, filePath := range files {
article := fileUtils.ReadFile(filePath)
content := convertToYaml(article)
newPath := changeFileExt(filePath, ".yaml")
newPath := fileUtils.AddSepIfNeeded(dist) + changeFileExt(path.Base(filePath), ".yaml")
fileUtils.WriteFile(newPath, content)
}
}
......@@ -76,6 +80,7 @@ func createDef(typ, table string) (conf model.DefExport) {
}
func createField(index int, prefix, exp string) (field model.DefFieldExport) {
field.From = strings.Replace(src, "/", ".", -1)
field.Field = strconv.Itoa(index)
field.Prefix = prefix
field.Rand = true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册