Skip to content

Commit

Permalink
Merge pull request #27 from koalaylj/dev
Browse files Browse the repository at this point in the history
v0.3.2
  • Loading branch information
zk-luke authored Jun 5, 2018
2 parents e9a20ae + 327692b commit 8785177
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 120 deletions.
47 changes: 20 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
> 让excel支持表达复杂的json格式,将xlsx文件转成json。
### 日志
* 2017-10-31 v0.3.1
* 修复: 对象类型列,对象值里面有冒号值解析错误
* 功能: 增加#id类型,主键索引(json格式以map形式输出,无id的表以数组格式输出)
* 功能: 输出的json是否压缩可以在config.json里面配置
* 2017-10-31 v0.3.2
* sheet名字以`#`开头则不导出此表
* 增加输出d.ts功能
* 去掉config.json中的arraySeparator配置

* 2017-10-26 v0.3.0
* 修复中文自动追加拼音问题。
* 修复日期解析错误。
* 去掉主外键功能(以后再追加)。
* 增加代码静态检查,核心代码重写并改成ES6语法。
* 更新依赖插件的版本。
### 分支

### npm相关
* 如需当做npm模块引用请切换到`npm`分支。
* `master`为主分支,此分支用于发布版本,包含当前稳定代码,不要往主分支直接提交代码。
* `dev`为开发分支,新功能提交到此分支,待稳定后合并到`master`分支。
* 如需当做npm模块引用请切换到`npm`分支(尚有功能未合并)。

### 使用说明
* 目前只支持.xlsx格式,不支持.xls格式。
Expand Down Expand Up @@ -48,15 +44,11 @@ npm install
/**
* 导出的json存放的位置
*/
"dest": "./json",

/**
* 数组的分隔符
* 有时候特殊需要,在excel单元格中里面逗号被当做他用。
* 已过时,将在v0.5.x移除。参考列类型是数组类型时候表头设置。
*/
"arraySeparator":","
"dest": "./json"
},

"ts":false,//是否导出d.ts(for typescript)

"json": {
/**
* 导出的json是否需要压缩
Expand Down Expand Up @@ -116,6 +108,7 @@ npm install
```

### 支持以下数据类型

* number 数字类型。
* boolean 布尔。
* string 字符串。
Expand All @@ -128,6 +121,7 @@ npm install
* id 主键类型(当表中有这个类型的时候,json会以map格式输出,否则以数组格式输出)。

### 表头规则

* 基本数据类型(string,number,bool)时候,一般不需要设置会自动判断,但是也可以明确声明数据类型。
* 字符串类型:命名形式 `列名#string`
* 数字类型:命名形式 `列名#number`
Expand All @@ -136,25 +130,24 @@ npm install
* 基本类型数组:命名形式 `列名#[]`,数组元素默认用逗号分隔(a,b,c),自定义数组元素分隔符`列名#[]/`(a/b/c)。
* 对象:命名形式 `列名#{}`
* 对象数组:命名形式`列名#[{}]`
* 主键:命名形式`列名#id`
* 主键:命名形式`列名#id` ,设置此将会输出为json map 格式

### 规则

### 数据规则
* 关键符号都是半角符号。
* 对象属性使用分号`;`分割。
* sheet名字以`#`开头则不导出此表。

### TODO

- [ ] 列为数组类型时候,嵌套复杂类型。
- [ ] 列为对象类型时候,嵌套复杂类型。
- [ ] 外键支持。
- [ ] 将主分支的代码合并到npm分支。
- [x] 数组分隔符的设置放到表头,默认用逗号。

### 感谢
某些想法也是借鉴了一个clojure的excel转json的开源项目 [excel-to-json](https://github.com/mhaemmerle/excel-to-json)

### 补充
* windows/mac/linux都支持。

* 可在windows/mac/linux下运行。
* 项目地址 [xlsx2json master](https://github.com/koalaylj/xlsx2json)
* 如有问题可以到QQ群内讨论:223460081
* 招募协作开发者,有时间帮助一起维护下这个项目,可以发issue或者到qq群里把你github邮箱告诉我。
6 changes: 3 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"xlsx": {
"head": 2,
"src": "./excel/**/[^~$]*.xlsx",
"dest": "./json",
"arraySeparator": ","
"dest": "./json"
},
"ts": false,
"json": {
"uglify": false
}
}
}
Binary file added excel/cat_config.xlsx
Binary file not shown.
Binary file modified excel/product.xlsx
Binary file not shown.
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let keys = Object.keys(commands);

for (let key in commands) {
let alias_array = commands[key].alias;
alias_array.forEach((e) => {
alias_array.forEach(e => {
alias_map[e] = key;
});
}
Expand All @@ -41,7 +41,7 @@ parsed_cmds = parseCommandLine(process.argv);

// console.log("%j", parsed_cmds);

parsed_cmds.forEach(function(e) {
parsed_cmds.forEach(function (e) {
exec(e);
});

Expand All @@ -53,21 +53,21 @@ parsed_cmds.forEach(function(e) {
function exportJson(args) {

if (typeof args === 'undefined' || args.length === 0) {
glob(config.xlsx.src, function(err, files) {
glob(config.xlsx.src, function (err, files) {
if (err) {
console.error("exportJson error:", err);
throw err;
}

files.forEach(function(element, index, array) {
xlsx.toJson(path.join(__dirname, element), path.join(__dirname, config.xlsx.dest),config);
files.forEach(function (element, index, array) {
xlsx.toJson(path.join(__dirname, element), path.join(__dirname, config.xlsx.dest), config);
});

});
} else {
if (args instanceof Array) {
args.forEach(function(element, index, array) {
xlsx.toJson(path.join(__dirname, element), path.join(__dirname, config.xlsx.dest),config);
args.forEach(function (element, index, array) {
xlsx.toJson(path.join(__dirname, element), path.join(__dirname, config.xlsx.dest), config);
});
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ function parseCommandLine(args) {
let pos = 0;
let cmd;

cli.forEach(function(element, index, array) {
cli.forEach(function (element, index, array) {

//replace alias name with real name.
if (element.indexOf('--') === -1 && element.indexOf('-') === 0) {
Expand Down Expand Up @@ -170,4 +170,4 @@ function defaultCommand() {
}
}

/*************************************************************************/
/*************************************************************************/
Loading

0 comments on commit 8785177

Please sign in to comment.