Add custom sheet and encoding

This commit is contained in:
Sambo Chea 2021-03-29 18:35:49 +07:00
parent 4d07c0e5fa
commit f696a468d6
2 changed files with 6 additions and 2 deletions

View File

@ -26,4 +26,6 @@ APP_NAME=custom app name
INPUT_FILE=./data/mydata.json INPUT_FILE=./data/mydata.json
OUTPUT_PATH=./data/outputs OUTPUT_PATH=./data/outputs
MAPPER_FILE=./data/mapper.json MAPPER_FILE=./data/mapper.json
SHEET_NAME=Sheet1
ENCODING=utf-8
``` ```

View File

@ -8,11 +8,13 @@ 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 ENCODING = process.env.ENCODING || "utf-8"
// get mapper in string // get mapper in string
const mapperString = fs.readFileSync( const mapperString = fs.readFileSync(
MAPPER_FILE, MAPPER_FILE,
{ encoding: "utf-8" }, { encoding: ENCODING },
(err) => { (err) => {
if (err) throw err; if (err) throw err;
console.log("Load file suceed =>", MAPPER_FILE); console.log("Load file suceed =>", MAPPER_FILE);
@ -25,7 +27,7 @@ const columsData = mapperJson.data;
// read workbook from excel file // read workbook from excel file
const wb = XLSX.readFile(INPUT_FILE); const wb = XLSX.readFile(INPUT_FILE);
const xlData = XLSX.utils.sheet_to_json(wb.Sheets["Sheet1"]); const xlData = XLSX.utils.sheet_to_json(wb.Sheets[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) => {