2021-03-29 17:32:54 +07:00
|
|
|
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";
|
2021-03-29 18:35:49 +07:00
|
|
|
const SHEET_NAME = process.env.SHEET_NAME || "Sheet1"
|
|
|
|
const ENCODING = process.env.ENCODING || "utf-8"
|
2021-03-29 17:32:54 +07:00
|
|
|
|
|
|
|
// get mapper in string
|
|
|
|
const mapperString = fs.readFileSync(
|
|
|
|
MAPPER_FILE,
|
2021-03-29 18:35:49 +07:00
|
|
|
{ encoding: ENCODING },
|
2021-03-29 17:32:54 +07:00
|
|
|
(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);
|
2021-03-29 18:35:49 +07:00
|
|
|
const xlData = XLSX.utils.sheet_to_json(wb.Sheets[SHEET_NAME]);
|
2021-03-29 17:32:54 +07:00
|
|
|
|
|
|
|
// 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!");
|
|
|
|
});
|