Updated the custom configs for mapper
This commit is contained in:
parent
53e7af0f20
commit
d5c75e8644
@ -1,17 +1,22 @@
|
|||||||
# Excel 2 Json
|
# excel2json
|
||||||
|
|
||||||
- Read file excel to json
|
- Read file excel to json
|
||||||
- Custom mapping with custom columns
|
- Custom mapping with custom columns and configs
|
||||||
|
|
||||||
# Build
|
# Build
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
bash build
|
bash build
|
||||||
```
|
```
|
||||||
|
|
||||||
OR
|
OR
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
make build run
|
make build run
|
||||||
```
|
```
|
||||||
|
|
||||||
# Example
|
# Example
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
docker run -v /my/path:/app/data --rm -it cubetiq/node-excel2json
|
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
|
# Mapper Config
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"data": [
|
"data": [
|
||||||
@ -33,11 +39,16 @@ docker run -v /home/sombochea/excel2json:/app/data -e APP_NAME="EXCEL 2 JSON" -e
|
|||||||
"label": "Age"
|
"label": "Age"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"configs": {}
|
"configs": {
|
||||||
|
"outputPath": "./data/outputs/exported",
|
||||||
|
"outputName": "my_exported_data",
|
||||||
|
"sheetName": "Sheet1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
# Environment
|
# Environment
|
||||||
|
|
||||||
```env
|
```env
|
||||||
APP_NAME=custom app name
|
APP_NAME=custom app name
|
||||||
INPUT_FILE=./data/mydata.xlsx
|
INPUT_FILE=./data/mydata.xlsx
|
||||||
|
@ -9,5 +9,9 @@
|
|||||||
"label": "Age"
|
"label": "Age"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"configs": {}
|
"configs": {
|
||||||
|
"outputPath": "./data/outputs/exported",
|
||||||
|
"outputName": "my_exported_data",
|
||||||
|
"sheetName": "Sheet1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,29 +8,37 @@ console.log("App Name =>", NAME);
|
|||||||
const INPUT_FILE = process.env.INPUT_FILE || "./data/people.xlsx";
|
const INPUT_FILE = process.env.INPUT_FILE || "./data/people.xlsx";
|
||||||
const OUTPUT_PATH = process.env.OUTPUT_PATH || "./data/outputs";
|
const OUTPUT_PATH = process.env.OUTPUT_PATH || "./data/outputs";
|
||||||
const MAPPER_FILE = process.env.MAPPER_FILE || "./data/mapper.json";
|
const MAPPER_FILE = process.env.MAPPER_FILE || "./data/mapper.json";
|
||||||
const SHEET_NAME = process.env.SHEET_NAME || "Sheet1"
|
const SHEET_NAME = process.env.SHEET_NAME || "Sheet1";
|
||||||
const ENCODING = process.env.ENCODING || "utf-8"
|
const ENCODING = process.env.ENCODING || "utf-8";
|
||||||
|
|
||||||
// get mapper in string
|
// get mapper in string
|
||||||
const mapperString = fs.readFileSync(
|
var mapperString = undefined;
|
||||||
MAPPER_FILE,
|
try {
|
||||||
{ encoding: ENCODING },
|
mapperString = fs.readFileSync(MAPPER_FILE, { encoding: ENCODING }, (err) => {
|
||||||
(err) => {
|
if (err) {
|
||||||
if (err) throw err;
|
console.error(err);
|
||||||
console.log("Load file suceed =>", MAPPER_FILE);
|
}
|
||||||
|
console.log("Load file suceed =>", MAPPER_FILE);
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.error("read file error", err)
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
// convert mapper from string to json object
|
// convert mapper from string to json object
|
||||||
const mapperJson = JSON.parse(mapperString);
|
const mapperJson = mapperString ? JSON.parse(mapperString) : {};
|
||||||
const columsData = mapperJson.data;
|
const configs = mapperJson.configs || {};
|
||||||
|
const columsData = mapperJson.data || undefined;
|
||||||
|
|
||||||
// read workbook from excel file
|
// read workbook from excel file
|
||||||
const wb = XLSX.readFile(INPUT_FILE);
|
const wb = XLSX.readFile(configs.inputFile || INPUT_FILE);
|
||||||
const xlData = XLSX.utils.sheet_to_json(wb.Sheets[SHEET_NAME]);
|
const xlData = XLSX.utils.sheet_to_json(wb.Sheets[configs.sheetName || SHEET_NAME]);
|
||||||
|
|
||||||
// mapping the data from read excel file
|
// mapping the data from read excel file
|
||||||
const data = xlData.map((row) => {
|
const data = xlData.map((row) => {
|
||||||
|
if (columsData == undefined || !columsData) {
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
var _r = {};
|
var _r = {};
|
||||||
|
|
||||||
columsData.map((col) => {
|
columsData.map((col) => {
|
||||||
@ -41,13 +49,20 @@ const data = xlData.map((row) => {
|
|||||||
});
|
});
|
||||||
console.log("Data read from excel and mapped\n", data);
|
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
|
// check directory and create it if not exist
|
||||||
if (!fs.existsSync(OUTPUT_PATH)) {
|
if (!fs.existsSync(outputPath)) {
|
||||||
fs.mkdirSync(OUTPUT_PATH);
|
fs.mkdirSync(outputPath, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
// write to file
|
// 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;
|
if (err) throw err;
|
||||||
console.log("Write succeed!");
|
console.log("Write succeed!");
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user