← Embed Raku

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 runs inside the spreadsheet, in WebAssembly: nothing to install on your computer, and the formulas send nothing anywhere.

=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 cell1
=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.

  1. Download the add-in's manifest, manifest.xml.
  2. 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).
  3. 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.
  4. 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 inside an Apps Script attached to the spreadsheet, which also adds a Raku menu. The script comes ready-built:

  1. Download raku-google-sheets.zip (3.7 MB) and unzip it: a folder of nine .gs files and an appsscript.json.
  2. In the spreadsheet, open Extensions → Apps Script.
  3. Give every .gs file a script file of the same name, and paste its contents in whole. Raku.gs goes into the Code.gs that is already there, renamed Raku; for each of the other eight, choose + → Script and name it after the file, without .gs. The five RakuWasm files are the engine, about 1 MB each. appsscript.json can be left out.
  4. Save, and reload the spreadsheet. The Raku menu appears, and =RAKU("6 * 7") gives 42. The first time you use a menu item, Google asks you to authorize the script for this one spreadsheet.

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

Every Sheets formula is its own script run, and each may load the engine again, so give one formula a whole range rather than writing one formula per cell: one load, and a column or a table of results.

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 menu 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. After you change it, press Recalculate in Excel's Raku pane, or choose Raku → Recalculate RAKU formulas in Sheets.

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.