Updated the custom configs for mapper

This commit is contained in:
Sambo Chea 2021-03-29 19:27:06 +07:00
parent 53e7af0f20
commit d5c75e8644
3 changed files with 53 additions and 23 deletions

View File

@ -1,17 +1,22 @@
# Excel 2 Json
# excel2json
- Read file excel to json
- Custom mapping with custom columns
- 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
```
@ -21,6 +26,7 @@ docker run -v /home/sombochea/excel2json:/app/data -e APP_NAME="EXCEL 2 JSON" -e
```
# Mapper Config
```json
{
"data": [
@ -33,11 +39,16 @@ docker run -v /home/sombochea/excel2json:/app/data -e APP_NAME="EXCEL 2 JSON" -e
"label": "Age"
}
],
"configs": {}
"configs": {
"outputPath": "./data/outputs/exported",
"outputName": "my_exported_data",
"sheetName": "Sheet1"
}
}
```
# Environment
```env
APP_NAME=custom app name
INPUT_FILE=./data/mydata.xlsx

View File

@ -9,5 +9,9 @@
"label": "Age"
}
],
"configs": {}
"configs": {
"outputPath": "./data/outputs/exported",
"outputName": "my_exported_data",
"sheetName": "Sheet1"
}
}

View File

@ -8,29 +8,37 @@ console.log("App Name =>", NAME);
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"
const SHEET_NAME = process.env.SHEET_NAME || "Sheet1";
const ENCODING = process.env.ENCODING || "utf-8";
// get mapper in string
const mapperString = fs.readFileSync(
MAPPER_FILE,
{ encoding: ENCODING },
(err) => {
if (err) throw err;
console.log("Load file suceed =>", MAPPER_FILE);
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 = JSON.parse(mapperString);
const columsData = mapperJson.data;
const mapperJson = mapperString ? JSON.parse(mapperString) : {};
const configs = mapperJson.configs || {};
const columsData = mapperJson.data || undefined;
// read workbook from excel file
const wb = XLSX.readFile(INPUT_FILE);
const xlData = XLSX.utils.sheet_to_json(wb.Sheets[SHEET_NAME]);
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) => {
@ -41,13 +49,20 @@ const data = xlData.map((row) => {
});
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(OUTPUT_PATH)) {
fs.mkdirSync(OUTPUT_PATH);
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
// write to file
fs.writeFile(`${OUTPUT_PATH}/people_${new Date().getTime()}.json`, JSON.stringify(data), (err) => {
fs.writeFileSync(
`${outputPath}/${configs.outputName || 'exported'}_${new Date().getTime()}.json`,
JSON.stringify(data),
(err) => {
if (err) throw err;
console.log("Write succeed!");
});
}
);