Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
f65301ce4d | |||
2b170c9bc0 | |||
|
88645b98ea | ||
fa8d223311 | |||
ab7a43a93f | |||
d9ee930204 | |||
abfeea00ec | |||
b7333cc184 | |||
338d2033bb | |||
ad92cd2899 | |||
3a0191a5d6 | |||
e1ebbf67d8 | |||
201be2179e | |||
acf0978d76 | |||
|
6c238508e2 | ||
f997501489 |
6
.github/workflows/docker-publish.yml
vendored
6
.github/workflows/docker-publish.yml
vendored
@ -1,8 +1,8 @@
|
||||
name: Docker CI
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [created]
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
docker:
|
||||
@ -20,4 +20,4 @@ jobs:
|
||||
|
||||
- name: Build and Push from Makefile
|
||||
run: |
|
||||
make build
|
||||
make build publish
|
||||
|
13
.github/workflows/npm-publish.yml
vendored
13
.github/workflows/npm-publish.yml
vendored
@ -25,16 +25,3 @@ jobs:
|
||||
- run: npm publish
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
||||
|
||||
publish-gpr:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: 14
|
||||
registry-url: https://npm.pkg.github.com/
|
||||
- run: npm publish
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||
|
12
Dockerfile
12
Dockerfile
@ -3,7 +3,7 @@ LABEL maintainer="sombochea@cubetiqs.com"
|
||||
|
||||
VOLUME [ "/app/data" ]
|
||||
|
||||
ENV INPUT_FILE './data/people.xlsx'
|
||||
ENV INPUT_FILE './data/source.xlsx'
|
||||
ENV OUTPUT_PATH './data/outputs'
|
||||
ENV MAPPER_FILE './data/mapper.json'
|
||||
|
||||
@ -11,6 +11,12 @@ WORKDIR /app
|
||||
|
||||
COPY . /app
|
||||
|
||||
RUN yarn
|
||||
RUN npm install
|
||||
|
||||
CMD [ "node" , "index.js"]
|
||||
RUN npm link
|
||||
|
||||
RUN ln -s $(which excel2json-xlsx) /usr/bin/x2j
|
||||
|
||||
ENTRYPOINT [ "/usr/bin/x2j"]
|
||||
|
||||
CMD [ "--help" ]
|
6
Makefile
6
Makefile
@ -1,4 +1,4 @@
|
||||
DOCKER_IMAGE=cubetiq/node-excel2json
|
||||
DOCKER_IMAGE=cubetiq/excel2json
|
||||
|
||||
build:
|
||||
@echo "Building docker image..."
|
||||
@ -8,4 +8,8 @@ run:
|
||||
@echo "Running container..."
|
||||
docker run --rm -t ${DOCKER_IMAGE}
|
||||
|
||||
publish:
|
||||
@echo "Publishing docker image..."
|
||||
docker push ${DOCKER_IMAGE}
|
||||
|
||||
.PHONY: build
|
||||
|
38
README.md
38
README.md
@ -1,10 +1,44 @@
|
||||
# excel2json
|
||||
|
||||
# excel2json-xlsx
|
||||
[![Node.js Package](https://github.com/CUBETIQ/excel2json/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/CUBETIQ/excel2json/actions/workflows/npm-publish.yml)
|
||||
[![Docker CI](https://github.com/CUBETIQ/excel2json/actions/workflows/docker-publish.yml/badge.svg)](https://github.com/CUBETIQ/excel2json/actions/workflows/docker-publish.yml)
|
||||
![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/cubetiq/excel2json)
|
||||
![Docker Pulls](https://img.shields.io/docker/pulls/cubetiq/excel2json)
|
||||
|
||||
- Read file excel to json
|
||||
- Custom mapping with custom columns and configs
|
||||
- Custom props
|
||||
- Docker suppport
|
||||
|
||||
# Install
|
||||
```shell
|
||||
npm i excel2json-xlsx
|
||||
```
|
||||
OR
|
||||
```shell
|
||||
yarn i excel2json-xlsx
|
||||
```
|
||||
OR Global Install
|
||||
```shell
|
||||
yarn add global excel2json-xlsx
|
||||
```
|
||||
Usage
|
||||
```shell
|
||||
excel2json-xlsx -i source.xlsx -o exported.json
|
||||
```
|
||||
OR
|
||||
```shell
|
||||
npx excel2json-xlsx -i source.xlsx -o exported.json
|
||||
```
|
||||
|
||||
# [Docker Hub](https://hub.docker.com/r/cubetiq/excel2json)
|
||||
***Pulling image***
|
||||
```shell
|
||||
docker pull cubetiq/excel2json
|
||||
```
|
||||
***Run container***
|
||||
```shell
|
||||
docker run --rm -it -v /my/path/data:/app/data cubetiq/excel2json -i ./data/source.xlsx -p true
|
||||
```
|
||||
|
||||
# Build
|
||||
|
||||
|
54
bin/cli.js
Normal file
54
bin/cli.js
Normal file
@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const yargs = require("yargs");
|
||||
const excel2json = require("./../index");
|
||||
|
||||
const options = yargs
|
||||
.usage("Usage: -i <input> -o <output> -m <mapper>")
|
||||
.option("i", {
|
||||
alias: "input",
|
||||
describe: "Input File Excel (Required .xlsx)",
|
||||
type: "string",
|
||||
demandOption: true,
|
||||
})
|
||||
.option("o", {
|
||||
alias: "output",
|
||||
describe: "Output File to JSON",
|
||||
type: "string",
|
||||
demandOption: false,
|
||||
})
|
||||
.option("m", {
|
||||
alias: "mapper",
|
||||
describe: "Mapper File in JSON",
|
||||
type: "string",
|
||||
demandOption: false,
|
||||
})
|
||||
.option("s", {
|
||||
alias: "sheet",
|
||||
describe: "Sheet Name in workbook",
|
||||
type: "string",
|
||||
demandOption: false,
|
||||
})
|
||||
.option("p", {
|
||||
alias: "print",
|
||||
describe: "Print the exported json to console",
|
||||
type: "boolean",
|
||||
demandOption: false,
|
||||
}).argv;
|
||||
|
||||
const inputFile = options.input;
|
||||
const outputFile = options.output;
|
||||
const mapperFile = options.mapper;
|
||||
const sheetName = options.sheet;
|
||||
const print = options.print;
|
||||
|
||||
const exported = excel2json({
|
||||
inputFile: inputFile,
|
||||
mapperFile: mapperFile,
|
||||
outputFile: outputFile,
|
||||
sheetName: sheetName,
|
||||
});
|
||||
|
||||
if (print) {
|
||||
console.log(exported);
|
||||
}
|
@ -1,18 +1 @@
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"dataIndex": "Name",
|
||||
"label": "Name"
|
||||
},
|
||||
{
|
||||
"dataIndex": "Age",
|
||||
"label": "Age"
|
||||
}
|
||||
],
|
||||
"configs": {
|
||||
"outputPath": "./data/outputs/exported",
|
||||
"outputName": "my_exported_data",
|
||||
"sheetName": "Sheet1",
|
||||
"saveToOutput": true
|
||||
}
|
||||
}
|
||||
{}
|
5
excel2json.d.ts
vendored
5
excel2json.d.ts
vendored
@ -1,7 +1,8 @@
|
||||
interface Excel2JsonProps {
|
||||
export interface Excel2JsonProps {
|
||||
mappings?: Array<any>;
|
||||
saveToOutput?: boolean;
|
||||
outputPath?: string;
|
||||
outputFile?: string;
|
||||
outputName?: string;
|
||||
sheetName?: string;
|
||||
inputFile?: string;
|
||||
@ -9,4 +10,4 @@ interface Excel2JsonProps {
|
||||
encoding?: string;
|
||||
}
|
||||
|
||||
declare function excel2json(props: Excel2JsonProps);
|
||||
export declare function excel2json(props: Excel2JsonProps);
|
||||
|
@ -1,14 +1,14 @@
|
||||
const XLSX = require("xlsx");
|
||||
const fs = require("fs");
|
||||
const { splitFilepath } = require("./util");
|
||||
|
||||
// do export for excel to json output or data json object
|
||||
function _internalExport(props = {}) {
|
||||
const NAME = process.env.APP_NAME || "excel2json";
|
||||
console.log("APP NAME =>", NAME, "\n");
|
||||
console.log("Name =>", NAME, "\n");
|
||||
|
||||
// load from env
|
||||
const INPUT_FILE =
|
||||
props.inputFile || process.env.INPUT_FILE || "./data/people.xlsx";
|
||||
const INPUT_FILE = props.inputFile || process.env.INPUT_FILE;
|
||||
const OUTPUT_PATH =
|
||||
props.outputPath || process.env.OUTPUT_PATH || "./data/outputs";
|
||||
const MAPPER_FILE =
|
||||
@ -30,17 +30,21 @@ function _internalExport(props = {}) {
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("read file error", err);
|
||||
// console.error("read file error", err);
|
||||
}
|
||||
|
||||
// convert mapper from string to json object
|
||||
const mapperJson = mapperString ? JSON.parse(mapperString) : {};
|
||||
const configs = { ...mapperJson.configs, ...props };
|
||||
console.log(configs);
|
||||
const columsData = props.mappings || mapperJson.data || undefined;
|
||||
const inFile = configs.inputFile || INPUT_FILE;
|
||||
|
||||
if (!inFile) {
|
||||
throw Error("Input file is required!");
|
||||
}
|
||||
|
||||
// read workbook from excel file
|
||||
const wb = XLSX.readFile(configs.inputFile || INPUT_FILE);
|
||||
const wb = XLSX.readFile(inFile);
|
||||
const xlData = XLSX.utils.sheet_to_json(
|
||||
wb.Sheets[configs.sheetName || SHEET_NAME]
|
||||
);
|
||||
@ -61,31 +65,47 @@ function _internalExport(props = {}) {
|
||||
});
|
||||
|
||||
// able to save to output or not (default is true)
|
||||
if (configs.saveToOutputput) {
|
||||
// parse a new path
|
||||
const outputPath = configs.outputPath || OUTPUT_PATH;
|
||||
if (configs.saveToOutputput || configs.outputFile) {
|
||||
var outputPath = undefined;
|
||||
const filePath = splitFilepath(configs.outputFile);
|
||||
|
||||
// check directory and create it if not exist
|
||||
if (!fs.existsSync(outputPath)) {
|
||||
fs.mkdirSync(outputPath, { recursive: true });
|
||||
// validate path and filename
|
||||
if (filePath && filePath.path) {
|
||||
outputPath = filePath.path;
|
||||
}
|
||||
|
||||
if (filePath.path) {
|
||||
// parse a new path
|
||||
outputPath = outputPath || configs.outputPath || OUTPUT_PATH;
|
||||
|
||||
// check directory and create it if not exist
|
||||
if (!fs.existsSync(outputPath)) {
|
||||
fs.mkdirSync(outputPath, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
// json data output
|
||||
const jsonStringData = JSON.stringify(data);
|
||||
|
||||
try {
|
||||
// write to file
|
||||
fs.writeFileSync(
|
||||
`${outputPath}/${
|
||||
var outFile = undefined;
|
||||
if (configs.outputFile) {
|
||||
outFile = configs.outputFile;
|
||||
} else {
|
||||
outFile = `${outputPath}/${
|
||||
configs.outputName || "exported"
|
||||
}_${new Date().getTime()}.json`,
|
||||
jsonStringData,
|
||||
(err) => {
|
||||
if (err) throw err;
|
||||
console.log("Write succeed!");
|
||||
}
|
||||
);
|
||||
} catch (err) {}
|
||||
}_${new Date().getTime()}.json`;
|
||||
}
|
||||
|
||||
// write to file
|
||||
fs.writeFileSync(outFile, jsonStringData, (err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
|
||||
console.log("Exported excel to json to output =>", outFile);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
@ -97,6 +117,6 @@ function _internalExport(props = {}) {
|
||||
*
|
||||
* @returns JSON Object of result
|
||||
*/
|
||||
module.exports = excel2json = (props = {}) => {
|
||||
module.exports.excel2json = excel2json = (props = {}) => {
|
||||
return _internalExport(props);
|
||||
};
|
||||
|
9
index.js
9
index.js
@ -1,8 +1,5 @@
|
||||
// import excel2json module
|
||||
const excel2json = require("./excel2json");
|
||||
const { excel2json } = require("./excel2json");
|
||||
|
||||
// called function export excel2json
|
||||
const exported = excel2json();
|
||||
|
||||
// output data from exported
|
||||
console.log("Output =>\n", exported);
|
||||
// export the function
|
||||
module.exports = excel2json;
|
||||
|
16
package.json
16
package.json
@ -1,6 +1,17 @@
|
||||
{
|
||||
"name": "excel2json-xlsx",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.6",
|
||||
"keywords": [
|
||||
"excel",
|
||||
"json",
|
||||
"excel2json",
|
||||
"parser",
|
||||
"xlsx"
|
||||
],
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"bin": "./bin/cli.js",
|
||||
"description": "Excel to JSON, able to export JSON from Excel (xlsx)",
|
||||
"repository": {
|
||||
"url": "https://github.com/CUBETIQ/excel2json.git"
|
||||
@ -13,6 +24,7 @@
|
||||
"author": "Sambo Chea <sombochea@cubetiqs.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"xlsx": "^0.16.9"
|
||||
"xlsx": "^0.16.9",
|
||||
"yargs": "^16.2.0"
|
||||
}
|
||||
}
|
||||
|
5
test.js
Normal file
5
test.js
Normal file
@ -0,0 +1,5 @@
|
||||
const excel2json = require("./index");
|
||||
const result = excel2json({
|
||||
inputFile: "./data/source.xlsx",
|
||||
});
|
||||
console.log(result);
|
15
util.js
Normal file
15
util.js
Normal file
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Split Filepath to Path and Filename
|
||||
*
|
||||
* @param {String} filePath
|
||||
* @returns Object of JSON
|
||||
*/
|
||||
module.exports.splitFilepath = function splitFilepath(filePath) {
|
||||
const filename = filePath.replace(/^.*[\\\/]/, "");
|
||||
const path = filePath.replace("/" + filename, "");
|
||||
|
||||
return {
|
||||
path: path == filename ? undefined : path,
|
||||
filename,
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user