# Total

Posted by dchar, Last update 8 months ago

Action directory: https://actions.getdrafts.com/a/1Pu

Discuss in Drafts Forum

This action adds up each line of a draft and displays the total. It’s handy for those cases in between “I should use a calculator” and “I should use a spreadsheet”. Some use cases: adding up receipts, keeping track of per diem, splitting a bill.

Numbers can be integers or decimal:

``````10
3.75
``````

Text, and everything after it, is ignored which is handy for adding notes:

``````# Twelve Days of Christmas

3  French hens
2  Turtle dovers
1  Partridge in a pear tree
``````

Simple calculations can be done on each line:

``````5.00 * 2   multiplication
4.25 x 3   quicker than find multiply
100 / 2    division
``````

### Steps

• #### script

```// Evaluate lines and accumulate the total (() => { const lines = () => { return editor.getText().split('\n'); }; const sum = (acc, l) => { try { l = l.replace(/\\$/g, ""); // Convert `2x3` to `2*3` l = l.replace(/[xX]+/g, "*"); // Text to comments before eval l = l.replace(/[a-zA-Z]/,"//"); l = l.replace(/#/,"//"); let f = eval(l); if (!Number.isFinite(f)) { f = parseFloat(l); } if (Number.isFinite(f)) { acc += f; } } catch(e) { } return acc; }; const t = lines().reduce(sum, 0); alert("total: " + t.toFixed(2)); })();```

### Options

•  After Success Nothing Notification Info Log Level Info