Skip to content
JS Runner
Share
Explore

icon picker
Formula ExecuteFunction

The JS Runber pack for Code provides the Formula ExecuteFunction. ExecuteFunction requires at least two parameters:
code: Javascript code defining one or more function.
functionName: Name of the function in the Javascript code to execute.
The simplest possible example (and rather useless):
ExecuteFunction("function sumOfTwo() { return 1+2;}", "sumOfTwo")
The result is:
3
Additionally parameters can be passed to ExecuteFunction. Each of these parameters are provided to the function with functionName.
A simple example to calculate the sum of two values is:
ExecuteFunction("function sum(v1, v2) { return v1+v2;}", "sum", 1, 2)
And the result is:
3
Now you can calculate the sum of any two numbers!

A more complex example

The following shows a more complex example, it allows to calculate the :
ExecuteFunction("
function fibonacci(num) {
if(num < 2) {
return num;
} else {
return fibonacci(num-1) + fibonacci(num - 2);
}
}", "fibonacci", 10)
The result is:
55
Please note that this is a very inefficient algorithm to calculate the fibonacci number, it only serves as a simple example here.

Reference code from a formula

You can "store" your code in a formula, name the formula and then reference the code later to reuse it:
function fibonacci(num) { if(num < 2) { return num; } else { return fibonacci(num-1) + fibonacci(num - 2); } }
Use it like this:
ExecuteFunction(thisDocument.fibonacci, "fibonacci", 10)
And the result is again:
55
You can use the pack to analyse your code:

Reuse code from a formula in a table

Once you have the code reusable, you can use a table to calculate a set of values:
The first n fibonacci numbers
1
N
Fibonacci(N)
1
1
1
2
2
1
3
3
2
4
4
3
5
5
5
6
6
8
7
7
13
8
8
21
9
9
34
10
10
55
There are no rows in this table
And see how it grows:
Chart of The first n fibonacci numbers
1


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.