Creating a macro - Writing a Script - Using the API


Postby garrowolf » Fri Apr 27, 2018 10:41 pm

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.
Posts: 26
Joined: Thu Jan 24, 2008 11:14 am

Re: Increment

Postby FJCC » Fri Apr 27, 2018 10:47 pm

This macro increases the value of Sheet1.A1 by one with each execution.
Code: Select all   Expand viewCollapse view
oSheets = ThisComponent.Sheets
oSheet1 = oSheets.getByName("Sheet1")
oCellA1 = oSheet1.getCellrangeByName("A1")
ValueA1 = oCellA1.Value
oCellA1.Value = ValueA1 + 1
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Posts: 7153
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Increment

Postby garrowolf » Fri Apr 27, 2018 11:01 pm

Thank you! Let me go try it out.
Posts: 26
Joined: Thu Jan 24, 2008 11:14 am

Re: Increment

Postby Lupp » Sat Apr 28, 2018 9:39 am

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: ... 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.)
(11.66 KiB) Downloaded 31 times
On Windows 10: LibreOffice 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
Lupp from München
User avatar
Posts: 2471
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Increment

Postby Villeroy » Sat Apr 28, 2018 3:29 pm

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.x
User avatar
Posts: 26728
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Return to Macros and UNO API

Who is online

Users browsing this forum: AndresSolar and 4 guests