Add example for excel to json with mapper from file and add docker support
This commit is contained in:
parent
481c916179
commit
83431e02a9
@ -1,2 +1,3 @@
|
|||||||
# Examples
|
# Examples
|
||||||
- Say Hello to Docker with Nodejs
|
- Say Hello to Docker with Nodejs
|
||||||
|
- Excel file transform to json with Nodejs
|
5
examples/excel2json/.gitignore
vendored
Normal file
5
examples/excel2json/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
node_modules/
|
||||||
|
yarn.lock
|
||||||
|
package-lock.json
|
||||||
|
yarn-error*
|
||||||
|
outputs/
|
14
examples/excel2json/Dockerfile
Normal file
14
examples/excel2json/Dockerfile
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
FROM cubetiq/calpine-node:latest
|
||||||
|
LABEL maintainer="sombochea@cubetiqs.com"
|
||||||
|
|
||||||
|
ENV INPUT_FILE './data/people.xlsx'
|
||||||
|
ENV OUTPUT_PATH './data/outputs'
|
||||||
|
ENV MAPPER_FILE './data/mapper.json'
|
||||||
|
|
||||||
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
|
COPY . /usr/src/app
|
||||||
|
|
||||||
|
RUN yarn
|
||||||
|
|
||||||
|
CMD [ "node" , "index.js"]
|
11
examples/excel2json/Makefile
Normal file
11
examples/excel2json/Makefile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
DOCKER_IMAGE=cubetiq/node-excel2json
|
||||||
|
|
||||||
|
build:
|
||||||
|
@echo "Building docker image..."
|
||||||
|
docker build . -t ${DOCKER_IMAGE}
|
||||||
|
|
||||||
|
run:
|
||||||
|
@echo "Running container..."
|
||||||
|
docker run --rm -t ${DOCKER_IMAGE}
|
||||||
|
|
||||||
|
.PHONY: build
|
13
examples/excel2json/data/mapper.json
Normal file
13
examples/excel2json/data/mapper.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"dataIndex": "Name",
|
||||||
|
"label": "Name"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"dataIndex": "Age",
|
||||||
|
"label": "Age"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"configs": {}
|
||||||
|
}
|
BIN
examples/excel2json/data/people.xlsx
Normal file
BIN
examples/excel2json/data/people.xlsx
Normal file
Binary file not shown.
51
examples/excel2json/index.js
Normal file
51
examples/excel2json/index.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
const XLSX = require("xlsx");
|
||||||
|
const fs = require("fs");
|
||||||
|
|
||||||
|
const NAME = process.env.APP_NAME || "excel2json";
|
||||||
|
console.log("App Name =>", NAME);
|
||||||
|
|
||||||
|
// load from env
|
||||||
|
const INPUT_FILE = process.env.INPUT_FILE || "./data/people.xlsx";
|
||||||
|
const OUTPUT_PATH = process.env.OUTPUT_PATH || "./data/outputs";
|
||||||
|
const MAPPER_FILE = process.env.MAPPER_FILE || "./data/mapper.json";
|
||||||
|
|
||||||
|
// get mapper in string
|
||||||
|
const mapperString = fs.readFileSync(
|
||||||
|
MAPPER_FILE,
|
||||||
|
{ encoding: "utf-8" },
|
||||||
|
(err) => {
|
||||||
|
if (err) throw err;
|
||||||
|
console.log("Load file suceed =>", MAPPER_FILE);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// convert mapper from string to json object
|
||||||
|
const mapperJson = JSON.parse(mapperString);
|
||||||
|
const columsData = mapperJson.data;
|
||||||
|
|
||||||
|
// read workbook from excel file
|
||||||
|
const wb = XLSX.readFile(INPUT_FILE);
|
||||||
|
const xlData = XLSX.utils.sheet_to_json(wb.Sheets["Sheet1"]);
|
||||||
|
|
||||||
|
// mapping the data from read excel file
|
||||||
|
const data = xlData.map((row) => {
|
||||||
|
var _r = {};
|
||||||
|
|
||||||
|
columsData.map((col) => {
|
||||||
|
_r[col.label] = row[col.dataIndex];
|
||||||
|
});
|
||||||
|
|
||||||
|
return _r;
|
||||||
|
});
|
||||||
|
console.log("Data read from excel and mapped\n", data);
|
||||||
|
|
||||||
|
// check directory and create it if not exist
|
||||||
|
if (!fs.existsSync(OUTPUT_PATH)) {
|
||||||
|
fs.mkdirSync(OUTPUT_PATH);
|
||||||
|
}
|
||||||
|
|
||||||
|
// write to file
|
||||||
|
fs.writeFile(`${OUTPUT_PATH}/people_${new Date().getTime()}.json`, JSON.stringify(data), (err) => {
|
||||||
|
if (err) throw err;
|
||||||
|
console.log("Write succeed!");
|
||||||
|
});
|
14
examples/excel2json/package.json
Normal file
14
examples/excel2json/package.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"name": "excel2json",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "Sambo Chea <sombochea@cubetiqs.com>",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"xlsx": "^0.16.9"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user