Skip to content

Commit

Permalink
Fixed the ouput and variable and updated function
Browse files Browse the repository at this point in the history
  • Loading branch information
sombochea committed Mar 30, 2021
1 parent 201be21 commit e1ebbf6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
19 changes: 1 addition & 18 deletions data/mapper.json
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
{
"data": [
{
"dataIndex": "Name",
"label": "Name"
},
{
"dataIndex": "Age",
"label": "Age"
}
],
"configs": {
"outputPath": "./data/outputs/exported",
"outputName": "my_exported_data",
"sheetName": "Sheet1",
"saveToOutput": true
}
}
{}
12 changes: 8 additions & 4 deletions excel2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ function _internalExport(props = {}) {
console.log("Name =>", NAME, "\n");

// load from env
const INPUT_FILE =
props.inputFile || process.env.INPUT_FILE || "./data/people.xlsx";
const INPUT_FILE = props.inputFile || process.env.INPUT_FILE;
const OUTPUT_PATH =
props.outputPath || process.env.OUTPUT_PATH || "./data/outputs";
const MAPPER_FILE =
Expand All @@ -31,16 +30,21 @@ function _internalExport(props = {}) {
}
);
} catch (err) {
console.error("read file error", err);
// console.error("read file error", err);
}

// convert mapper from string to json object
const mapperJson = mapperString ? JSON.parse(mapperString) : {};
const configs = { ...mapperJson.configs, ...props };
const columsData = props.mappings || mapperJson.data || undefined;
const inFile = configs.inputFile || INPUT_FILE;

if (!inFile) {
throw Error("Input file is required!");
}

// read workbook from excel file
const wb = XLSX.readFile(configs.inputFile || INPUT_FILE);
const wb = XLSX.readFile(inFile);
const xlData = XLSX.utils.sheet_to_json(
wb.Sheets[configs.sheetName || SHEET_NAME]
);
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
const excel2json = require("./excel2json");

// called function export excel2json
const exported = excel2json();
const exported = excel2json({
inputFile: "./data/people.xlsx"
});

// output data from exported
console.log("Output =>\n", exported);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "excel2json-xlsx",
"version": "1.0.1",
"version": "1.0.2",
"keywords": [
"excel",
"json",
Expand Down

0 comments on commit e1ebbf6

Please sign in to comment.