Increment

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
garrowolf
Posts: 26
Joined: Thu Jan 24, 2008 11:14 am

Increment

Post by garrowolf »

I am trying to figure out how to increment the contents of a cell. I am going to assign it to a button so I can press one button and have several other cells increment and some decrement. I figured out how to create a macro but I can't find anything on this.
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Increment

Post by FJCC »

This macro increases the value of Sheet1.A1 by one with each execution.

Code: Select all

oSheets = ThisComponent.Sheets
oSheet1 = oSheets.getByName("Sheet1")
oCellA1 = oSheet1.getCellrangeByName("A1")
ValueA1 = oCellA1.Value
oCellA1.Value = ValueA1 + 1
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.
garrowolf
Posts: 26
Joined: Thu Jan 24, 2008 11:14 am

Re: Increment

Post by garrowolf »

Thank you! Let me go try it out.
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Increment

Post by Lupp »

Concerning the original demand to increment / decrement more than one cells by different amounts in one go:

If you want to apply some incrementing / decrementing of cells by a Sub invoked by a click on an OK-button, you have a basic problem:

A) Either you hard-code the information which cells to treat in what way in the Sub itself OR
B) You find a way to pass that information to a Sub capable to interpret it as needed. This is not specifically supported.

Some viable ways to pass parameters as option B requires you find listed in my recent answer to this question in a different forum:
https://ask.libreoffice.org/en/question ... arameters/ .

All these ways suffer, however, from the need to create/apply a specific syntax for the parameter list, and to analyse it by a Sub.
Using the 'Tag' property 'Additional information' of a form control you can at least write the parameter list in a similar way as it would occur in a function call.

See the attached demonstration.

You need to check the included code to be sure it is safe, and to reload the file then permitting macro execution.
(Of course, you can also move the module to your standard library or create your own version there.)
Attachments
aoo93394IncDecButton_1.ods
(11.66 KiB) Downloaded 113 times
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Increment

Post by Villeroy »

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
Post Reply