question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

From json to sheet.

See original GitHub issue

Hi guys, thanks for the wonderful work on this module.

Some quick questions:

  1. How to iterate on the sheet’s rows? Right now i’m using the sheet_to_json utility…
  2. Is there a way to get a sheet from the JSON? Like a json_to_sheet utility. To be able to write back the changed xls to the file.

Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:21 (8 by maintainers)

github_iconTop GitHub Comments

21reactions
jomelcommented, May 26, 2017

@samuelkavin I use something like this to create a xlsx file from json and send it as a response to a web request:


const XLSX = require('xlsx');
data = [ { "agentNo":"324234", "subName":"30, Jul 2013 09:24 AM" }, { "agentNo":"444443", "subName":"30, Jul 2013 09:24 AM" } ];

/* create workbook & set props*/
   const wb = { SheetNames: [], Sheets: {} };
   wb.Props = {
      Title: "Stats from app",
      Author: "John Doe"
   };

/*create sheet data & add to workbook*/
var ws = XLSX.utils.json_to_sheet(data);
var ws_name = "DataSheet 1";
XLSX.utils.book_append_sheet(wb, ws, ws_name);

/* create file 'in memory' */
var wbout = new Buffer(XLSX.write(wb, { bookType: 'xlsx', type: 'buffer' }));

/* send it by web request - where app is express()*/
app.get('/api/jobs/download', (req, res) => {
   var filename = "myDataFile.xlsx";
   res.setHeader('Content-Disposition', 'attachment; filename=' + filename);
   res.type('application/octet-stream');
   res.send(wbout);
}

4reactions
SheetJSDevcommented, Mar 30, 2017

@roccomuso Here’s a simple function, we should add a more refined version of this to the library in the next release:

function json_to_sheet(js/*:Array<any>*/, opts)/*:Worksheet*/ {
	var o = opts || {};
	var ws = ({}/*:any*/);
	var range/*:Range*/ = ({s: {c:0, r:0}, e: {c:0, r:js.length}}/*:any*/);
	var hdr = o.header || [], C = 0;

	for(var R = 0; R != js.length; ++R) {
		Object.keys(js[R]).filter(function(x) { return js[R].hasOwnProperty(x); }).forEach(function(k) {
			if((C=hdr.indexOf(k)) == -1) hdr[C=hdr.length] = k;
			var v = js[R][k];
			var t = 'z';
			if(typeof v == 'number') t = 'n';
			else if(typeof v == 'boolean') t = 'b';
			ws[XLSX.utils.encode_cell({c:C,r:R+1})] = {t:t, v:v};
		});
	}
	range.e.c = hdr.length - 1;
	for(C = 0; C < hdr.length; ++C) ws[XLSX.utils.encode_col(C) + "1"] = {t:'s', v:hdr[C]};
	ws['!ref'] = XLSX.utils.encode_range(range);
	return ws;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

3 Best Ways To Import JSON To Google Sheets [Ultimate Guide]
1.) Code a JSON Importer Yourself · Open a Google Sheet · In the menu at the top of the Google Sheets, click...
Read more >
ImportJSON | Import JSON data into Google Sheets
Import JSON data from any API and convert it into an easy-to-read table. All through a simple function!
Read more >
The Easiest Google Sheets Import JSON Guide for 2023 | SSP
JSON is an acronym that stands for JavaScript Object Notation, and it is a text format used to store and transport data. People...
Read more >
How to import JSON data into a Google Sheet - Geckoboard
While Google Sheets offers a built-in =ImportData() function that can retrieve JSON data, we recommend using a trusted community script that ...
Read more >
How to Convert a JSON File to Microsoft Excel - How-To Geek
To import a JSON file into Excel, open Excel to the Data tab and navigate to Get Data > From File > From...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found