Skip to content

expandFormula

This function is very useful, but quite hard to explain. It takes a few strings as arguments, and replaces itself with a formula. The best way to learn what it does is by looking at simple examples or real-world applications. For the inquisitive, there is also a section on how it works.

expandFormula(
    formulaString,
    expansion1,
    [expansion2,]
    […]
)

Arguments

formulaString
A string representing the original formula, which will be expanded according to the following arguments.
expansionString1
The first expansion expression.
expansionString2, …
More expansion expressions. Optional.

Examples

Single expansion

expandFormula(
    "foo(x)",
    "x = 4 * [A] + 6 * [B]",
)

resolves to

4 * foo("A") + 6 * foo("B")

Single expansion with nested functions

expandFormula(
    "foo(bar(x))",
    "x = 4 * [A] + 6 * [B]",
)

resolves to

4 * foo(bar("A")) + 6 * foo(bar("B"))

Multiple expansions

You will only see the end result, but we show the internal steps here for clarity:

expandFormula(
    "foo(x, y)",
    "x = 4 * [A] + 6 * [B]",
    "y = 1 * [C] + 3 * [D]",
)

resolves to

expandFormula(
    "4 * foo("A", y) + 6 * foo("B", y)",
    "y = 1 * [C] + 3 * [D]",
)

which resolves to

expandFormula(
    "1 * (4 * foo("A", "C") + 6 * foo("B", "C")) + 3 * (4 * foo("A", "D") + 6 * foo("B", "D"))",
)

which resolves to

1 * (4 * foo("A", "C") + 6 * foo("B", "C"))
+
3 * (4 * foo("A", "D") + 6 * foo("B", "D"))

Multiple expansions with nested functions

You will only see the end result, but we show the internal steps here for clarity:

expandFormula(
    "foo(x, bar(y))",
    "x = 4 * [A] + 6 * [B]",
    "y = 1 * [C] + 3 * [D]",
)

resolves to

expandFormula(
    "4 * foo("A", bar(y)) + 6 * foo("B", bar(y))",
    "y = 1 * [C] + 3 * [D]",
)

which resolves to

expandFormula(
    "1 * (4 * foo("A", bar("C")) + 6 * foo("B", bar("C"))) + 3 * (4 * foo("A", bar("D")) + 6 * foo("B", bar("D")))",
)

which resolves to

1 * (4 * foo("A", bar("C")) + 6 * foo("B", bar("C")))
+
3 * (4 * foo("A", bar("D")) + 6 * foo("B", bar("D")))

Using with TableLookup

expandFormula with TableLookup is useful for looking up values in various tables, columns or rows without repeating the same formula manually.

Example Result
expandFormula(
    "TableLookup('table1', 'row1', col)",
    "col = [A] + [B] + [C]",
)
            
TableLookup('table1', 'row1', "A")
+ TableLookup('table1', 'row1', "B")
+ TableLookup('table1', 'row1', "C")
            
expandFormula(
    "TableLookup('table1', row, col)",
    "col = [A] + [B] + [C]",
    "row = [X] + [Y] + [Z]",
)
            
TableLookup('table1',"X","A")
+ TableLookup('table1',"X","B")
+ TableLookup('table1',"X","C")
+ TableLookup('table1',"Y","A")
+ TableLookup('table1',"Y","B")
+ TableLookup('table1',"Y","C")
+ TableLookup('table1',"Z","A")
+ TableLookup('table1',"Z","B")
+ TableLookup('table1',"Z","C")
            

Using with loopTable

expandFormula with loopTable can be used to quickly created nested loops, or reference multiple columns in the same loop, without repeating the same formula manually.

Example Result
expandFormula(
    "loopTable('table1', col)",
    "col = [A] + [B] + [C]",
)
            
loopTable('table1', "A")
+ loopTable('table1', "B")
+ loopTable('table1', "C")
            
Example with multiple loops coming soon…

Using with arbitrary formulas inside the placeholders

In the previous examples, the contents of the [] placeholders were always interpreted as strings. Sometimes it may be necessary to specify a more complex formula inside a placeholder in the expansion expression.

Coming soon

This feature is in development, and not available to all users yet.

Edge cases

These examples are a bit more obscure, but follow the same pattern as the previous examples.

No placeholders

Example Result Comment
expandFormula("foo(x)", "x = 4 * [A]") 4 * foo("A") Basic example.
expandFormula("foo(x)", "x = [A]") foo("A") Remove the 4 *. Intuitive enough.
expandFormula("foo(x)", "x = 4") 4 This might not be intuitive, but correct.

Empty placeholders

Example Result Comment
expandFormula("foo(x)", "x = 4 * [A]") 4 * foo("A") Basic example.
expandFormula("foo(x)", "x = 4 * []") 4 * foo("") Empty string, not an omitted argument.

Empty expansion expression

Example Result Comment
asdf(expandFormula("foo(x)", "x = [A] + [B]")) asdf(foo("A") + foo("B")) Basic example.
asdf(expandFormula("foo(x)", "x = [A]")) asdf(foo("A")) Remove one placeholder.
asdf(expandFormula("foo(x)", "x = ")) asdf() Empty formula, not an empty string.

Real-world applications

Coming soon

Links to tutorials with real-world applications.

How it works

Coming soon