Compare commits

18 Commits

Author SHA1 Message Date
ec41473af3 Add node version 16.5 and yarn 1.22.15 2021-12-23 08:56:19 +07:00
Sambo Chea
db3b875005 Update docker-publish.yml 2021-09-26 18:51:12 +07:00
Sambo Chea
82b78116e9 Update docker-publish.yml 2021-07-14 15:04:32 +07:00
9987810cca Updated docker buiuld action 2021-03-30 09:31:13 +07:00
f8d8671ef2 Updated package json 2021-03-30 09:30:56 +07:00
c6ecb4cc4b Add mapper config for save to output 2021-03-29 19:48:59 +07:00
af96b1c305 Add function for excel2json for exported with file and data json object 2021-03-29 19:47:35 +07:00
d5c75e8644 Updated the custom configs for mapper 2021-03-29 19:27:06 +07:00
Sambo Chea
53e7af0f20 Update README.md 2021-03-29 19:05:12 +07:00
Sambo Chea
336f5b69a5 Update README.md 2021-03-29 19:03:57 +07:00
f696a468d6 Add custom sheet and encoding 2021-03-29 18:35:49 +07:00
4d07c0e5fa Updated the readme for excel to json 2021-03-29 17:57:52 +07:00
4bc23af526 Updated workdir 2021-03-29 17:50:02 +07:00
418adc0abd Updated the script and volumn for container 2021-03-29 17:46:28 +07:00
83431e02a9 Add example for excel to json with mapper from file and add docker support 2021-03-29 17:32:54 +07:00
481c916179 Updated readme 2021-03-29 15:30:14 +07:00
b459c402a5 Merge branch 'master' of https://git.cubetiqs.com/CUBETIQ/calpine-node 2021-03-29 15:29:01 +07:00
4f71406de2 Updated the build 2021-03-29 15:26:49 +07:00
15 changed files with 258 additions and 6 deletions

View File

@@ -2,8 +2,7 @@ name: Docker CI
on: on:
push: push:
branches: branches: [ main ]
- 'master'
jobs: jobs:
docker: docker:
@@ -21,7 +20,6 @@ jobs:
- -
name: Checkout name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- -
name: Build and Push from Makefile name: Build and Push from Makefile
run: | run: |

View File

@@ -4,7 +4,7 @@ LABEL maintainer="sombochea@cubetiqs.com"
# Build with root access # Build with root access
USER root USER root
ENV NODE_VERSION 14.16.0 ENV NODE_VERSION 16.5.0
ENV USER_UID 1001 ENV USER_UID 1001
RUN addgroup -g ${USER_UID} node \ RUN addgroup -g ${USER_UID} node \
@@ -77,7 +77,7 @@ RUN addgroup -g ${USER_UID} node \
&& node --version \ && node --version \
&& npm --version && npm --version
ENV YARN_VERSION 1.22.5 ENV YARN_VERSION 1.22.15
RUN apk add --no-cache --virtual .build-deps-yarn curl gnupg tar \ RUN apk add --no-cache --virtual .build-deps-yarn curl gnupg tar \
&& for key in \ && for key in \

View File

@@ -15,6 +15,20 @@ docker push cubetiq/calpine-node:14
docker run --rm -it cubetiq/calpine-node:14 /bin/sh docker run --rm -it cubetiq/calpine-node:14 /bin/sh
``` ```
# Example
```Dockerfile
FROM cubetiq/calpine-node:latest
LABEL maintainer="sombochea@cubetiqs.com"
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN yarn
CMD [ "node" , "index.js"]
```
# Contributors # Contributors
- Sambo Chea <sombochea@cubetiqs.com> - Sambo Chea <sombochea@cubetiqs.com>

View File

@@ -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
View File

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

View File

