Skip to content

Commit 5b1440a

Browse files
Srijon Sahaianjennings
andauthored
Update electron demo (#2027)
* Update electron demo to version 9 [ci skip] Co-authored-by: Ian Jennings <[email protected]>
1 parent 9ec9f4f commit 5b1440a

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

demos/electron/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Electron
22

33
This library is compatible with Electron and should just work out of the box.
4-
The demonstration uses Electron 1.7.5. The library is added via `require` from
5-
the render process. It can also be required from the main process, as shown in
6-
this demo to render a version string in the About dialog on OSX.
4+
The demonstration uses Electron 9.0.5. The library is added via `require` from
5+
the renderer process. Note that Electron now requires `nodeIntegration: true`
6+
in order to `require('XLSX')` in the renderer process. It can also be required
7+
from the main process, as shown in this demo to render a version string in the
8+
About dialog on OSX.
79

810
The standard HTML5 `FileReader` techniques from the browser apply to Electron.
911
This demo includes a drag-and-drop box as well as a file input box, mirroring

demos/electron/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<html>
55
<head>
66
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
7+
<meta http-equiv="Content-Security-Policy" content="script-src 'self' https:">
78
<title>SheetJS Electron Demo</title>
89
<style>
910
#drop{

demos/electron/index.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
2-
/*global Uint8Array, console */
1+
/* xlsx.js (C) 2013-present SheetJS -- https://sheetjs.com */
2+
/* global Uint8Array, console */
33
/* exported export_xlsx */
44
/* eslint no-use-before-define:0 */
55
var XLSX = require('xlsx');
@@ -19,16 +19,6 @@ var process_wb = (function() {
1919
};
2020
})();
2121

22-
var _gaq = _gaq || [];
23-
_gaq.push(['_setAccount', 'UA-36810333-1']);
24-
_gaq.push(['_trackPageview']);
25-
26-
(function() {
27-
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
28-
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
29-
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
30-
})();
31-
3222
var do_file = (function() {
3323
return function do_file(files) {
3424
var f = files[0];
@@ -64,16 +54,16 @@ var do_file = (function() {
6454

6555
(function() {
6656
var readf = document.getElementById('readf');
67-
function handleF(/*e*/) {
68-
var o = electron.dialog.showOpenDialog({
57+
async function handleF(/*e*/) {
58+
var o = await electron.dialog.showOpenDialog({
6959
title: 'Select a file',
7060
filters: [{
7161
name: "Spreadsheets",
7262
extensions: "xls|xlsx|xlsm|xlsb|xml|xlw|xlc|csv|txt|dif|sylk|slk|prn|ods|fods|uos|dbf|wks|123|wq1|qpw|htm|html".split("|")
7363
}],
7464
properties: ['openFile']
7565
});
76-
if(o.length > 0) process_wb(XLSX.readFile(o[0]));
66+
if(o.filePaths.length > 0) process_wb(XLSX.readFile(o.filePaths[0]));
7767
}
7868
readf.addEventListener('click', handleF, false);
7969
})();
@@ -87,18 +77,18 @@ var do_file = (function() {
8777
var export_xlsx = (function() {
8878
var HTMLOUT = document.getElementById('htmlout');
8979
var XTENSION = "xls|xlsx|xlsm|xlsb|xml|csv|txt|dif|sylk|slk|prn|ods|fods|htm|html".split("|")
90-
return function() {
80+
return async function() {
9181
var wb = XLSX.utils.table_to_book(HTMLOUT);
92-
var o = electron.dialog.showSaveDialog({
82+
var o = await electron.dialog.showSaveDialog({
9383
title: 'Save file as',
9484
filters: [{
9585
name: "Spreadsheets",
9686
extensions: XTENSION
9787
}]
9888
});
99-
console.log(o);
100-
XLSX.writeFile(wb, o);
101-
electron.dialog.showMessageBox({ message: "Exported data to " + o, buttons: ["OK"] });
89+
console.log(o.filePath);
90+
XLSX.writeFile(wb, o.filePath);
91+
electron.dialog.showMessageBox({ message: "Exported data to " + o.filePath, buttons: ["OK"] });
10292
};
10393
})();
10494
void export_xlsx;

demos/electron/main.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ var app = electron.app;
77
var win = null;
88

99
function createWindow() {
10-
if(win) return;
11-
win = new electron.BrowserWindow({width:800, height:600});
10+
if (win) return;
11+
win = new electron.BrowserWindow({
12+
width: 800, height: 600,
13+
webPreferences: {
14+
nodeIntegration: true,
15+
enableRemoteModule: true
16+
}
17+
});
1218
win.loadURL("file://" + __dirname + "/index.html");
1319
win.webContents.openDevTools();
14-
win.on('closed', function() { win = null; });
20+
win.on('closed', function () { win = null; });
1521
}
16-
if(app.setAboutPanelOptions) app.setAboutPanelOptions({ applicationName: 'sheetjs-electron', applicationVersion: "XLSX " + XLSX.version, copyright: "(C) 2017-present SheetJS LLC" });
17-
app.on('open-file', function() { console.log(arguments); });
22+
if (app.setAboutPanelOptions) app.setAboutPanelOptions({ applicationName: 'sheetjs-electron', applicationVersion: "XLSX " + XLSX.version, copyright: "(C) 2017-present SheetJS LLC" });
23+
app.on('open-file', function () { console.log(arguments); });
1824
app.on('ready', createWindow);
1925
app.on('activate', createWindow);
20-
app.on('window-all-closed', function() { if(process.platform !== 'darwin') app.quit(); });
26+
app.on('window-all-closed', function () { if (process.platform !== 'darwin') app.quit(); });

demos/electron/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"version": "0.0.0",
55
"main": "main.js",
66
"dependencies": {
7-
"electron": "~1.7.x",
8-
"xlsx": "*"
7+
"xlsx": "*",
8+
"electron": "^9.0.5"
9+
},
10+
"scripts": {
11+
"start": "electron ."
912
}
1013
}

0 commit comments

Comments
 (0)