Increment

Creating a macro - Writing a Script - Using the API

Increment

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.
garrowolf
 
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
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 6518
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.
garrowolf
 
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:
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 13 times
On Windows 10: LibreOffice 5.4.4 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Maybe we might! (Create a powerful UFO: United Free Office)
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 1824
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 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Villeroy
Volunteer
 
Posts: 25373
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 10 guests