tgrosinger / md-advanced-tables

A text editor independent library to enable formatting and Excel-style navigation, and spreadsheet formulas to Markdown tables.
MIT License
151 stars 30 forks source link

Support for strings and numbers in formulas #43

Open FredD opened 2 years ago

FredD commented 2 years ago

Tony Grosinger

Thanks for the wonderful Advanced Table plugin. I have two requests which would make it even more wonderful for me.

The requests have to do with the formula functionality, which makes this plugin so useful for me.

The ability to add a raw number into a formula. Currently, it seems in order to reference a value you need to have it in the table. For instance, to divide a number, you need to have the divisor somewhere in the table.

The ability to add string literals into the outcome of a function (24 hours) (16 minutes)

Many thanks for this plugin, I use it every day

agnoldo commented 2 years ago

+1

Totally agree with the amazingness of Advanced Tables and with the usefulness of this idea. With string concatenation, it's possible to create dynamic links to web pages, for example. Suppose you have an id in the first column and a country name in the third column: with string concatenation we can create a last column with links to the object (using id) and to the country in any web app (Wikipedia, for example) using the country name.

Thanks for the great work!

cthulhubuddha commented 2 years ago

+1 also. Would it be possible to, for example, sum the values of a column but format the cell to read Sum:: ? And if so, would I then be able to use dataview to select that value from multiple notes? So for example, if I have a folder of author notes, each author note has a table of the book titles i own and their value, i can currently sum the value of the books per author note. But i haven't been able to figure out how to take the sums from each note and provide a total collection value. If each note's tables can have a Sum:: that comes from advanced tables formula that supports strings that might allow dataview to pick up that field for each note to sum all of them. Also, there may be a way easier way to do what i'm thinking. Thanks either way!

weltenwort commented 1 year ago

The ability to add a raw number into a formula. Currently, it seems in order to reference a value you need to have it in the table. For instance, to divide a number, you need to have the divisor somewhere in the table.

@FredD, just to make sure you're aware: You can use constant numbers (called real and float in the grammar) in the formula:

https://github.com/tgrosinger/md-advanced-tables/blob/b292421df65b40ade498f4af031631664a9aa1a8/src/calc/calc.ts#L35

What tripped me up at first is the fact that every arithmetic operation in infix notation needs be surrounded in parentheses:

https://github.com/tgrosinger/md-advanced-tables/blob/b292421df65b40ade498f4af031631664a9aa1a8/src/calc/calc.ts#L56

So @2$2=@2$1*0.5 can't be parsed, but @2$2=(@2$1*0.5) should work.