Compare commits

..

No commits in common. "main" and "v1.0.0" have entirely different histories.
main ... v1.0.0

14 changed files with 74 additions and 192 deletions

View File

@ -1,8 +1,8 @@
name: Docker CI name: Docker CI
on: on:
push: release:
branches: [main] types: [created]
jobs: jobs:
docker: docker:
@ -20,4 +20,4 @@ jobs:
- name: Build and Push from Makefile - name: Build and Push from Makefile
run: | run: |
make build publish make build

View File

@ -25,3 +25,16 @@ jobs:
- run: npm publish - run: npm publish
env: env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}} 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}}

View File

@ -3,7 +3,7 @@ LABEL maintainer="sombochea@cubetiqs.com"
VOLUME [ "/app/data" ] VOLUME [ "/app/data" ]
ENV INPUT_FILE './data/source.xlsx' ENV INPUT_FILE './data/people.xlsx'
ENV OUTPUT_PATH './data/outputs' ENV OUTPUT_PATH './data/outputs'
ENV MAPPER_FILE './data/mapper.json' ENV MAPPER_FILE './data/mapper.json'
@ -11,12 +11,6 @@ WORKDIR /app
COPY . /app COPY . /app
RUN npm install RUN yarn
RUN npm link CMD [ "node" , "index.js"]
RUN ln -s $(which excel2json-xlsx) /usr/bin/x2j
ENTRYPOINT [ "/usr/bin/x2j"]
CMD [ "--help" ]

View File

@ -1,4 +1,4 @@
DOCKER_IMAGE=cubetiq/excel2json DOCKER_IMAGE=cubetiq/node-excel2json
build: build:
@echo "Building docker image..." @echo "Building docker image..."
@ -8,8 +8,4 @@ run:
@echo "Running container..." @echo "Running container..."
docker run --rm -t ${DOCKER_IMAGE} docker run --rm -t ${DOCKER_IMAGE}
publish:
@echo "Publishing docker image..."
docker push ${DOCKER_IMAGE}
.PHONY: build .PHONY: build

View File

