2023-01-05 03:57:48 +00:00
|
|
|
---
|
2025-02-17 04:49:35 +00:00
|
|
|
title: Sheets in NW.js
|
|
|
|
|
sidebar_label: NW.js
|
2023-01-05 23:33:49 +00:00
|
|
|
pagination_prev: demos/mobile/index
|
2024-03-18 08:24:41 +00:00
|
|
|
pagination_next: demos/cli/index
|
2023-01-05 03:57:48 +00:00
|
|
|
sidebar_position: 2
|
|
|
|
|
sidebar_custom_props:
|
|
|
|
|
summary: Embedded Chromium + NodeJS
|
|
|
|
|
---
|
|
|
|
|
|
2023-04-27 09:12:19 +00:00
|
|
|
import current from '/version.js';
|
2023-01-05 03:57:48 +00:00
|
|
|
import Tabs from '@theme/Tabs';
|
|
|
|
|
import TabItem from '@theme/TabItem';
|
2023-04-29 11:21:37 +00:00
|
|
|
import CodeBlock from '@theme/CodeBlock';
|
2023-01-05 03:57:48 +00:00
|
|
|
|
2025-02-17 04:49:35 +00:00
|
|
|
[NW.js](https://nwjs.io/), formerly `node-webkit`, is a modern toolkit for
|
|
|
|
|
building desktop apps using web technologies.
|
2023-01-05 03:57:48 +00:00
|
|
|
|
2025-02-17 04:49:35 +00:00
|
|
|
[SheetJS](https://sheetjs.com) is a JavaScript library for reading and writing
|
|
|
|
|
data from spreadsheets.
|
|
|
|
|
|
|
|
|
|
The ["Complete Example"](#complete-example) section covers a complete desktop
|
|
|
|
|
app to read and write workbooks. The app will look like the screenshots below:
|
2023-01-05 03:57:48 +00:00
|
|
|
|
2023-01-09 05:08:30 +00:00
|
|
|
<table><thead><tr>
|
2023-12-02 08:39:35 +00:00
|
|
|
<th><a href="#complete-example">Windows</a></th>
|
2023-01-09 05:08:30 +00:00
|
|
|
<th><a href="#complete-example">macOS</a></th>
|
|
|
|
|
<th><a href="#complete-example">Linux</a></th>
|
|
|
|
|
</tr></thead><tbody><tr><td>
|
2023-01-05 03:57:48 +00:00
|
|
|
|
2023-12-02 08:39:35 +00:00
|
|
|

|
2023-02-21 01:04:05 +00:00
|
|
|
|
|
|
|
|
</td><td>
|
|
|
|
|
|
2023-01-09 05:08:30 +00:00
|
|
|

|
2023-01-05 03:57:48 +00:00
|
|
|
|
2023-01-09 05:08:30 +00:00
|
|
|
</td><td>
|
2023-01-05 03:57:48 +00:00
|
|
|
|
2023-01-09 05:08:30 +00:00
|
|
|

|
2023-01-05 03:57:48 +00:00
|
|
|
|
2023-01-09 05:08:30 +00:00
|
|
|
</td></tr></tbody></table>
|
2023-01-05 03:57:48 +00:00
|
|
|
|
2023-01-09 05:08:30 +00:00
|
|
|
## Integration Details
|
2023-01-05 03:57:48 +00:00
|
|
|
|
2025-02-17 04:49:35 +00:00
|
|
|
The [SheetJS Standalone scripts](/docs/getting-started/installation/standalone)
|
|
|
|
|
can be referenced in a `SCRIPT` tag from the entry point HTML page.
|
|
|
|
|
|
2023-01-09 05:08:30 +00:00
|
|
|
NW.js provides solutions for reading and writing files.
|
2023-01-05 03:57:48 +00:00
|
|
|
|
2023-02-12 08:15:17 +00:00
|
|
|
### Reading Files
|
2023-01-05 03:57:48 +00:00
|
|
|
|
|
|
|
|
The standard HTML5 `FileReader` techniques from the browser apply to NW.js!
|
|
|
|
|
|
|
|
|
|
NW.js handles the OS minutiae for dragging files into app windows. The
|
|
|
|
|
[drag and drop snippet](/docs/solutions/input#example-user-submissions) apply
|
|
|
|
|
to DIV elements on the page.
|
|
|
|
|
|
|
|
|
|
Similarly, file input elements automatically map to standard Web APIs.
|
|
|
|
|
|
|
|
|
|
For example, assuming a file input element on the page:
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<input type="file" name="xlfile" id="xlf" />
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The event handler would process the event as if it were a web event:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
async function handleFile(e) {
|
|
|
|
|
const file = e.target.files[0];
|
|
|
|
|
const data = await file.arrayBuffer();
|
|
|
|
|
/* data is an ArrayBuffer */
|
|
|
|
|
const workbook = XLSX.read(data);
|
|
|
|
|
|
|
|
|
|
/* DO SOMETHING WITH workbook HERE */
|
|
|
|
|
}
|
|
|
|
|
document.getElementById("xlf").addEventListener("change", handleFile, false);
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-12 08:15:17 +00:00
|
|
|
### Writing Files
|
2023-01-05 03:57:48 +00:00
|
|
|
|
|
|
|
|
File input elements with the attribute `nwsaveas` show UI for saving a file. The
|
|
|
|
|
standard trick is to generate a hidden file input DOM element and "click" it.
|
|
|
|
|
Since NW.js does not present a `writeFileSync` in the `fs` package, a manual
|
|
|
|
|
step is required:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
/* pre-build the hidden nwsaveas input element */
|
|
|
|
|
var input = document.createElement('input');
|
|
|
|
|
input.style.display = 'none';
|
|
|
|
|
input.setAttribute('nwsaveas', 'SheetJSNWDemo.xlsx');
|
|
|
|
|
input.setAttribute('type', 'file');
|
|
|
|
|
document.body.appendChild(input);
|
|
|
|
|
|
|
|
|
|
/* show a message if the save is canceled */
|
|
|
|
|
input.addEventListener('cancel',function(){ alert("Save was canceled!"); });
|
|
|
|
|
|
|
|
|
|
/* write to a file on the 'change' event */
|
|
|
|
|
input.addEventListener('change',function(e){
|
|
|
|
|
/* the `value` is the path that the program will write */
|
|
|
|
|
var filename = this.value;
|
|
|
|
|
|
|
|
|
|
/* use XLSX.write with type "buffer" to generate a buffer" */
|
|
|
|
|
/* highlight-next-line */
|
|
|
|
|
var wbout = XLSX.write(workbook, {type:'buffer', bookType:"xlsx"});
|
|
|
|
|
/* highlight-next-line */
|
2023-01-09 05:08:30 +00:00
|
|
|
require("fs").writeFile(filename, wbout, function(err) {
|
2023-01-05 03:57:48 +00:00
|
|
|
if(!err) return alert("Saved to " + filename);
|
|
|
|
|
alert("Error: " + (err.message || err));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.click();
|
|
|
|
|
```
|
2023-01-09 05:08:30 +00:00
|
|
|
|
|
|
|
|
## Complete Example
|
|
|
|
|
|
2023-12-02 08:39:35 +00:00
|
|
|
:::note Tested Deployments
|
2023-02-12 08:15:17 +00:00
|
|
|
|
2023-09-25 07:30:54 +00:00
|
|
|
This demo was tested in the following environments:
|
|
|
|
|
|
2026-06-14 23:29:38 +00:00
|
|
|
| OS and Version | Architecture | NW.js | Date |
|
|
|
|
|
|:---------------|:-------------|:----------|:-----------|
|
|
|
|
|
| macOS 15.7.4 | `darwin-x64` | `0.112.0` | 2026-06-13 |
|
|
|
|
|
| macOS 14.5 | `darwin-arm` | `0.94.0` | 2025-03-30 |
|
|
|
|
|
| Windows 11 | `win11-x64` | `0.111.3` | 2026-05-22 |
|
|
|
|
|
| Windows 11 | `win11-arm` | `0.94.0` | 2025-02-23 |
|
|
|
|
|
| Linux (Ubuntu) | `linux-x64` | `0.101.2` | 2025-07-06 |
|
|
|
|
|
| Linux (Debian) | `linux-arm` | `0.112.0` | 2026-06-14 |
|
2023-09-27 21:58:41 +00:00
|
|
|
|
2023-02-12 08:15:17 +00:00
|
|
|
:::
|
|
|
|
|
|
2023-12-02 08:39:35 +00:00
|
|
|
0) Create a project folder:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
mkdir sheetjs-nwjs
|
|
|
|
|
cd sheetjs-nwjs
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-09 05:08:30 +00:00
|
|
|
1) Create a `package.json` file that specifies the entry point:
|
|
|
|
|
|
2023-04-29 11:21:37 +00:00
|
|
|
<CodeBlock language="json" title="package.json">{`\
|
2023-01-09 05:08:30 +00:00
|
|
|
{
|
|
|
|
|
"name": "sheetjs-nwjs",
|
|
|
|
|
"author": "sheetjs",
|
|
|
|
|
"version": "0.0.0",
|
|
|
|
|
"main": "index.html",
|
2026-06-13 05:51:30 +00:00
|
|
|
"nwbuild": {
|
|
|
|
|
"app": {
|
|
|
|
|
"icon": "favicon-196x196.png",
|
|
|
|
|
"LSApplicationCategoryType": "public.app-category.utilities",
|
|
|
|
|
"NSHumanReadableCopyright": "Copyright © SheetJS LLC"
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-01-09 05:08:30 +00:00
|
|
|
"dependencies": {
|
2026-06-13 05:51:30 +00:00
|
|
|
"nw": "0.112.0",
|
2023-04-29 11:21:37 +00:00
|
|
|
"xlsx": "https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz"
|
2023-01-09 05:08:30 +00:00
|
|
|
}
|
2023-04-29 11:21:37 +00:00
|
|
|
}`}
|
|
|
|
|
</CodeBlock>
|
2023-01-09 05:08:30 +00:00
|
|
|
|
2026-06-13 05:51:30 +00:00
|
|
|
2) Download the following files into the same folder:
|
|
|
|
|
- [`index.html`](pathname:///nwjs/index.html)
|
|
|
|
|
- [`favicon-196x196.png`](https://sheetjs.com/favico/favicon-196x196.png)
|
2023-01-09 05:08:30 +00:00
|
|
|
|
2023-09-24 03:59:48 +00:00
|
|
|
:::caution pass
|
2023-01-09 05:08:30 +00:00
|
|
|
|
2026-06-13 05:51:30 +00:00
|
|
|
Right-click each link and select "Save Link As...". Left-clicking a link will
|
2023-01-09 05:08:30 +00:00
|
|
|
try to load the page in your browser. The goal is to save the file contents.
|
|
|
|
|
|
|
|
|
|
:::
|
|
|
|
|
|
2023-02-21 01:04:05 +00:00
|
|
|
In the terminal window, the download can be performed with:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
curl -LO https://docs.sheetjs.com/nwjs/index.html
|
2026-06-13 05:51:30 +00:00
|
|
|
curl -LO https://sheetjs.com/favico/favicon-196x196.png
|
2023-02-21 01:04:05 +00:00
|
|
|
```
|
|
|
|
|
|
2025-05-27 22:50:10 +00:00
|
|
|
:::note pass
|
|
|
|
|
|
|
|
|
|
In PowerShell, the command may fail with a parameter error:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
Invoke-WebRequest : A parameter cannot be found that matches parameter name 'L'.
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`curl.exe` must be invoked directly:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
curl.exe -LO https://docs.sheetjs.com/nwjs/index.html
|
2026-06-13 05:51:30 +00:00
|
|
|
curl.exe -LO https://sheetjs.com/favico/favicon-196x196.png
|
2025-05-27 22:50:10 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
:::
|
|
|
|
|
|
2023-09-27 04:43:00 +00:00
|
|
|
3) Install dependencies:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
npm i
|
|
|
|
|
```
|
2023-01-09 05:08:30 +00:00
|
|
|
|
|
|
|
|
4) To verify the app works, run in the test environment:
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-06-14 23:29:38 +00:00
|
|
|
npx -y nw .
|
2023-01-09 05:08:30 +00:00
|
|
|
```
|
|
|
|
|
|
2024-05-28 05:20:05 +00:00
|
|
|
On launch, the app will fetch and parse https://docs.sheetjs.com/pres.numbers .
|
|
|
|
|
|
|
|
|
|
Using the file input element, a file can be selected from the filesystem and the
|
|
|
|
|
table will refresh with the contents of the selected file.
|
|
|
|
|
|
|
|
|
|
Click "Export Data!" and save the generated file to `SheetJSNWDemo.xlsx`. This
|
|
|
|
|
file can be opened in Excel or another spreadsheet editor.
|
2023-01-09 05:08:30 +00:00
|
|
|
|
|
|
|
|
5) To build a standalone app, run the builder:
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-06-14 23:29:38 +00:00
|
|
|
npx -y -p nw-builder@4.17.10 nwbuild --mode=build --version=0.111.3 --glob=false --outDir=../out ./
|
2023-01-09 05:08:30 +00:00
|
|
|
```
|
|
|
|
|
|
2023-09-27 21:58:41 +00:00
|
|
|
This will generate the standalone app in the `..\out\` folder.
|
|
|
|
|
|
2024-07-08 08:18:18 +00:00
|
|
|
6) Launch the generated application:
|
|
|
|
|
|
|
|
|
|
| Architecture | Command |
|
|
|
|
|
|:-------------|:--------------------------------------------------------------|
|
2025-01-06 02:51:20 +00:00
|
|
|
| `darwin-x64` | `open ../out/sheetjs-nwjs.app` |
|
2025-03-31 02:09:31 +00:00
|
|
|
| `darwin-arm` | `open ../out/sheetjs-nwjs.app` |
|
2024-12-21 03:32:22 +00:00
|
|
|
| `win11-x64` | `..\out\sheetjs-nwjs.exe` |
|
2025-02-24 01:17:05 +00:00
|
|
|
| `win11-arm` | `..\out\sheetjs-nwjs.exe` |
|
|
|
|
|
| `linux-x64` | `../out/sheetjs-nwjs` |
|
2026-06-14 23:29:38 +00:00
|
|
|
| `linux-arm` | `../out/sheetjs-nwjs` |
|