From e1ebbf67d8f20da8d8cc1010dc6f9a4de4b12e4f Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Tue, 30 Mar 2021 11:47:54 +0700 Subject: [PATCH] Fixed the ouput and variable and updated function --- data/mapper.json | 19 +------------------ excel2json.js | 12 ++++++++---- index.js | 4 +++- package.json | 2 +- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/data/mapper.json b/data/mapper.json index 80ff8e5..9e26dfe 100644 --- a/data/mapper.json +++ b/data/mapper.json @@ -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 - } -} +{} \ No newline at end of file diff --git a/excel2json.js b/excel2json.js index 7d5f5ef..a852e9a 100644 --- a/excel2json.js +++ b/excel2json.js @@ -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 = @@ -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] ); diff --git a/index.js b/index.js index 4bca17e..9afff12 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/package.json b/package.json index f636b4f..58e0fdc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "excel2json-xlsx", - "version": "1.0.1", + "version": "1.0.2", "keywords": [ "excel", "json",