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")
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)
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)
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: 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
And see how it grows:
Chart of The first n fibonacci numbers