shabegom / buttons

Buttons in Obsidian
The Unlicense
477 stars 50 forks source link

Tally counter #135

Closed IamSmeagol closed 8 months ago

IamSmeagol commented 1 year ago

I would love it if the calculate button could be modified into working as a tally counter. Instead of outputting the result on a new line, it would replace the existing value.

Thank you for the button plugin!

shabegom commented 1 year ago

You could do this using templater and a template button.

Let's say your count happens on line 10 of the note (based on the line numbers which are 1-based)

Your templater command would be something like:

<%*
const content = tp.file.content.split("\n")
const currentCount = parseInt(content[9])
const newCount = currentCount + 1
tR += newCount
-%>

And then the button would be:

name tally
type line(10) template
action Tally Template
replace [10,10]
IamSmeagol commented 1 year ago

I tried using that, but it returns NaN. I can't seem to find a way that works while still replacing the value.

shabegom commented 1 year ago

NaN means not a number. Which means the line number is wrong or you have things on that line which isn't a number.

IamSmeagol commented 1 year ago

I tried changing the numbers within the code and locations of the button and number, but I could only get it to send the calculation separately. When it replaced the number it always sent NaN, it seemed like the template appeared where the number was and then it couldn't calculate.

Sorry I don't understand why its not working 😕

shabegom commented 1 year ago

An yeah. That would explain it. Will have to add the template elsewhere and adjust the logic for it to replace the number.

Take a look at this basic read/replace example: https://shbgm.ca/blog/obsidian/Templater+Snippets#Basic+read+replace

IamSmeagol commented 1 year ago

Thank you for the help, I got it the map working (shown in the console that the number become 2). However, I can't get the modify to work. image

shabegom commented 1 year ago

Whaaat is that? That's not how you do a template button. Check the readme or try using the button maker. 😅