This commit is contained in:
SheetJS 2023-02-14 03:22:31 -05:00
parent c2a3326377
commit 39b171c011
3 changed files with 125 additions and 1 deletions

@ -0,0 +1,100 @@
---
title: Ruby Bindings
pagination_prev: demos/cli
pagination_next: demos/clipboard
---
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
This demo was tested on 2023 February 14.
:::
0) Install Ruby, `gem` (RubyGems), and the dependencies:
```bash
gem install execjs
```
1) Download the standalone script and test file:
<ul>
<li><a href={`https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js`}>xlsx.full.min.js</a></li>
<li><a href="https://sheetjs.com/pres.numbers">pres.numbers</a></li>
</ul>
```bash
curl -LO https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js
curl -LO https://sheetjs.com/pres.numbers
```
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.

@ -142,7 +142,7 @@ const config = {
prism: {
theme: lightCodeTheme,
darkTheme: darkCodeTheme,
additionalLanguages: [ "swift", "java", "csharp", "perl" ],
additionalLanguages: [ "swift", "java", "csharp", "perl", "ruby" ],
},
liveCodeBlock: {
playgroundPosition: 'top'

@ -0,0 +1,24 @@
#!/usr/bin/env ruby
# ExecSheetJS.rb (c) SheetJS LLC -- https://sheetjs.com
require "execjs"
require "base64"
source = File.open("xlsx.full.min.js").read;
context = ExecJS.compile(source);
puts context.eval("XLSX.version");
data = Base64.strict_encode64(File.open(ARGV[0]).read);
result = context.call(<<EOF, data);
function(data) {
var wb = XLSX.read(data, {type: 'base64'});
var ws = wb.Sheets[wb.SheetNames[0]];
/* to avoid re-parsing, CSV and XLSB are written in the same call */
return [
XLSX.utils.sheet_to_csv(ws),
XLSX.write(wb, {bookType: "xlsb", type: "base64"})
];
}
EOF
puts result[0];
xlsb = Base64.strict_decode64(result[1]);
File.write("sheetjsw.xlsb", xlsb, mode: "wb");