Counting trip expenses

Feels a bit silly, but on the other hand Excel or programming are overkill for the task. And this way it’s editable on mobile and kept together with other notes about the trip :slight_smile:

The page can have one or more tables, and then I pass a query to a custom function:

```template
**Total cost: {{sum("cost", {table where page = @page.name})}}€**
transport: {{sum("cost", {table where page = @page.name and tags = 'transport'})}}€
```

|Description|Cost|
|-----------|----|
|train ticket #transport|6.25|
|supermarket|15.12|

The sum function is defined in this Space Script

silverbullet.registerFunction("sum", (key, items) => {
  return items
    .map(i => parseFloat(i[key]))
    .reduce((acc, n) => acc + (isNaN(n) ? 0 : n), 0)
    .toFixed(2);
});
7 Likes

Ah nice use case for the table indexing. Bye bye spreadsheets! :joy:

I love this. Thanks for sharing!
Perfect timing as I am reading this feature while being on vacation and sharing costs.

I have been using for 3 trips so far SilverBullet for cost tracking, though through a simple bullet list. No automatic calculations.

This will save me time.
It would be amazing to enhance it to provide cost-sharing, as I normally create the cost tracking during vacation because I need to share the expenses with other people and at the end of the trip we make numbers.

[UPDATE] I guess I can use tags for it for now :slight_smile:

1 Like

Well, thanks again for doing this.
I just added some more formulas and now I have what I need.

Example with splitting expenses between 2 families (1 of 3 people and the other of 2, so we should pay different amounts at the end of the trip)

5 Likes

the script gave me wrong sums, then after some troubleshooting i realized its not counting lines with empty colums.

as you can see in the image, only the items where link is present are summed. any idea how to fix this?

image

I’m afraid that this is not my fault, but a bug in how SilverBullet handles empty cells, that seems to be previously unknown. After a bit of my troubleshooting I described it in the general case on GitHub:

To work around the bug with empty cells in your specific table, you can either:

  • put any text in each cell of the “Link” column, like N/A or TODO or just a . (dot)
  • reorder the columns so the “Price” column isn’t after any column that may be missing items; in your example that order would be “Item”, “Price”, “Link”