docs.sheetjs.com/docz/docs/03-demos/42-engines/15-rb.md

179 lines
4.7 KiB
Markdown
Raw Normal View History

2023-02-14 08:22:31 +00:00
---
2023-02-15 01:00:49 +00:00
title: Ruby + Bindings
2023-02-28 11:40:44 +00:00
pagination_prev: demos/bigdata/index
pagination_next: solutions/input
2023-02-14 08:22:31 +00:00
---
2023-04-27 09:12:19 +00:00
import current from '/version.js';
2023-05-07 13:58:36 +00:00
import CodeBlock from '@theme/CodeBlock';
2023-04-27 09:12:19 +00:00
2023-02-14 08:22:31 +00:00
ExecJS is a Ruby abstraction over a number of JS runtimes including V8.
2024-06-04 04:56:22 +00:00
[SheetJS](https://sheetjs.com) is a JavaScript library for reading and writing
data from spreadsheets.
This demo uses ExecJS and SheetJS to pull data from sheets and print CSV rows.
We'll explore how to load SheetJS in ExecJS contexts and process data in Ruby.
The ["Complete Example"](#complete-example) section includes a complete Ruby
script for reading data from files.
## Integration Details
2023-09-22 06:32:55 +00:00
The [SheetJS Standalone scripts](/docs/getting-started/installation/standalone)
can be parsed and evaluated in every supported runtime.
2023-02-14 08:22:31 +00:00
2024-06-04 04:56:22 +00:00
### Initialize ExecJS
The `require` command performs the required initialization steps:
```rb
require "execjs"
```
2023-02-14 08:22:31 +00:00
2024-06-04 04:56:22 +00:00
### Load SheetJS Scripts
2023-02-14 08:22:31 +00:00
The main library can be loaded and compiled in a new context:
```rb
require "execjs"
2023-09-25 07:30:54 +00:00
source = File.open("xlsx.full.min.js", "rb").read;
source.force_encoding("UTF-8");
2023-02-14 08:22:31 +00:00
context = ExecJS.compile(source);
```
To confirm the library is loaded, `XLSX.version` can be inspected:
```rb
puts context.eval("XLSX.version");
```
2024-06-04 04:56:22 +00:00
### Reading and Writing Files
2023-02-14 08:22:31 +00:00
The architecture of ExecJS forces users to combine reading and writing in one
function step. Base64 strings should be used for interchange. For example,
the following snippet reads data from `pres.numbers`, generates an XLSB file,
and writes to `sheetjsw.xlsb`:
```rb
require "base64"
# read and encode data to Base64
2023-09-25 07:30:54 +00:00
data = Base64.strict_encode64(File.open("pres.numbers", "rb").read);
2023-02-14 08:22:31 +00:00
# define function and call with the data
xlsb = context.call(<<EOF, data);
function(data) {
/* parse data -- the argument is the data from Ruby code */
var wb = XLSX.read(data, {type: 'base64'});
/* write XLSB data (encoded as base64) */
return XLSX.write(wb, {bookType: "xlsb", type: "base64"});
}
EOF
# at this point, `xlsb` is a Base64-encoded string
# decode and write to file
File.write("sheetjsw.xlsb", Base64.strict_decode64(xlsb), mode: "wb");
```
The `strict_` variants ensure that no newlines are added to the strings.
## Complete Example
2023-12-02 08:39:35 +00:00
:::note Tested Deployments
2023-02-14 08:22:31 +00:00
2023-07-25 02:31:22 +00:00
This demo was tested in the following deployments:
| Platform | Ruby | ExecJS | Date |
|:-------------|:---------|:--------|:-----------|
2024-04-26 04:16:13 +00:00
| `darwin-x64` | `2.6.10` | `2.9.1` | 2024-04-25 |
2024-05-26 07:50:55 +00:00
| `darwin-arm` | `2.6.10` | `2.9.1` | 2024-05-25 |
2024-03-12 06:47:52 +00:00
| `win10-x64` | `3.2.3` | `2.9.1` | 2024-03-10 |
2024-05-26 07:50:55 +00:00
| `win11-arm` | `3.0.2` | `2.9.1` | 2024-05-25 |
2024-03-22 04:45:40 +00:00
| `linux-x64` | `3.0.5` | `2.9.1` | 2024-03-21 |
2024-05-26 07:50:55 +00:00
| `linux-arm` | `3.1.2` | `2.9.1` | 2024-05-25 |
2023-09-25 07:30:54 +00:00
2024-05-26 07:50:55 +00:00
When the demo was last tested, there was no official Ruby release for Windows
on ARM. The `win11-arm` test was run in WSL. The `win10-x64` test used the
official Ruby for Windows x64 release.
2023-02-14 08:22:31 +00:00
:::
0) Install Ruby, `gem` (RubyGems), and the dependencies:
```bash
gem install execjs
```
2024-04-26 04:16:13 +00:00
<details>
<summary><b>Installation Notes</b> (click to show)</summary>
2023-07-25 02:31:22 +00:00
The command may need to be run as an administrator or root user:
```bash
sudo gem install execjs
```
2024-03-22 04:45:40 +00:00
:::note pass
On Arch Linux-based platforms including the Steam Deck, `rubygems` must be
installed through the package manager:
```bash
sudo pacman -Syu rubygems
```
:::
2024-04-26 04:16:13 +00:00
</details>
2024-03-12 06:47:52 +00:00
1) Create a new project folder:
```bash
mkdir sheetjs-rb
cd sheetjs-rb
```
2) Download the SheetJS Standalone script and the test file. Save both files in
2023-09-22 06:32:55 +00:00
the project directory:
2023-02-14 08:22:31 +00:00
<ul>
2023-04-27 09:12:19 +00:00
<li><a href={`https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js`}>xlsx.full.min.js</a></li>
2024-04-26 04:16:13 +00:00
<li><a href="https://docs.sheetjs.com/pres.numbers">pres.numbers</a></li>
2023-02-14 08:22:31 +00:00
</ul>
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2023-04-27 09:12:19 +00:00
curl -LO https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js
2024-04-26 04:16:13 +00:00
curl -LO https://docs.sheetjs.com/pres.numbers`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2023-02-14 08:22:31 +00:00
2024-03-12 06:47:52 +00:00
3) Download [`ExecSheetJS.rb`](pathname:///execjs/ExecSheetJS.rb):
2023-02-14 08:22:31 +00:00
```bash
curl -LO https://docs.sheetjs.com/execjs/ExecSheetJS.rb
```
2024-03-12 06:47:52 +00:00
4) Run the demo:
2023-02-14 08:22:31 +00:00
```bash
ruby ExecSheetJS.rb pres.numbers
```
If the program succeeded, the CSV contents will be printed to console and the
file `sheetjsw.xlsb` will be created. That file can be opened with Excel.
2023-09-25 07:30:54 +00:00
:::caution pass
If a JavaScript runtime is not available, the script will throw an error:
```
execjs/runtimes.rb:68:in `autodetect': Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
```
ExecJS 2.9.1 supports the Bun runtime. Install the Bun runtime[^1], restart the
terminal, and re-run the script.
:::
[^1]: `curl -fsSL https://bun.sh/install | bash` can be run from macOS, Linux, and Windows WSL.