@ -1,44 +1,10 @@
# excel2json-xlsx # excel2json
[![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) [![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 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 - Read file excel to json
- Custom mapping with custom columns and configs - 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 # Build

View File

@ -1,54 +0,0 @@
#!/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);
}

View File

@ -1 +1,18 @@
{} {
"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
View File

@ -1,8 +1,7 @@
export interface Excel2JsonProps { interface Excel2JsonProps {
mappings?: Array<any>; mappings?: Array<any>;
saveToOutput?: boolean; saveToOutput?: boolean;
outputPath?: string; outputPath?: string;
outputFile?: string;
outputName?: string; outputName?: string;
sheetName?: string; sheetName?: string;
inputFile?: string; inputFile?: string;
@ -10,4 +9,4 @@ export interface Excel2JsonProps {
encoding?: string; encoding?: string;
} }
export declare function excel2json(props: Excel2JsonProps); declare function excel2json(props: Excel2JsonProps);

View File

@ -1,14 +1,14 @@
const XLSX = require("xlsx"); const XLSX = require("xlsx");
const fs = require("fs"); const fs = require("fs");
const { splitFilepath } = require("./util");
// do export for excel to json output or data json object // do export for excel to json output or data json object
function _internalExport(props = {}) { function _internalExport(props = {}) {
const NAME = process.env.APP_NAME || "excel2json"; const NAME = process.env.APP_NAME || "excel2json";
console.log("Name =>", NAME, "\n"); console.log("APP NAME =>", NAME, "\n");
// load from env // load from env
const INPUT_FILE = props.inputFile || process.env.INPUT_FILE; const INPUT_FILE =
props.inputFile || process.env.INPUT_FILE || "./data/people.xlsx";
const OUTPUT_PATH = const OUTPUT_PATH =
props.outputPath || process.env.OUTPUT_PATH || "./data/outputs"; props.outputPath || process.env.OUTPUT_PATH || "./data/outputs";
const MAPPER_FILE = const MAPPER_FILE =
@ -30,21 +30,17 @@ function _internalExport(props = {}) {
} }
); );
} catch (err) { } catch (err) {
// console.error("read file error", err); console.error("read file error", err);
} }
// convert mapper from string to json object // convert mapper from string to json object
const mapperJson = mapperString ? JSON.parse(mapperString) : {}; const mapperJson = mapperString ? JSON.parse(mapperString) : {};
const configs = { ...mapperJson.configs, ...props }; const configs = { ...mapperJson.configs, ...props };
console.log(configs);
const columsData = props.mappings || mapperJson.data || undefined; 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 // read workbook from excel file
const wb = XLSX.readFile(inFile); const wb = XLSX.readFile(configs.inputFile || INPUT_FILE);
const xlData = XLSX.utils.sheet_to_json( const xlData = XLSX.utils.sheet_to_json(
wb.Sheets[configs.sheetName || SHEET_NAME] wb.Sheets[configs.sheetName || SHEET_NAME]
); );
@ -65,47 +61,31 @@ function _internalExport(props = {}) {
}); });
// able to save to output or not (default is true) // able to save to output or not (default is true)
if (configs.saveToOutputput || configs.outputFile) { if (configs.saveToOutputput) {
var outputPath = undefined; // parse a new path
const filePath = splitFilepath(configs.outputFile); const outputPath = configs.outputPath || OUTPUT_PATH;
// validate path and filename // check directory and create it if not exist
if (filePath && filePath.path) { if (!fs.existsSync(outputPath)) {
outputPath = filePath.path; fs.mkdirSync(outputPath, { recursive: true });
}
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 // json data output
const jsonStringData = JSON.stringify(data); const jsonStringData = JSON.stringify(data);
try { try {
var outFile = undefined;
if (configs.outputFile) {
outFile = configs.outputFile;
} else {
outFile = `${outputPath}/${
configs.outputName || "exported"
}_${new Date().getTime()}.json`;
}
// write to file // write to file
fs.writeFileSync(outFile, jsonStringData, (err) => { fs.writeFileSync(
if (err) throw err; `${outputPath}/${
}); configs.outputName || "exported"
}_${new Date().getTime()}.json`,
console.log("Exported excel to json to output =>", outFile); jsonStringData,
} catch (err) { (err) => {
console.error(err); if (err) throw err;
} console.log("Write succeed!");
}
);
} catch (err) {}
} }
return data; return data;
@ -117,6 +97,6 @@ function _internalExport(props = {}) {
* *
* @returns JSON Object of result * @returns JSON Object of result
*/ */
module.exports.excel2json = excel2json = (props = {}) => { module.exports = excel2json = (props = {}) => {
return _internalExport(props); return _internalExport(props);
}; };

View File

@ -1,5 +1,8 @@
// import excel2json module // import excel2json module
const { excel2json } = require("./excel2json"); const excel2json = require("./excel2json");
// export the function // called function export excel2json
module.exports = excel2json; const exported = excel2json();
// output data from exported
console.log("Output =>\n", exported);

View File

@ -1,17 +1,6 @@
{ {
"name": "excel2json-xlsx", "name": "excel2json-xlsx",
"version": "1.0.6", "version": "1.0.0",
"keywords": [
"excel",
"json",
"excel2json",
"parser",
"xlsx"
],
"publishConfig": {
"access": "public"
},
"bin": "./bin/cli.js",
"description": "Excel to JSON, able to export JSON from Excel (xlsx)", "description": "Excel to JSON, able to export JSON from Excel (xlsx)",
"repository": { "repository": {
"url": "https://github.com/CUBETIQ/excel2json.git" "url": "https://github.com/CUBETIQ/excel2json.git"
@ -24,7 +13,6 @@
"author": "Sambo Chea <sombochea@cubetiqs.com>", "author": "Sambo Chea <sombochea@cubetiqs.com>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"xlsx": "^0.16.9", "xlsx": "^0.16.9"
"yargs": "^16.2.0"
} }
} }

View File

@ -1,5 +0,0 @@
const excel2json = require("./index");
const result = excel2json({
inputFile: "./data/source.xlsx",
});
console.log(result);

15
util.js
View File

@ -1,15 +0,0 @@
/**
* 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,
};
}