[Solved] Best approach for a counter ?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sigridt
Posts: 2
Joined: Sat Jul 24, 2021 10:05 pm

[Solved] Best approach for a counter ?

Post by sigridt »

Hi,
I would like to have a cell's value be automatically added with one each time a certain keystroke or mouse click occurs. So the cell would show the number of times the event occured. When the document is saved, next time the counter should continue (not starting from zero each time). But I have to be able to reset the value, and occasionally go back a few numbers - maybe using another key to hit.

What would be the best approach to make such a row counter ? Or maybe it already exists ? How to search that?

Thanks for your help.
Sigridt

(it would be a row counter for my knitting projects,and based on the value of this counter some rows in my pattern would change background color using conditional formatting - this is called a Knit leader, or charting device)

libre office 7.4.1.2 on ubuntu 21.04
Last edited by sigridt on Sun Jul 25, 2021 6:20 pm, edited 1 time in total.
libre office 7.4.1.2 on ubuntu 21.04
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Best approach for a counter ?

Post by Villeroy »

You are running Linux. You don't need any 350 MB office suite for this trivial task. A littel script does the job
Save this as ~/bin/counter.sh and make it executable.

Code: Select all

#!/bin/bash
COUNTFILE=~/counter.txt
COUNTVAR=0

function finish {
    echo $COUNTVAR > $COUNTFILE
    echo -e "\nCaught EXIT and saved $COUNTVAR to $COUNTFILE"
}

trap finish EXIT

if [ -f $COUNTFILE ]; then COUNTVAR=`cat  $COUNTFILE`; fi
while true; do
    echo -e "Add integer to $COUNTVAR [default 1]: \c"
    read NUM
    if [ -z $NUM ]; then NUM=1; fi
    let COUNTVAR+=$NUM
done
Execute the script:

Code: Select all

$ counter.sh 
Add integer to 161 [default 1]: 
161 is the number stored in ~/counter.txt. Every time I hit Enter, 1 is added. Every time I enter an integer number, that number is added to the number in ~/counter.txt
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Best approach for a counter ?

Post by FJCC »

There are keyListeners and mouseListeners, though I do not know if they can be attached to a whole sheet. Here is a thread where a keyListener is discussed with respect to a dialog.
viewtopic.php?f=20&t=75537
It would be easy to count rows using normal spreadsheet functions if the cells were marked with some content. For example, counting all of the cells that contain a certain character. Is using something like that feasible?
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Best approach for a counter ?

Post by JeJe »

What's the "certain keystroke or mouse click"?

You can easily assign a shortcut key or assign a macro to the right click or double click sheet events.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Best approach for a counter ?

Post by Villeroy »

Another approach that works with a GUI, with keystrokes, which does not throw away the entries and which also stores timestamps.
Open the form, click the numeric input box.
Use key up to add 1 up, key down subtracts 1, the Enter key stores the number with timestamp in the underlying database just in case you are interested in how much has been counted when.

http://forum.openoffice.org/en/forum/do ... hp?id=3048
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
sigridt
Posts: 2
Joined: Sat Jul 24, 2021 10:05 pm

Re: Best approach for a counter ?

Post by sigridt »

Thanks to all! Great answers. :)
libre office 7.4.1.2 on ubuntu 21.04
Post Reply