Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XLSX.read is not a function Electron JS #2209

Closed
shamomomo opened this issue Jan 2, 2021 · 6 comments
Closed

XLSX.read is not a function Electron JS #2209

shamomomo opened this issue Jan 2, 2021 · 6 comments

Comments

@shamomomo
Copy link

Dears, I'm fairly new to this thing, so let me know what info I'm missing:
VSCode
Electron 11
Trying to read a local excel file in browser.

What I tried:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.16.9/xlsx.core.min.js"></script>

AND (not simultaneously)

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.16.9/xlsx.full.min.js"></script>

What happened:
I wrote the html file, and opened it in chrome (87.0 64bit, latest as of 2021-jan-03) It worked fine, reading alright.
Then I npm-started the project in VSCode, and I got:
"Uncaught TypeError: XLSX.read is not a function
at FileReader.filereader.onload"

My Code Snippet:

`<script type="text/javascript">

    let resultingJSON;
    function selectFile() 
    {
        let selectedFile;
        document.getElementById('file').click();
        document.getElementById('file').addEventListener('change', function (e)
        {
            selectedFile = e.target.files[0];
            var fileReader = new FileReader();
            fileReader.readAsBinaryString(selectedFile);
            fileReader.onload = (event)=>
            {                
                let data = event.target.result;
                let workbook = XLSX.read(data,{type:"binary"});
                let workSheet = workbook.Sheets["Sheet1"];
                resultingJSON = XLSX.utils.sheet_to_json(workSheet);
            }
        });
    }
</script>`
@thrnd
Copy link

thrnd commented Mar 1, 2021

Be sure that XSLX library loads before your main script.

...and i think you don't need both core and full. I think full includes core.

And also check paths for scripts in electron. I'm developing some electron-app with this lib and sometimes i fed up with paths =)

@SheetJSDev
Copy link
Contributor

Overall, electron pretends to be node-like both in the main thread and the renderer thread. This presents issues with the standalone scripts.

In the electron demo the renderer process requires the module. The demo works with electron 11, so that approach should be used.

@fujinxiang
Copy link

if nodeIntegration = true,window.module is not undefined,xlsx use CommonJs, so window.XLSX = {}

there is source code from xlsx

if(typeof exports !== 'undefined') make_xlsx_lib(exports);
else if(typeof module !== 'undefined' && module.exports) make_xlsx_lib(module.exports);
else if(typeof define === 'function' && define.amd) define(function() { if(!XLSX.version) make_xlsx_lib(XLSX); return XLSX; });
else make_xlsx_lib(XLSX);

maybe you can find an answer: https://juejin.cn/post/7006212507719172127/

@SheetJSDev
Copy link
Contributor

@fujinxiang Does the demo project work for you?

The required arguments for electron 14 are:

	win = new electron.BrowserWindow({
		width: 800, height: 600,
		webPreferences: {
			worldSafeExecuteJavaScript: true, // required for Electron 12+
			contextIsolation: false, // required for Electron 12+
			nodeIntegration: true,
			enableRemoteModule: true
		}
	});

With that, const XLSX = require("xlsx") works from both the main thread and from the renderer thread

@fujinxiang
Copy link

i have not try the demo, i use xlsx in script tag, not require, and the electron version of my application is 4.x.x.

@SheetJSDev
Copy link
Contributor

Please try using the require form. You can see how the demo does it by using a script tag to reference the JS file and performing a require from the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants