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.

Add a jason data to an existing sheet.

See original GitHub issue

Hi guys, From previous example, we know that after reading a file, we can add new data into that particular sheet:

var XLSX = require('xlsx');
var wb = XLSX.readFile('sheetjs.xlsx');
add_cell_to_sheet(wb.Sheets.SheetJS, "F6", 12345);
XLSX.writeFile('sheetjs-new.xlsx', wb);

But how to do if my data is not just a single cell? I have a json data that I would like to add into the file that I read. How to add the json data?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
SheetJSDevcommented, Jun 20, 2018

A literal conversion of the first example would be:

var XLSX = require('xlsx');
var wb = XLSX.readFile('sheetjs.xlsx');
XLSX.utils.sheet_add_json(
  wb.Sheets.SheetJS, // worksheet
  [{number: 12345}], // data
  { // options
    header: ["number"], // properties to read from each object
    skipHeader:true, // do not include header row
    origin: "F6" // start from cell F6
  }
);
XLSX.writeFile('sheetjs-new.xlsx', wb);

This example writes F6=“a”, F7=“b”, G6=1, G7=2:

XLSX.utils.sheet_add_json(
  wb.Sheets.SheetJS,
  [
    {text: "a", number: 1},
    {number:2, text: "b"}
  ],
  {
    header: ["text", "number"],
    skipHeader:true,
    origin: "F6"
  }
);
0reactions
Madmalscommented, Jun 24, 2022

this is what working with me…after getting error var ext = o.file.slice(o.file.lastIndexOf(".")).toLowerCase(); ^ TypeError: o.file.lastIndexOf is not a function

var wb = XLSX.readFile('./data.xlsx');
const ws = XLSX.utils.sheet_add_json(
wb.Sheets.Sheet1, // worksheet
		[{ number: 12345 }], // data
		{ // options
			header: ["number"], // properties to read from each object
			skipHeader: true, // do not include header row
			origin: "A3" // start from cell F6
		}
	);

	XLSX.utils.book_append_sheet(wb, ws)
	XLSX.writeFile(wb, 'data.xlsx');```
Read more comments on GitHub >

github_iconTop Results From Across the Web

Add a jason data to an existing sheet js-xlsx - Stack Overflow
After you readFile, you want to create worksheet by json_to_sheet property. var ws = XLSX.utils.json_to_sheet(jsonPrep);.
Read more >
Insert an external Excel file and populate it with JSON data
This sample shows how to insert an existing template from an external Excel file into the currently open Excel file. Then it retrieves...
Read more >
Import JSON to Google Sheets: Step-by-Step Tutorial & Script
If you want to try an alternative method to import JSON data into your Google Sheet, you can use the add-on we created...
Read more >
JSON into Excel - YouTube
Demo step-by-step for how to load JSON files into MS Excel and format as table. Power Query provides ability to map JSON array...
Read more >
Import JSON data to an Excel spreadsheet using Python
In this tutorial, we are going to learn how to import JSON data into an Excel spreadsheet using Python.PS: To interact with an...
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