I have excel sheet with list of data.
- Reading excel data
- searching excel data against another system using API
- Taking top result data and converting to csv file.
Upto this step working good. But after that I need to format the data in csv file like excel data and search results both have to display in csv file.
Here i'm not able to bring excel data into csv file.For example "Honda" is car name in excel file and i'm reading it and searching against another system.Those results need to be display in csv.
Please advise.
Excel input:
Car name, Description
Honda, some description
API response data:
[{
"total": 10,
"results": [{
"name": {
"val": "Honda",
"id": "0271b276",
"type": "String",
},
"attributes": [{
"val": "accord",
"type": "field1",
}, {
"val": "test123",
"type": "field3",
}],
}]
},
]
Expectation output in the CSV file.
Car Name , Description,Total results,Make , Model
honda , Description,10 , Honda, accord
Code
const _ = require('lodash');
const xlsx = require('xlsx');
const workbook = xlsx.readFile(__dirname + '/test.xlsx');
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
for (let z in worksheet) {
if(z.toString()[0] === 'A'){
request({
url: 'http://url', //URL to hit
method: 'POST',
json: {
query: worksheet[z].v,
}
}, function(error, response, data){
if(error) {
console.log(error);
} else {
var fields = ['Make','Model','total', 'results[0].name.val','results[0].name[0].val'];
var fieldNames = ['Make','Model','Total','Name','Description'];
var opts1 = {
data: data,
fields: fields,
fieldNames: fieldNames,
};
var json2csv = require('json2csv');
var csv = json2csv(opts1);
fs.writeFile('file.csv', csv, function(err) {
if (err) throw err;
console.log('file saved');
});
'in yourvar fieldnames