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.

The ReactJS demo for js-xlsx is very unclear to me

See original GitHub issue

Sorry if I am not skilled enough to understand the demo, but this page: https://github.com/SheetJS/js-xlsx/tree/master/demos/react is very unclear to me and I still don’t get how this library has to be used in ReactJS.

How do we have to use this object

{
  cols: [{ name: "A", key: 0 }, { name: "B", key: 1 }, { name: "C", key: 2 }],
  data: [
    [ "id",    "name", "value" ],
    [    1, "sheetjs",    7262 ],
    [    2, "js-xlsx",    6969 ]
  ]
}

and download it as a xlsx? Please help!

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
SheetJSDevcommented, Jul 5, 2018

If the demo doesn’t make sense, we probably are missing something. Here’s a longer explanation. If this makes sense, please let us know how to improve the README:

To run the demo, clone the repo and go to that subdirectory (/demos/react) and run make && make react, then go to http://localhost:8000 to see the demo.

You should see something like:

demo1

Then select a spreadsheet by clicking the file input button or dragging and dropping a file like Book1.xlsx – you can click and download it.

demo2

If you add the line console.log(this.state); at the top of the exportFile function, you can see the state when the file is generated:

demo3

That object represents the state that the example uses. https://github.com/SheetJS/js-xlsx/blob/master/demos/react/sheetjs.jsx is the demo component used in the web examples.

this.state.data is an array of arrays, so the exportFile function just converts that part of the state:

/* convert state.data to worksheet */
const ws = XLSX.utils.aoa_to_sheet(this.state.data);
/* build a workbook */
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "SheetJS");
/* generate XLSX file and send to client */
XLSX.writeFile(wb, "sheetjs.xlsx")

The callback is triggered when a certain button is clicked:

<button disabled={!this.state.data.length} className="btn btn-success" onClick={this.exportFile}>Export</button>

On the read side, the state is set when you process a file.

If that explanation makes sense, please feel free to submit a PR to clarify the README.

2reactions
dave-windcommented, Sep 26, 2019

react ant design you can try to see https://github.com/dave-wind/excel

Read more comments on GitHub >

github_iconTop Results From Across the Web

2 Answers - 2 - Stack Overflow
The cell object has the property f , which is the formula you want to use. Here you can see all the options: ......
Read more >
React JS - React Tutorial for Beginners - YouTube
React JS Tutorial - Get up & running with React JS: the most popular JavaScript library in the world! - Want to master...
Read more >
React JS— The Confusing Parts - Level Up Coding
What's so confusing about React? There are many reasons why certain parts of React may be confusing to a developer.
Read more >
Tutorial: Intro to React
Overview will teach you the fundamentals of React: components, props, and state. Completing the Game will teach you the most common techniques in...
Read more >
React Tutorial: An Overview and Walkthrough - Tania Rascia
Build a very simple React app that demonstrates the above concepts. Here's the source and a live demo of the end result.
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