tgrosinger / md-advanced-tables

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

using sum() when there are commas in table cells? #66

Open luckman212 opened 3 years ago

luckman212 commented 3 years ago

This may be a hard limitation but, I was wondering, is it somehow possible to support math e.g. sum() functions when the numbers contain commas? E.g. see below:

| Item              | Grams |
| ----------------- | -----:|
| Whole Wheat Flour |   110 |
| Bread Flour       |   748 |
| Warm Water        | 1,691 |
| Salt              |    18 |
| Starter           |    40 |
| **Total Grams**   |   917 |
<!-- TBLFM: @>$2=sum(@I..@-1) -->

result: image

(should be 2607)

tgrosinger commented 3 years ago

Yeah, should be able to do that. I'll take a look next time I have that code open.

tgrosinger commented 3 years ago

Ah, ran into a hitch here. I can't blindly ignore commas because in some locales they are used instead of decimal points. That makes this a bit trickier. It looks like there is a native way to format numbers to the current locale, but not parse them. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat

luckman212 commented 3 years ago

Hmm, I see. What about adding a setting box where the user can input a "character to ignore" (simpler than trying to work this out automatically based on the num format or their regional settings etc).

I haven't looked yet at the code but if the function just runs a str.replace(/char/g,"") to strip them before performing the math—could that work?

goranche commented 6 months ago

should you go the "settings" way (adding characters to ignore), maybe also add a setting allowing the user to select what a decimal "point" is 🙈 (for me it's a comma 😆)