From 83431e02a9f56fc3fa06250f62dc7df083d9044c Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Mon, 29 Mar 2021 17:32:54 +0700 Subject: [PATCH] Add example for excel to json with mapper from file and add docker support --- examples/README.md | 3 +- examples/excel2json/.gitignore | 5 +++ examples/excel2json/Dockerfile | 14 ++++++++ examples/excel2json/Makefile | 11 ++++++ examples/excel2json/data/mapper.json | 13 +++++++ examples/excel2json/data/people.xlsx | Bin 0 -> 8544 bytes examples/excel2json/index.js | 51 +++++++++++++++++++++++++++ examples/excel2json/package.json | 14 ++++++++ 8 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 examples/excel2json/.gitignore create mode 100644 examples/excel2json/Dockerfile create mode 100644 examples/excel2json/Makefile create mode 100644 examples/excel2json/data/mapper.json create mode 100644 examples/excel2json/data/people.xlsx create mode 100644 examples/excel2json/index.js create mode 100644 examples/excel2json/package.json diff --git a/examples/README.md b/examples/README.md index b0b7c0e..7bb4408 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,2 +1,3 @@ # Examples -- Say Hello to Docker with Nodejs \ No newline at end of file +- Say Hello to Docker with Nodejs +- Excel file transform to json with Nodejs \ No newline at end of file diff --git a/examples/excel2json/.gitignore b/examples/excel2json/.gitignore new file mode 100644 index 0000000..5de57d7 --- /dev/null +++ b/examples/excel2json/.gitignore @@ -0,0 +1,5 @@ +node_modules/ +yarn.lock +package-lock.json +yarn-error* +outputs/ \ No newline at end of file diff --git a/examples/excel2json/Dockerfile b/examples/excel2json/Dockerfile new file mode 100644 index 0000000..09963ad --- /dev/null +++ b/examples/excel2json/Dockerfile @@ -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"] \ No newline at end of file diff --git a/examples/excel2json/Makefile b/examples/excel2json/Makefile new file mode 100644 index 0000000..32140e5 --- /dev/null +++ b/examples/excel2json/Makefile @@ -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 diff --git a/examples/excel2json/data/mapper.json b/examples/excel2json/data/mapper.json new file mode 100644 index 0000000..1995182 --- /dev/null +++ b/examples/excel2json/data/mapper.json @@ -0,0 +1,13 @@ +{ + "data": [ + { + "dataIndex": "Name", + "label": "Name" + }, + { + "dataIndex": "Age", + "label": "Age" + } + ], + "configs": {} +} diff --git a/examples/excel2json/data/people.xlsx b/examples/excel2json/data/people.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..91626964331ee236df50bb7a4e8ad6db20968943 GIT binary patch literal 8544 zcmeHs1y@|j)^-CSxVu|$cY+0XCj@tQ*PxBNy95Xjf(3U8u8q4xAUFhKP{BIRF*_0FVLXgdx%NApn55X8-^O02WeP)ZWg; z)Xqi!wWourvo4c|tqp1ZGf0|T03`VR|F!?(9VknZSLkLz4ZfCICOgBXw9_OF%5do% zL#Iiqtn*=UKGxq0Px3^aDkh6}QDp~e}U*nc` zD3E>%&qn?;eIKgRC((}cmd^U);P>GYqFl=HIcoj5PNv(TvFGk5JH3YS0!i(ba!8AF zkuexFEMiLd+JJqV_^rAeqy_j zpUr*RZIkB{4`h??X768hXNMz^$$lAT&u!B;g_banCR$Jj&3{cPo8Ogfpzp!5&DtW#s5xB$<{fz=KHte5&urZ&#ZOn>zM)5rf}CH&jc z%M#=jyIBxJPNeQb2d-yU;!s6o-G!xE$X@&UOE06;MZbSRu+l+Ii252o5K7Xo&G&w2 zX@x&}e}L?2lcOvW9g~lu-n~2|_0G{1hK|Z9MZ&RcqX*4x=5ppLT~fxA#;q-uuC%c@ zPj+yPLSpJvtO|9ENgW>!qmVEJTOd6^t505Y)##=IVn#&us63>y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=Pl91I+}&(JqHr!aL_88W;fQ{FSWXsSbH|yXIe-0-uODOl?Y{8d z=i$Ijhm+fF0A7iM9*Gmrs!DUPJ5k^9dWRXZ@C-4{-#^ef|gmEFPH5>%{QL4qNX7(cwXZ>TK zWQ&q~x^ts+b)3x2e5MrF+ik=7`vN~HN~~tfqFBD1nVANPe(pdselhVzSHndVGmh0_ zO2GtemD)`7XV(gQHwl@3;4}E0z9gBtktg7p2rz|F|4v^+2Z#T$7rZflwye0H6#<%S zPCO}$j7hmKvN}{eQ7iAKkI1$7WV)sw-y&i_J8qeMuOsQGUx*TAR)}*VE!(c1Q~`Kd zLAup>ue@TiD;VfC{Ng22y!}km?r=NUQyP;j8KB7m>C6~g*i)ynKw{|7WQ3<_NsCnB zkijc%OTfRJ=tlyGY@YNgG+^*Y;)vvd+D>m2Onwp}ID7YaX81~+%Lc^NC+MMmBh!K) zs9aPZ7CIryoKuAYEAep5$`@J@XcC?Ao)162J6zWJqUWY(%D#aK3c=5big@%>-LP8W zr@F_*GhO!mo$xEL75fiKARrmR#{A!F*?(=yU*o@2wv}Z6Dd3+)x4(dYjA`J%{96g` zSKzOOh@a3laJ=+OS>jjlKQp^Oq2Rg(+z;^oC(rw literal 0 HcmV?d00001 diff --git a/examples/excel2json/index.js b/examples/excel2json/index.js new file mode 100644 index 0000000..db488f0 --- /dev/null +++ b/examples/excel2json/index.js @@ -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!"); +}); diff --git a/examples/excel2json/package.json b/examples/excel2json/package.json new file mode 100644 index 0000000..5c8dd88 --- /dev/null +++ b/examples/excel2json/package.json @@ -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 ", + "license": "MIT", + "dependencies": { + "xlsx": "^0.16.9" + } +}