docs.sheetjs.com/docz/docs/03-demos/12-engines/15_rb.md

120 lines
2.9 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.
The [Standalone scripts](/docs/getting-started/installation/standalone) can be
parsed and evaluated in every supported runtime.
## Integration Details
_Load SheetJS Scripts_
The main library can be loaded and compiled in a new context:
```rb
require "execjs"
source = File.open("xlsx.full.min.js").read;
context = ExecJS.compile(source);
```
To confirm the library is loaded, `XLSX.version` can be inspected:
```rb
puts context.eval("XLSX.version");
```
_Reading and Writing Files_
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
data = Base64.strict_encode64(File.open("pres.numbers").read);
# 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
:::note
2023-07-25 02:31:22 +00:00
This demo was tested in the following deployments:
| Platform | Ruby | ExecJS | Date |
|:-------------|:---------|:--------|:-----------|
| `darwin-x64` | `2.7.6` | `2.8.1` | 2023-07-24 |
| `darwin-arm` | `2.6.10` | `2.8.1` | 2023-07-24 |
2023-08-28 22:40:53 +00:00
| `linux-x64` | `3.0.4` | `2.8.1` | 2023-08-27 |
2023-02-14 08:22:31 +00:00
:::
0) Install Ruby, `gem` (RubyGems), and the dependencies:
```bash
gem install execjs
```
2023-07-25 02:31:22 +00:00
:::note
The command may need to be run as an administrator or root user:
```bash
sudo gem install execjs
```
:::
2023-02-14 08:22:31 +00:00
1) Download the standalone script and test file:
<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>
2023-02-14 08:22:31 +00:00
<li><a href="https://sheetjs.com/pres.numbers">pres.numbers</a></li>
</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
curl -LO https://sheetjs.com/pres.numbers`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2023-02-14 08:22:31 +00:00
2) Download [`ExecSheetJS.rb`](pathname:///execjs/ExecSheetJS.rb):
```bash
curl -LO https://docs.sheetjs.com/execjs/ExecSheetJS.rb
```
3) Run the demo:
```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.