I tried on converting my xl to json. Conversion is happening well, but though I have written code for conversion to lowercase and trim, its not happening. I am getting the output as below.. but I am expecting the output all in lowercase and trimmed..can someone help
var exceltojson = require("xlsx-to-json");
exceltojson(
{
input: "test.xlsx",
output: 'test.txt',
sheet: "Sheet1",
}, function(err, result)
{
if(err)
{
console.error(err);
}
});
output:
[
{
Email: '[email protected]',
'First Name': 'a',
'Middle Name': 'b',
'Last Name': 'c',
'Address 1': 'd',
'Address 2': 'lol',
'Phone No': '123456789'
},
{
Email: '[email protected]',
'First Name': 'g',
'Middle Name': 'h',
'Last Name': 'i',
'Address 1': 'j',
'Address 2': 'lol',
'Phone No': '458745'
}
]
I expect output like
[
{
email: '[email protected]',
firstname: 'a',
middlename: 'b',
lastname: 'c',
'address 1': 'd',
'address 2': 'lol',
phoneno: '123456789',
},
{
email: '[email protected]',
firstname: 'g',
middlename: 'h',
lastname: 'i',
address1: 'j',
'address 2': 'lol',
phoneno: '458745',
},
];
The library doesn't have any options such as trim/tolowercase. You have to do it manually.
const exceltojson = require('xlsx-to-json');
const fs = require('fs');
exceltojson({
input: 'test.xlsx',
// output: 'test.txt', Don't need output
sheet: 'Sheet1'
},
function(err, result) {
if (err) {
console.error(err);
return;
}
const newResult = result.map(obj => {
const newObj = Object.keys(obj).reduce((acc, key) => {
const newKey = key.replace(/ /g, '').toLowerCase();
acc[newKey] = obj[key];
return acc;
}, {});
return newObj;
});
fs.writeFileSync('file.txt', JSON.stringify(newResult));
}
);