@@ -0,0 +1,16 @@
FROM cubetiq/calpine-node:latest
LABEL maintainer="sombochea@cubetiqs.com"
VOLUME [ "/app/data" ]
ENV INPUT_FILE './data/people.xlsx'
ENV OUTPUT_PATH './data/outputs'
ENV MAPPER_FILE './data/mapper.json'
WORKDIR /app
COPY . /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,60 @@
# excel2json
- Read file excel to json
- Custom mapping with custom columns and configs
# Build
```shell
bash build
```
OR
```shell
make build run
```
# Example
```shell
docker run -v /my/path:/app/data --rm -it cubetiq/node-excel2json
```
```shell
docker run -v /home/sombochea/excel2json:/app/data -e APP_NAME="EXCEL 2 JSON" -e MAPPER_FILE="./data/mapper.json" --rm -it cubetiq/node-excel2json
```
# Mapper Config
```json
{
"data": [
{
"dataIndex": "Name",
"label": "Name"
},
{
"dataIndex": "Age",
"label": "Age"
}
],
"configs": {
"outputPath": "./data/outputs/exported",
"outputName": "my_exported_data",
"sheetName": "Sheet1",
"saveToOutput": true
}
}
```
# Environment
```env
APP_NAME=custom app name
INPUT_FILE=./data/mydata.xlsx
OUTPUT_PATH=./data/outputs
MAPPER_FILE=./data/mapper.json
SHEET_NAME=Sheet1
ENCODING=utf-8
```

View File

@@ -0,0 +1,3 @@
#!/bin/bash
make build run

View File

@@ -0,0 +1,18 @@
{
"data": [
{
"dataIndex": "Name",
"label": "Name"
},
{
"dataIndex": "Age",
"label": "Age"
}
],
"configs": {
"outputPath": "./data/outputs/exported",
"outputName": "my_exported_data",
"sheetName": "Sheet1",
"saveToOutput": true
}
}

Binary file not shown.

View File

@@ -0,0 +1,100 @@
const XLSX = require("xlsx");
const fs = require("fs");
// do export for excel to json output or data json object
function _internalExport() {
const NAME = process.env.APP_NAME || "excel2json";
console.log("APP NAME =>", NAME, "\n");
// 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";
const SHEET_NAME = process.env.SHEET_NAME || "Sheet1";
const ENCODING = process.env.ENCODING || "utf-8";
// get mapper in string
var mapperString = undefined;
try {
mapperString = fs.readFileSync(
MAPPER_FILE,
{ encoding: ENCODING },
(err) => {
if (err) {
console.error(err);
}
console.log("Load file suceed =>", MAPPER_FILE);
}
);
} catch (err) {
console.error("read file error", err);
}
// convert mapper from string to json object
const mapperJson = mapperString ? JSON.parse(mapperString) : {};
const configs = mapperJson.configs || {};
const columsData = mapperJson.data || undefined;
// read workbook from excel file
const wb = XLSX.readFile(configs.inputFile || INPUT_FILE);
const xlData = XLSX.utils.sheet_to_json(
wb.Sheets[configs.sheetName || SHEET_NAME]
);
// mapping the data from read excel file
const data = xlData.map((row) => {
if (columsData == undefined || !columsData) {
return row;
}
var _r = {};
columsData.map((col) => {
_r[col.label] = row[col.dataIndex];
});
return _r;
});
// parse a new path
const outputPath = configs.outputPath || OUTPUT_PATH;
// check directory and create it if not exist
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
// able to save to output or not (default is true)
const saveToOutput = configs.saveToOutput || true;
if (saveToOutput) {
// json data output
const jsonStringData = JSON.stringify(data);
try {
// write to file
fs.writeFileSync(
`${outputPath}/${
configs.outputName || "exported"
}_${new Date().getTime()}.json`,
jsonStringData,
(err) => {
if (err) throw err;
console.log("Write succeed!");
}
);
} catch (err) {}
}
return data;
}
/**
* excel2json
* Allow to export data from excel to json object or output of json.
*
* @returns JSON Object of result
*/
module.exports = excel2json = () => {
return _internalExport()
}

View File

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

View File

@@ -0,0 +1,15 @@
{
"name": "excel2json",
"version": "1.0.0",
"description": "Excel to json",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Sambo Chea <sombochea@cubetiqs.com>",
"license": "MIT",
"dependencies": {
"xlsx": "^0.16.9"
}
}

View File

@@ -0,0 +1,3 @@
#!/bin/bash
make build run