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 0000000..9162696 Binary files /dev/null and b/examples/excel2json/data/people.xlsx differ 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" + } +}