A Demo of Javascript translation to Coda

Again, this is just a proof of concept.
just to demonstrate that it is possible to take Javascript code and run it in a Coda document.

only a tiny subset of the language is covered in this demo.
later i will expand it to cover most of the useful statement types and to provide some syntax checking and error messages etc.

but for now - it shows what is possible

max

6 Likes

I see you’re having a kick out of it :grin:

Looking at your doc, I’m wondering — do you not have to use $$[] references in your constructed action anymore? I mean, is it sufficient now to just {"type":"PendingEvaluation","formula":"CUSTOMERS.nth(c).ORDERS.formulamap(...)"}?

2 Likes

yes @Paul_Danyliuk, that is the case

the clf intrepreter only needs the identifiers in text. it returns quite useful error messages if the cfl is ill-formed.

i am sure there is a performance hit, the $$[…] form is presumably faster.

think of it as an eval() function ala javascript…
not as optimized as compiled code, but the late-binding is extremely useful.

the location: nodes which give the table, column and row info for the formula must be given as grid references so the intrepreter knows the context, so that column names and thisRow can be intrepreted correctly.

i suspect that we will now be able to find MANY powerful uses of this variation of the Black Magic technique

respect
max

1 Like

Oh cool, and I was painfully constructing each and every $$[] in my dedupe template… twice, because I first created it with the new editor and could not make into a template.

1 Like

@Paul_Danyliuk

i saw that in the video and assumed it was for performance reasons.

i also noticed in that video that you have a progress bar inside your buttons! looks amazing.

have you shown how to do that in one of your videos/posts/docs elsewhere?

max

2 Likes

Hey @Xyzor_Max ,
I was taking a look at your demo doc, but it is view and non-copy, so there isn’t all that much to see. Is is intentionally shared this way?
Greetings,
Joost

1 Like

@joost_mineur ,

hmm. my intention is to share the knowledge behind the technique.

so it should be playable and the formulas should be visible. no?

you should be able to modify the javascript code and press Run! to see the code running.

meantime i am building a more complete document that explains, step by step, how to implement this.

respect
max

2 Likes

No copy and view only…
To look at the formulas, I would like the no-copy option to be copy, makes it easier to look at the details.
Not sure how that is going to work for people that don’t have the pack option yet (like me :frowning: )

@joost_mineur

no problem. will change permissions.

and this has nothing to do with packs… its all pure coda formula language

but

it does use hidden, unsupported functions like _Merge(), _Deref_Object() and _Noop()

which is why i am doing a bigger doc to explain the dependencies and cotcha’s

for example, typing code into a table cell will do crazy things with certain characters (eg. there are SIX types of quotation characters) even if you set the format to ‘code block’. the RegexReplace() function needed to fix all this is a nightmare :wink: and still a w.i.p

i am also working on a proper lexical analyser to at least ensure the syntax is correct and that the cfl it emits is reasonably correct.

so meantime i just pushed out this demo doc ahead of time before its production ready because i think the tricks it uses will be of use to other makers

its now copyable and i put a copy doc button at the top

respect
max

Hey Max,

Thank you so much, for your efforts and sharing. Looking forward to you next doc, in the meantime I will enjoy myself looking at the current version.

Greetings, Joost

1 Like

Hello @Xyzor_Max ,

I just got done looking in dept at your doc: very impressive, you put a lot of effort into making this work and to ‘finish’ it will take a lot of extra effort.

Again, thanks for sharing, very inspirational.

Greetings, Joost

Ha, this is neat. I tried to do a pack with return new Function(input1)(input2) but dynamic code evaluation is prohibited by the compiler so I wasn’t able to. I can see you’re getting around that by using CLF to parse it somehow? Gonna dig into your code a bit more to see where the secret sauce is, how do you actually generate executable code in your pack?

thanks @joost_mineur and @Mykola_Bilokonsky

its not a pack

i am building on the “Black Magic” hack from @Paul_Danyliuk (and others)

i extract the inner JSON of an existing button, modify it to insert the text of the code i want to execute to get a new Action which can be executed using RunActions().

so no pack involved so far. but it uses hidden unsupported formulas, and will fail if coda changes any of the many details of how buttons or actions are managed.

i do want to encapsulate this into a pack to minimise such risks, improve performance, provide syntax and error checking etc

however, the reason the eval() and execute() functions of javascript are NOT provided for packs is to ensure security. dont want anyone inserting dodgy unsafe or malignant code via that trojan horse!

BUT

this technique only generates native coda formula code that will run WITHIN the owners own document. so it cannot do anything nasty that the user couldnt do with bog-standard formulas anyway.

as well as being a fun thing to do, my clients have TONS of existing BASIC and JS code snippets in their thousands of spreadsheets (excel and google sheets) and lots of experience writing automations in those languages.

so i am on a ‘Great Quest’ to find a way to migrate such workflows without having to re-write every line of code.

migrating native excel formulas to coda would help as well. changing the A99 cell refs into column names etc, so its gonna be a long and facinating Quest

if you know anyone looking to migrate existing workflows from excel, access, sheets or notion, please let me know. i have lots of tools to help that process

respect
max

1 Like

Yeah, none of the known eval methods would work in the Packs SDK :slight_smile:

That said, I wouldn’t be the Paul… :grin:

BTW @Xyzor_Max I am actually super glad that I didn’t just give you the above implementation, and you went ahead and built your own one in Coda :wink:

3 Likes

Is that pack reaching out to an evaluation server to do the actual dynamic part? That’s the best way I can think of to make this work…

Where can I learn about this so-called “Black Magic”? Are these just the undocumented APIs? I can see you’ve got some way to extract JSON from Coda values…

Can you share your code for your evaluate pack?? Super interested!! Awesome!

That’s a good idea but no :slight_smile:

I found a JS interpreter written in JS and included it’s whole code within the Pack :joy: (because I’m too lazy to go the CLI way and do it with modules)

I did a very crude job making it work (basically slapped a lot of : any to shut up TSLint and also removed anything around web workers APIs) so it stopped working reliably lol. That’s why I never shared it or published it… or took any money for it :wink:

3 Likes