Add example for excel to json with mapper from file and add docker support

This commit is contained in:
Sambo Chea 2021-03-29 17:32:54 +07:00
parent 481c916179
commit 83431e02a9
8 changed files with 110 additions and 1 deletions

View File

@ -1,2 +1,3 @@
# 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
View File

@ -0,0 +1,5 @@
node_modules/
yarn.lock
package-lock.json
yarn-error*
outputs/

View 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"]

View 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

View File

@ -0,0 +1,13 @@
{
"data": [
{
"dataIndex": "Name",
"label": "Name"
},
{
"dataIndex": "Age",
"label": "Age"
}
],
"configs": {}
}

Binary file not shown.

View 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!");
});

View 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"
}
}