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

combine all excel data to single table

上级 f61ec3e4
package main
import (
"github.com/Chain-Zhang/pinyin"
commonUtils "github.com/easysoft/zendata/src/utils/common"
fileUtils "github.com/easysoft/zendata/src/utils/file"
_ "github.com/mattn/go-sqlite3"
"io/ioutil"
"path"
"path/filepath"
"strings"
)
func getFilesInDir(folder, ext string, files *[]string) {
folder, _ = filepath.Abs(folder)
if !fileUtils.IsDir(folder) {
if path.Ext(folder) == ext {
*files = append(*files, folder)
}
return
}
dir, err := ioutil.ReadDir(folder)
if err != nil {
return
}
for _, fi := range dir {
name := fi.Name()
if commonUtils.IngoreFile(name) {
continue
}
filePath := fileUtils.AddSepIfNeeded(folder) + name
if fi.IsDir() {
getFilesInDir(filePath, ext, files)
} else if strings.Index(name, "~") != 0 && path.Ext(filePath) == ".xlsx" {
*files = append(*files, filePath)
}
}
}
func getFileName(filePath string) string {
fileName := path.Base(filePath)
fileName = strings.TrimSuffix(fileName, path.Ext(filePath))
return fileName
}
func changeFileExt(filePath, ext string) string {
ret := strings.TrimSuffix(filePath, path.Ext(filePath))
ret += ext
return ret
}
func getPinyin(word string) string {
p, _ := pinyin.New(word).Split("").Mode(pinyin.WithoutTone).Convert()
return p
}
\ No newline at end of file
......@@ -4,16 +4,10 @@ import (
"database/sql"
"fmt"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/Chain-Zhang/pinyin"
commonUtils "github.com/easysoft/zendata/src/utils/common"
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/mattn/go-sqlite3"
"io/ioutil"
"path"
"path/filepath"
"strconv"
"strings"
"testing"
......@@ -21,7 +15,7 @@ import (
func TestImportSqlite(t *testing.T) {
files := make([]string, 0)
getExcelFilesInDir("xdoc/words-9.3", &files)
getFilesInDir("xdoc/words-9.3", "xlsx", &files)
tableName := "words"
seq := 1
......@@ -69,8 +63,7 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[]
return
}
fileName := path.Base(filePath)
fileName = strings.TrimSuffix(fileName, path.Ext(filePath))
fileName := getFileName(filePath)
fileName = strings.TrimSuffix(fileName, "词库")
colPrefix := getPinyin(fileName)
......@@ -95,7 +88,8 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[]
colName := getPinyin(val)
if colIndex == 0 && colName != "ci" {
colName = "ci"
} else {
}
if colName != "ci" {
colName = colPrefix + ":" + colName
}
......@@ -145,41 +139,4 @@ func importExcel(filePath, tableName string, seq *int, ddlFields, insertSqls *[]
)
*insertSqls = append(*insertSqls, insertSql)
}
}
func getExcelFilesInDir(folder string, files *[]string) {
folder, _ = filepath.Abs(folder)
if !fileUtils.IsDir(folder) {
if path.Ext(folder) == ".xlsx" {
*files = append(*files, folder)
}
return
}
dir, err := ioutil.ReadDir(folder)
if err != nil {
return
}
for _, fi := range dir {
name := fi.Name()
if commonUtils.IngoreFile(name) {
continue
}
filePath := fileUtils.AddSepIfNeeded(folder) + name
if fi.IsDir() {
getExcelFilesInDir(filePath, files)
} else if strings.Index(name, "~") != 0 && path.Ext(filePath) == ".xlsx" {
*files = append(*files, filePath)
}
}
}
func getPinyin(word string) string {
p, _ := pinyin.New(word).Split("").Mode(pinyin.WithoutTone).Convert()
return p
}
\ No newline at end of file
package main
import (
fileUtils "github.com/easysoft/zendata/src/utils/file"
_ "github.com/mattn/go-sqlite3"
"testing"
)
func TestGenerate(t *testing.T) {
files := make([]string, 0)
getFilesInDir("xdoc/words-9.3", ".txt", &files)
for _, filePath := range files {
article := fileUtils.ReadFile(filePath)
content := convertToYaml(article)
newPath := changeFileExt(filePath, ".yaml")
fileUtils.WriteFile(newPath, content)
}
}
func convertToYaml(article string) (content string) {
return
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册