convert JSON to Excel in node - node.js

I have a json response containing inner json content. I've added a library of json2xls and converted my json to excel. But, on the excel file in the address column, I'm getting [object object]. Is there any way where I could able to get those inner json value on excel?
var option = [{id: 1, name: "jason", address: [{city: "XYZ", state: "ABC"}]}]
var json2xls = require('json2xls');
var xls = json2xls(option);
fs.writeFileSync('data-report.xlsx', xls, 'binary');

Related

How to read and write in excel file using nodejs?

I am using sheetjs for reading and writing purpose.
const data = {
'col1': 2,
'col2': 3,
'col3': ['1','2','3','4'],
'col4': {'test': '1'}
}
const worksheet = XLSX.utils.json_to_sheet(data);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, `${tableName}`);
const file = XLSX.write(workbook, {
type: "buffer",
bookType: "xlsx",
bookSST: false
});
JSON and Array values are not getting added to file.
When I try to stringify JSON and array on reading the file, I am getting stringify value.
As my method is generic so JSON parse can't be done on columns individually.
Worksheet is getting created like this => t: 's", values: "{}"
Reading file like
const file = await this.s3Service.getFile(`${tableName}.xlsx`, uuid)
const workbook = XLSX.read(file.Body, {cellDates: true})
const sheet = workbook.Sheets[`${tableName}`]
let sheetData = XLSX.utils.sheet_to_json(sheet, {defval: null})
return sheetData
How can i store json and array as object

HOW READ METADATA IN EXCEL SHAREPOINT FILE (JSON)

I got the data from the sharepoint but can't read the tables or the data because in JSON how can I get the value inside the metadata
spauth.getAuth(url, {
username:username,
password:password
})
.then(function(options){
console.log(listTitle)
var headers = options.headers;
headers['Accept'] = 'application/json;odata=verbose';
// Pull the SharePoint list items
requestprom.get({
url: url+"/_api/web/lists/getbytitle('Documents')/items(xxxx)",
headers: headers,
json: true
}).then(function(listresponse){
// var items = listresponse.d;
var items = listresponse;
var responseJSON = [];
console.log(items);
Code
Data get
For SharePoint Online, we could use Graph API to get the value of the excel.
Try this endpoint:
https://graph.microsoft.com/v1.0/sites/domain.sharepoint.com,id,id/drives/drive id/items/item id/workbook
You could take a look at the following blog:
Accessing the Excel file content
Test result:
Official document for your reference:
Get Worksheet

SheetJS How to create sheet from json and save it as buffer

Documentations seems confusing to me. How can i create a document from a array of objects, each object representing a row and save it as buffer?
You can use the write function and pass the WritingOptions object to set the type of your workbook data.
Example:
const workbook = XLSX.utils.book_new()
const filename = 'mySheet'
const dataSheet = XLSX.utils.json_to_sheet(myJSONData)
XLSX.utils.book_append_sheet(workbook, dataSheet, filename.replace('/', ''))
and return for your controller, send it to the network, etc...
return XLSX.write(workbook, { type: 'buffer', bookType: 'csv' })

How to read images from excel sheet(have multiple worksheets) using nodejs?

I need to load .xlsx in node and angularjs app.
i have used some packages for that purpose like js-xlsx it give me a all the data, but I can't find images of the excel sheets nor in JSON neither in HTML generated by js-xlsx package.
var wb = XLSX.read(data.data, {
type: "array"
});
let sheets = [];
for (const key in wb.Sheets) {
sheets.push({
paneId: key.replace(/ /g, '').toLowerCase(),
title: key,
active: false,
disabled: false,
data: $scope.processData(XLSX.utils.sheet_to_csv(wb.Sheets[key]))
})
}
I have also tried.
XLSX.utils.sheet_to_json(wb.Sheets[key])
XLSX.utils.sheet_to_html(wb.Sheets[key])
$scope.processData is the function for converting csv to array.

Writes [object Object] to csv file using fast-csv

I´m using fast-csv to export some data from a DB to a CSV file.
When I use the code from the example in the docs :
var csvStream = csv.createWriteStream({headers:true}),
writableStream = fs.createWriteStream('./csv/list.csv');
writableStream.on('finish', function(){
console.log('DONE!');
});
csvStream.pipe(writableStream);
csvStream.write(
[
{
a: "a1",
b: "b1",
c: "c1"
}
]
);
csvStream.end();
res.send('export done!')
my csv file have one entry [object Object]
It looks like csvStream.write() will only accept object arguments:
csvStream.write({
a: "a1",
b: "b1",
c: "c1"
});
If you want to write arrays, you should use csv.write() or csv.writeToStream() (documented here, search for "Writing data" as I can't link to it directly).

Resources