Visual Basic in Coda

This weekend we released our latest pack for compiling Visual Basic code into Coda Buttons and Automations.

This is useful for people who are migrating business workflows from EXCEL. Its trivial to import the data tables into Coda, and migrating the formulas is easy, as formulas are written using very similar languages.

But EXCEL Macros for buttons and automations are written in the ‘procedural paradigm’ of Visual Basic, which is far more difficult to translate into the ‘pure-functional paradigm’ of the Coda’s formula language.

With this pack, EXCEL experts can use the familiar structures, statements and techniques of Visual Basic when migrating or building automations in Coda.

(We are also launching our packs for Python and JavaScript for those migrating from Google Sheets and Apache Open Office Calc.


Lets start with an example of a canvas button that does some simple math using if and for statements.

Our beta testing showed us that makers preferred to edit their VBA code inside the button itself, rather than storing the code in a table elsewhere. So this is the method we devised to support that.

Inside the Division Table button formula we have the following code…

The first and last lines stores the VBA code in the Basic variable and then runs the VBA compiler to translate and execute the VBA code. The variable N is a canvas control value, set by the user to select the ‘Division Table’ to be printed. Those familiar with Visual Basic will instantly understand what this code does.

When you click on this button you see this pop-up dialog (the _Console)
image
The VBA code does the following;

  • Uses an if-then-else-endIf statement group to check that the divisor N is not zero.
  • If it is zero, it uses the error statement to display an error message at the bottom of the screen like this,
    image
  • Otherwise, a for-next statement block is executed with the variable I going from 1 to 12.
  • On each pass of the loop the print is executed to print the calculation to the _Console.
  • At the end, we increment the N variable by one (unless N=12 when it is set to 1)

When you install the Visual Basic pack in a document, it provides a Diagnostics page that shows the source code, the corresponding Coda code and the JSON of the executable object. So if the cde does not perform as you expected, you can investigate and resolve the issue.

There is also an option to show a Wayne Orr Structure Diagram of the executable object, which is easier to interpret if you are not familiar with JSON.

The next example is a button inside a table where the compiler must understand the Context of the button the user clicked, ie: which Row of which Table the clicked button is on. This allows the code to use the thisTable and thisRow variables. It also allows the interpreter to resolve the use of column names for this table to the values on the correct row of the table.

This simple example uses values in thisRow to set the values of a new row in another table. The scenario is as follows

The code inside the Add Order button is as follows…
image
In this case the first line passes the variable thisRow to Basic along with the VBA code so the compiler can use it to define the Context of the action.

This button does the following when clicked (again it is all familiar code to VBA developers)

  • uses the with new statement group to create a new row in the Order table
  • uses the .<attrubute>=<value> statements to set the Product, Amount, and DeliverBy columns of the new Order row
  • notice how Quantity and Price are assumed to be from thisRow, ie: the row where the button was clicked
  • the let OrderedAt=now() statement sets the OrderedAt attribute of thisRow to the current time-stamp
  • finally the show thisrow statement displays the current row as a pop-up dialog (similar to OpenRow() in Coda, like this…

Note that the executable object has the correct context values for thisTable (ObjectID) and thisRow (RowID) as seen below

We have refined the compiler process extensively over the last 18 months to fine-tune its performance. At present it takes between 2 milliseconds and 4 milliseconds per VBA source-line to compile the code because it is done on the client-side and does not involve a round-trip to the coda servers. So the additional time taken to do the JIT compilation is not noticeable in most real-world cases.

The Visual Basic pack is available now and the Python and JavaScript packs are to be released this week.

Please private-message me here, or mail me at Max.Xyzor@Agile-Dynamics.com if you are interested.

Respect
Max Xyzor
Agile-Dynamics

18 Likes

Sorry folks, there was meant to be a video demo with that last post. But the internet gremlins have nobbled the Youtube upload.

Meantime. here is a list of the Visual Basic statements supported by our pack.

S denotes a statement, N denotes a variable-name and E denotes an expression (called a formula in coda).

Parts in […] brackets are optional.
Parts in {…} braces are reoccurring.

print E1 {, E}

  • adds a line to the print area in the _console
  • line contains the values in the comma-seperated list

for N= E1 to E2 [step E3] {S} next [N]

  • loops setting index var N to values in sequence
  • E1…E2 by E3 if defined, by 1 otherwise

for [each] N in E1 [where E2] {S} next [N]

  • loops through elements in E1
  • for which E2 is true (if defined)
  • setting var N to the current element

with N=E1 {S} end with

  • sets temporary constant N to E1
  • which only exists within this block

with [new] E1 [where E2] {.N=E3} end with

  • selects the object E1 (creates new object if ‘new’ is included)
  • where the condition E2 is true (if defined)
  • sets each of its attribute N
  • to the value of E2

N=E

  • sets control value N to value of E

let [E1].N=E2

  • sets attribute N of object E1 (or thisRow if missing)
  • to the value of E2

show E1

  • pops-up a dialog modal for the row E1
  • allows the user to edit attributes
  • unless the page is locked

clear

  • clears the print area in the _console

call E1

  • calls the procedure defined by E1
  • awaits its return and continues execution

while E {S} wend

  • loops as long as E is true
  • if E is never false, will time-out after a hundred thousand iterations to prevent infinite loops

if E1 [then] {S1} [{ elseif E2 then {S2} }] [else {S3} ] end if

  • if the result of E1 is true, executes statements S1 and skips the rest
  • otherwise evaluates E2 for each elseif block (if present) and executes S2 if true
  • otherwise executes S3 if the else clause is present

Further statements are being added for V2 later this year.

Max

5 Likes

this is simply genius! In line with your intent to make the transition from Excel macros as smooth as possible for power users, you bypass any UX shenanigans and simply allow the user to insert their code as is most familiar to them.

But, lest users were to remain in their isolated basic Bubble, you equip them with all the info they need to make sense of how Basic is translated into CFL. Such a inspired way to drive user adoption!

The significance of this cannot be overstated. You are enabling users to hit the ground running in coda, without needing to spend tons of energy and time to recreate their basic setup in coda.

The fact that you then allow users to visualize the structure in a diagram is merely icing on the cake after that.

What. An. Accomplishment. And what an insanely valuable addition, further bridging the gap between power excel users and coda. Can’t wait to see to which heights you’ll take this!

5 Likes

here is a quick video showing how you create a button that contains visual basic code

max

3 Likes

This is so impressive! It’s a lot for me to digest, but I have a feeling the potential is huge!

2 Likes

Here is the published compiler, if you want to try it out.
Max

1 Like

Here is the VBA Installer Pack.

1 Like

smart interesting take indeed.

1 Like

Max truly is a Coda Yoda!