Compare commits
7 Commits
d5c75e8644
...
v14
| Author | SHA1 | Date | |
|---|---|---|---|
|
daef0ef378
|
|||
|
|
db3b875005 | ||
|
|
82b78116e9 | ||
| 9987810cca | |||
| f8d8671ef2 | |||
| c6ecb4cc4b | |||
| af96b1c305 |
4
.github/workflows/docker-publish.yml
vendored
4
.github/workflows/docker-publish.yml
vendored
@@ -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: |
|
||||||
|
|||||||
7
Makefile
7
Makefile
@@ -1,13 +1,10 @@
|
|||||||
DOCKER_IMAGE=cubetiq/calpine-node
|
DOCKER_IMAGE=cubetiq/calpine-node:14
|
||||||
DOCKER_IMAGE_NAME=${DOCKER_IMAGE}:14
|
|
||||||
|
|
||||||
build:
|
build:
|
||||||
@echo 'Starting docker build'
|
@echo 'Starting docker build'
|
||||||
docker build . -t ${DOCKER_IMAGE}
|
docker build . -t ${DOCKER_IMAGE}
|
||||||
|
|
||||||
@echo 'Starting docker push'
|
@echo 'Starting docker push'
|
||||||
docker tag ${DOCKER_IMAGE} ${DOCKER_IMAGE_NAME}
|
docker tag ${DOCKER_IMAGE}
|
||||||
docker push ${DOCKER_IMAGE}
|
docker push ${DOCKER_IMAGE}
|
||||||
docker push ${DOCKER_IMAGE_NAME}
|
|
||||||
|
|
||||||
.PHONY:build
|
.PHONY:build
|
||||||
@@ -42,7 +42,8 @@ docker run -v /home/sombochea/excel2json:/app/data -e APP_NAME="EXCEL 2 JSON" -e
|
|||||||
"configs": {
|
"configs": {
|
||||||
"outputPath": "./data/outputs/exported",
|
"outputPath": "./data/outputs/exported",
|
||||||
"outputName": "my_exported_data",
|
"outputName": "my_exported_data",
|
||||||
"sheetName": "Sheet1"
|
"sheetName": "Sheet1",
|
||||||
|
"saveToOutput": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
"configs": {
|
"configs": {
|
||||||
"outputPath": "./data/outputs/exported",
|
"outputPath": "./data/outputs/exported",
|
||||||
"outputName": "my_exported_data",
|
"outputName": "my_exported_data",
|
||||||
"sheetName": "Sheet1"
|
"sheetName": "Sheet1",
|
||||||
|
"saveToOutput": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
100
examples/excel2json/excel2json.js
Normal file
100
examples/excel2json/excel2json.js
Normal 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()
|
||||||
|
}
|
||||||
@@ -1,68 +1,8 @@
|
|||||||
const XLSX = require("xlsx");
|
// import excel2json module
|
||||||
const fs = require("fs");
|
const excel2json = require("./excel2json");
|
||||||
|
|
||||||
const NAME = process.env.APP_NAME || "excel2json";
|
// called function export excel2json
|
||||||
console.log("App Name =>", NAME);
|
const exported = excel2json();
|
||||||
|
|
||||||
// load from env
|
// output data from exported
|
||||||
const INPUT_FILE = process.env.INPUT_FILE || "./data/people.xlsx";
|
console.log("Output =>\n", exported);
|
||||||
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;
|
|
||||||
});
|
|
||||||
console.log("Data read from excel and mapped\n", data);
|
|
||||||
|
|
||||||
// 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 });
|
|
||||||
}
|
|
||||||
|
|
||||||
// write to file
|
|
||||||
fs.writeFileSync(
|
|
||||||
`${outputPath}/${configs.outputName || 'exported'}_${new Date().getTime()}.json`,
|
|
||||||
JSON.stringify(data),
|
|
||||||
(err) => {
|
|
||||||
if (err) throw err;
|
|
||||||
console.log("Write succeed!");
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "excel2json",
|
"name": "excel2json",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "Excel to json",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"start": "node index.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"author": "Sambo Chea <sombochea@cubetiqs.com>",
|
"author": "Sambo Chea <sombochea@cubetiqs.com>",
|
||||||
|
|||||||
Reference in New Issue
Block a user