Raku in Excel and Google Sheets
Write a spreadsheet formula in Raku. Decimals are exact, integers have no size limit, and regexes and grammars work on the text in your cells. The engine, in WebAssembly, runs inside Excel, and for Google Sheets in a sidebar in your browser: nothing to install on your computer, and the formulas send nothing outside the spreadsheet.
=RAKU.EVAL("($^a - $^b) + 1", 43.1, 43.2) Excel
=RAKU("($^a - $^b) + 1", 43.1, 43.2) Google Sheets
Both are exactly 0.9. Excel itself works in doubles, which
cannot hold these decimals, and Microsoft's own example,
=(43.1-43.2)+1, shows 0.899999999999999
(Floating-point arithmetic may give inaccurate results in Excel).
What a formula looks like #
The code comes first. $^a, $^b, … are the values
after it, in order, and @_ is all of them, with ranges flattened
into their cells. These are the Excel spellings; in Google Sheets, write
RAKU for RAKU.EVAL.
=RAKU.EVAL("$^a - $^b - $^c", 0.5, 0.4, 0.1) | 0 |
=RAKU.EVAL("[+] @_", A1:A10), with 0.1 in every cell | 1 |
=RAKU.EVAL("[*] 1..$^n", 30) | 265252859812191058636308480000000, as text |
=RAKU.EVAL("(1/3 + 1/7).raku") | <10/21> |
=RAKU.EVAL("(1..4).map(* ** 2)") | 1, 4, 9 and 16, down a column |
=RAKU.EVAL("~($^s ~~ / \d+ ' kg' /)", "Box of 12 kg flour") | 12 kg |
=RAKU.EVAL("1/0") | #DIV/0! in Excel; an error saying so in Sheets |
Excel #
The Raku add-in gives Excel one more function, =RAKU.EVAL.
- Download the add-in's manifest, manifest.xml.
- Load it into Excel (Microsoft calls this sideloading):
- Excel on the web: open the Add-ins dialog, choose Upload My Add-in and pick the file (how).
- Excel for Mac: copy the file into
~/Library/Containers/com.microsoft.Excel/Data/Documents/wef/, making the folder if it is not there, and restart Excel. Then choose Home → Add-ins and pick Raku formulas from the menu (how). - Excel for Windows: put the file in a shared folder and add that folder under File → Options → Trust Center → Trust Center Settings → Trusted Add-in Catalogs (how).
- Click the Raku button on the Home tab, once. That starts the add-in for the first time, which is when Excel registers
=RAKU.EVAL, for you and every workbook from then on. Until then a formula shows#NAME?, and a cell that already does keeps it until you enter the formula again. - Type
=RAKU.EVAL(in a cell.
The pane the Raku button opens has examples to insert into the selected cell,
the engine's version, and what formulas printed with say. The
engine loads once per session, in a second or two; formulas that arrive
together run as one batch, so 200 of them cost about as much as one. Only
#VALUE! and #N/A can carry a message in Excel, so
most errors are #VALUE! with Raku's message under the cell's
error flag.
Google Sheets #
In Sheets the formula is =RAKU, and the engine runs in the
Raku sidebar, a page in your browser that loads it once and
keeps it. A small script attached to the spreadsheet adds the formula, the
sidebar and a Raku menu. It comes ready-built:
- Download raku-google-sheets.zip (9 KB) and unzip it: a folder with
Raku.gs,RakuSidebar.htmlandappsscript.json. - In the spreadsheet, open Extensions → Apps Script.
- Paste
Raku.gsinto theCode.gsthat is already there, and rename itRaku. Choose + → HTML, name the fileRakuSidebar, and pasteRakuSidebar.htmlinto it.appsscript.jsoncan be left out. - Save, and reload the spreadsheet. Choose Raku → Open the Raku sidebar; the first time, Google asks you to let the script show its sidebar and work on this one spreadsheet. Then
=RAKU("6 * 7")gives42.
With clasp, Google's command
line for Apps Script (Node.js 20 or later, and the Apps Script API turned on
at script.google.com/home/usersettings),
steps 2 and 3 are two commands, run where you unzipped the folder. The
spreadsheet's ID is the long part of its address, between /d/
and /edit.
clasp create-script --parentId SPREADSHEET-ID --rootDir raku-google-sheets
clasp push
Keep the sidebar open while you work. A new or changed formula shows
⏳ Raku sidebar until the sidebar has calculated it; the sidebar
looks for such cells every second and runs them as one batch. Results
already calculated stay in their cells, for anyone who opens the
spreadsheet. The sidebar also has the examples, the Raku sheet's buttons,
and what formulas printed with say.
Your own subs: the Raku sheet #
Add a sheet named Raku and write Raku in column A, one line per
row. Every formula in the file can call what it defines. The Raku pane in
Excel and the Raku sidebar in Sheets both have a button that adds the sheet
with two examples on it:
# =RAKU.EVAL("iban-ok($^s)", A2) is TRUE for a valid IBAN: 30 digits, modulo 97, exactly.
sub iban-ok(Str $iban) {
my $t = $iban.uc.comb(/<alnum>/).join;
my $digits = ($t.substr(4) ~ $t.substr(0, 4)).comb.map({ /\d/ ?? $_ !! .ord - 55 }).join;
$digits % 97 == 1
}
The spreadsheet does not know that a formula depends on the Raku sheet. In Sheets, the Raku sidebar notices a change to it and calculates the formulas again; in Excel, press Recalculate in the Raku pane after you change it.
More #
The guide covers the rest: how values go in and come out (an empty cell, a date, a range, a result too big for a double), the limits (no files or network, about a hundred levels of recursion), and how the add-in and the script are built.