🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →

js2excel

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js2excel - npm Package Compare versions

Comparing version

to
0.7.8

{
"name": "js2excel",
"version": "0.7.5",
"version": "0.7.8",
"author": "pomysky@gmail.com",

@@ -23,3 +23,3 @@ "description": "A simple module for excel and json converts each other",

"scripts": {
"clean": "rm -rf dist typings example/dist types",
"clean": "rm -rf dist typings types",
"dev": "./node_modules/.bin/webpack --config ./webpack.example.config.js -w --progress --display-error-details",

@@ -31,3 +31,4 @@ "prepush": "npm run lint",

"lint": "./node_modules/.bin/tslint -p ./tsconfig.json --type-check src/**/*.ts",
"fix": "./node_modules/.bin/tslint -p ./tsconfig.json --type-check --fix src/**/*.ts"
"fix": "./node_modules/.bin/tslint -p ./tsconfig.json --type-check --fix src/**/*.ts",
"prepublishOnly": "npm run clean && npm run build"
},

@@ -34,0 +35,0 @@ "dependencies": {

## js2excel
A simple module exports json data to excel, which works in the browser.
A simple module for excel and json converts each other.

@@ -27,8 +27,10 @@ ## Installation

## Usage
### Convert json to excel
```
// es6
import js2excel from 'js2excel';
import {json2excel, excel2json} from 'js2excel';
//CommonJS
let js2excel = require('js2excel');
let { json2excel, excel2json } = require('js2excel');

@@ -39,6 +41,6 @@ /**

// excel's header columes
let columns = [
// excel's columns's header
let headers = [
{
// the name will be as the excel colume name
// the name will be as the excel column name
name: 'User Id',

@@ -55,2 +57,6 @@ // the prop will be as the excel row data, which is the rows' item's property.

prop: 'userAddress'
},
{
name: 'Date',
prop: 'date'
}

@@ -65,3 +71,4 @@ ];

"userPhoneNumber": 1888888888,
"userAddress": 'xxxx'
"userAddress": 'xxxx',
"date": '2013/09/10 09:10' // string
},

@@ -71,3 +78,4 @@ {

"userPhoneNumber": 1888888888,
"userAddress": 'xxxx'
"userAddress": 'xxxx',
"date": new Date()
},

@@ -77,3 +85,4 @@ {

"userPhoneNumber": 1888888888,
"userAddress": 'xxxx'
"userAddress": 'xxxx',
"date": new Date()
}

@@ -85,3 +94,7 @@ ];

try {
js2excel(columns, rows, 'user-info-data');
json2excel({
headers, rows,
name: 'user-info-data',
formateDate: 'yyyy/mm/dd'
});
} catch (e) {

@@ -92,4 +105,8 @@ console.error('export error');

// for webpack 3: dynamic import
import(/* webpackChunkName: "js2excel" */ 'js2excel').then((js2excel) => {
js2excel(columns, rows, 'user-info-data');
import(/* webpackChunkName: "js2excel" */ 'js2excel').then(({json2excel}) => {
json2excel({
headers, rows,
name: 'user-info-data',
formateDate: 'dd/mm/yyyy'
});
}).catch((e) => {

@@ -99,7 +116,122 @@

```
Exports result as the image shows:
![user-data](https://sfault-image.b0.upaiyun.com/122/505/1225057000-5960fb885c904_articlex)
### Convert excel to json
```
import { excel2json } from 'js2excel';
// html
<input type="file" multiple="false" id="sheets" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" @change="onchange" />
// methods
onchange(e){
excel2json(e.target.files, (data) => {
console.log('json', data)
}, 'excel2json')
}
// for webpack 3: dynamic import
onchange(e) {
import(/* webpackChunkName: "js2excel" */ 'js2excel').then(({excel2json}) => {
excel2json(e.target.files, (data) => {
console.log('json', data)
}, 'excel2json')
}).catch((e) => {
});
}
```
Example, if you hava a excel as following:
![excel](https://sfault-image.b0.upaiyun.com/411/420/4114209136-5960fa90e8e6d_articlex)
The data maybe as following:
![data](https://sfault-image.b0.upaiyun.com/314/083/3140838997-5960fabf7c7b0_articlex)
## API
### json2excel(opts)
Convert json to excel(.xlsx).
**opts**
Type: `Object`
`opts.headers`
Type: `Array`
Default: []
Excel's column's headers.
`opts.rows`
Type: `Array`,
Default: []
Excel's rows's data.
`opts.name`
Type: `String`,
Default: 'excel'
Excel's name, whose suffix is `.xlsx`.
`opts.formateDate`
Type: `String`,
Default: 'dd/mm/yyyy'
The date formate in rows' data. Examples:
```
'dd/mm/yyyy' => 08/07/2017
'd/m/yy' => 8/7/17
'd/m/yy hh:ss' => 8/7/17 18:29
'yyyy/mm/dd hh:ss' => 2017/07/17 18:29
```
### excel2json(files, cb(data), [defval])
Convert excel(.xlsx/.xls) to json.
**files**
Type: `Array`
[FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) from `<input type='file' multiple="false" >`.
**cb(data)**
Type: `Function`
Callback function called on finish. The `data` maybe as following:
```
{
`${sheetName}`: `[${excelRowsData}]`
'sheet1': [/** excel rows' data **/],
'sheet2': [/** excel rows' data **/],
'sheet3': [/** excel rows' data **/]
...
}
```
**defval**
Type: `String`
The default value when the row data corresponding to the column is blank.
## Supported browsers
See this: [FileSaver#supported-browsers](https://github.com/eligrey/FileSaver.js#supported-browsers)
* [FileSaver#supported-browsers](https://github.com/eligrey/FileSaver.js#supported-browsers)
* [FileReader](https://caniuse.com/#search=FileReader)
## License
MIT