# Solve math formula that is currently text in a cell using a button? (trying to make a calculator)

I’m not sure if I’m missing something obvious or if this isn’t a function, but I’d like to be able to solve a math equation, written in the form of say something like: 9+(5*2)/10-2 that has been entered into a table as text.

If I enter that into a formula directly, it renders the answer as 8. But if I have it entered into a cell of a table, all I can do is call up the row.column, and it displays “9+(5*2)/10-2”, but I can’t solve it

This came up when I was trying to make a calculator template I could paste into docs when convenient. Realized after I built it to do some basic functions that it would be very hard to render complex formulas like the eg above. But also that it would be fairly easy if I could just use the buttons to type into a cell and then another button to solve the equation when I’m done.

here’s the semi functioning calc I’ve built

1 Like

With some black magic, everything’s possible

It’s not obvious though, and not a recommended solution. This takes a button, extracts its code and replaces a piece of that code with the expression. Then creates an Action object out of that new adjusted code and sets it on another button. Please mind that the code uses slightly different notation than Coda formulas, e.g. you cannot reference rows and columns like you’d do in Coda formula language. For literal numbers it works though.

4 Likes

@Paul_Danyliuk could you add some illumination on `_Merge()` and `_Deref_Object()`? It seems like these help create raw Coda notation, which is why they don’t update/reference as usual, correct?

I haven’t explored `_Merge()` to its fullest (i.e. what’s its intended use). All I know is that it can return an object that describes any provided Coda value, reference, control etc. By adding a blank text line `+ ""` it converts that object to its text representation, which is JSON.

Then you can manipulate that JSON as regular text, e.g. replace parts of an action formula, manipulate list nesting level, manipulate character and paragraph styling data etc.

Then you can turn JSON back into an object using `ParseJson()`. However Coda won’t convert objects to “meaningful” Coda objects unless they are nested at least one level down. Hence what I’m doing is that I’m making an object out of:

``````{"o": <actual object here>}
``````

and then extracting the value of key `"o"` using `_Deref_Object(object, key)`, which is again a control, an action, a reference etc.

1 Like

This is amazing, thanks. I had to read your comment like five times but I get the shape of how it works, but I think I do now. Very interesting for sure. I get why it’s not a recommended solution because it seems like it would break pretty easily if something changes in how buttons are coded

Still probably gonna use it for now though for my personal little calculator until it breaks. I was surprised there was no simple calculator template out there, but i guess most people would just open the one on their computer. I liked the idea though of having the calc template I could bring in anywhere I was in coda, and have the answers as data that I could throw into whatever doc I’m in more easily than the windows calculator

Thanks for the brain stretch and temp solution!