Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'json_to_sheet') #2933

Closed
opened 2023-05-01 19:46:53 +00:00 by CharanGIS · 5 comments

I'm using the latest version to convert JSON to excel but when i try to convert json to csv I get an error

<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.19.3/package/dist/xlsx.full.min.js"></script>
                
                
const exportdata =  JSON.stringify(sitelogDataset);

var ws = XLSX.utils.json_to_sheet(exportdata);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Site Log");
XLSX.writeFile(wb, "SiteLog.xlsx")
I'm using the latest version to convert JSON to excel but when i try to convert json to csv I get an error ``` <script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.19.3/package/dist/xlsx.full.min.js"></script> const exportdata = JSON.stringify(sitelogDataset); var ws = XLSX.utils.json_to_sheet(exportdata); var wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, "Site Log"); XLSX.writeFile(wb, "SiteLog.xlsx") ```
Owner

The error suggests XLSX is defined but XLSX.utils is not. Are you potentially defining the variable XLSX somewhere in your code? For example, the following elicits an error: https://jsfiddle.net/thc5sdv4/

<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.19.3/package/dist/xlsx.full.min.js"></script>
<script>
  var XLSX = {}; // masks the XLSX variable from the global scope
  XLSX.utils.sheet_to_json([{a:1}]); // error
</script>

First check the developer tools console to see if there are any errors before that specific error.

If there are no other errors, console.log(XLSX.version) just before your XLSX.utils.sheet_to_json call.

PS: json_to_sheet expects an array of row objects, so you would call var ws = XLSX.utils.json_to_sheet(sitelogDataset);

The error suggests `XLSX` is defined but `XLSX.utils` is not. Are you potentially defining the variable XLSX somewhere in your code? For example, the following elicits an error: https://jsfiddle.net/thc5sdv4/ ```html <script lang="javascript" src="https://cdn.sheetjs.com/xlsx-0.19.3/package/dist/xlsx.full.min.js"></script> <script> var XLSX = {}; // masks the XLSX variable from the global scope XLSX.utils.sheet_to_json([{a:1}]); // error </script> ``` First check the developer tools console to see if there are any errors before that specific error. If there are no other errors, `console.log(XLSX.version)` just before your `XLSX.utils.sheet_to_json` call. PS: `json_to_sheet` expects an array of row objects, so you would call `var ws = XLSX.utils.json_to_sheet(sitelogDataset);`
Author

Hi, Im not using XLSX anywhere, when i do console.log(XLSX.verion) I get undefined

Hi, Im not using XLSX anywhere, when i do console.log(XLSX.verion) I get undefined
Author

I moved me script to the top of the code, I get the version but now i get this error: image

I moved me script to the top of the code, I get the version but now i get this error: ![image](/attachments/9410a1dd-5475-4077-8f4d-8820cd79d37d)
Owner

The script itself should work. https://sheetjs.com/pres-0.19.3.html is a copy of our tutorial example https://docs.sheetjs.com/docs/getting-started/example#run-the-demo-locally

The error message suggests there is an issue in your deployment. Please share more details here (where is it running? is there a public site that reproduces the issue?) or pop into our Discord

The script itself should work. https://sheetjs.com/pres-0.19.3.html is a copy of our tutorial example https://docs.sheetjs.com/docs/getting-started/example#run-the-demo-locally The error message suggests there is an issue in your deployment. Please share more details here (where is it running? is there a public site that reproduces the issue?) or pop into our [Discord](https://discord.com/invite/sheetjs)
Owner

The new error suggests you aren't passing an array of objects. json_to_sheet expects an array of JS objects. If sitelogDataset is an array of objects, use that directly:

var ws = XLSX.utils.json_to_sheet(sitelogDataset);

If sitelogDataset is a large JS object, you have to massage it into an array of objects. https://docs.sheetjs.com/docs/getting-started/example includes an example

The new error suggests you aren't passing an array of objects. `json_to_sheet` expects an array of JS objects. If `sitelogDataset` is an array of objects, use that directly: ```js var ws = XLSX.utils.json_to_sheet(sitelogDataset); ``` If `sitelogDataset` is a large JS object, you have to massage it into an array of objects. https://docs.sheetjs.com/docs/getting-started/example includes an example
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#2933
No description provided.