[Solved] Change status of Radio buttons on Calc from a macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
alb21
Posts: 6
Joined: Tue Apr 10, 2018 8:06 am

[Solved] Change status of Radio buttons on Calc from a macro

Post by alb21 »

Can you guys please help me with this one? I have a spreadsheet with a group of three radio buttons called "Rates". The labels of the buttons are A, B, and C. Of course the user can select any one at will, but eventually I need to select one of them using a macro. I can only find in the forums how to read which one is selected, but I haven't been able to find how to change their status. Your help will be much appreciated.
Last edited by RoryOF on Wed Mar 13, 2019 12:43 pm, edited 2 times in total.
Reason: Added green tick [RoryOF, Moderator]
Apache OpenOffice 4.1.5
Windows 10 Home
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Change status of Radio buttons on Calc from a macro

Post by JeJe »

.state = 1 or .state = 0
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Change status of Radio buttons on Calc from a macro

Post by Zizi64 »

Download, install and use one of the invaluable Object inspection tools like the MRI or the XrayTool for easy examination the properties, methods and other things of the programming objects.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
alb21
Posts: 6
Joined: Tue Apr 10, 2018 8:06 am

Re: Change status of Radio buttons on Calc from a macro

Post by alb21 »

The .status thing doesn't work for me probably because my main problem is to identify the control, I have tried ByIndex, and ByName, but, for instance, I don't know how to make up the name of the control out of the group name and the label of the control. In other words, thank you for your help with what goes to the right of the dot in ".status", can you give me a hint about what goes to the left?
Apache OpenOffice 4.1.5
Windows 10 Home
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Change status of Radio buttons on Calc from a macro

Post by Zizi64 »

Please, upload your macro code and an ODF type sample file here.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
mikele
Posts: 72
Joined: Wed Nov 21, 2018 11:11 am
Location: Germany

Re: Change status of Radio buttons on Calc from a macro

Post by mikele »

Hello,
first of all it is important to notice that a group box (I guess "Rates" is one) is not connected to the elements inside the box. It's only a graphical elelement. Each of your radio-buttons has an (unique) index and an name. The easiest way is to give each of the radio-buttons a unique name too (unless it is already made), let me say radio1, radio2 and radio3.
Then you can access these radio-buttons (at first sheet, with one form):

Code: Select all

oButton=ThisComponent.Sheets(0).Drawpage.Forms(0).getByName("radio1")
LibreOffice 5.4, 7.0, 7.2 on LinuxMint/Win10
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Change status of Radio buttons on Calc from a macro

Post by Zizi64 »

Your macro works for me in my AOO portable 4.1.5, and LibreOffice 6.1.5.
Radio.ods
(11.98 KiB) Downloaded 227 times
Please upload your sample file here.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Change status of Radio buttons on Calc from a macro

Post by JeJe »

You can also identify a control from its label if its unique.

Code: Select all


for each c in Thiscomponent.Sheets(0).Drawpage.Forms(0) 'loop through every control
if c.label = "Option Button1" then  'if its got the label you want then
c.state = 1
exit for
end if
next

Edit: or some other unique property, for example you could set the .tag property to a unique string value.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Change status of Radio buttons on Calc from a macro

Post by RPG »

I think alb21 does have made his controls with a wizard. Then the radiobuttons and the group control do have the same name. You can not access it with getbyname. First you have to access the group with getgroupbyname. This return an array with the controls. You access then with a number as normal.

Code: Select all

Sub Main
'Define first variables
dim oDoc
dim oSheet
dim oForm
dim oGroupbyname
dim mGroupForButtons()
dim oButton
oDoc=Thiscomponent
oSheet=Thiscomponent.getSheets.getByname("Blad1") 'For sheet
oForm=oSheet.getdrawpage.getforms.getbyname("Formulier") ' For form
oForm.getgroupbyname("Rates",mGroupForButtons()) ' for the controls who have the same name as you see in the formnavigator
oButton=mGroupForButtons(1) 'Take a control from the group
oButton.state= cint(not(oButton.state) ) Toggle it
End Sub
You can find an earlier example.
[Solved] Check box problem (View topic) • Apache OpenOffice Community Forum

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
alb21
Posts: 6
Joined: Tue Apr 10, 2018 8:06 am

Re: Change status of Radio buttons on Calc from a macro

Post by alb21 »

Thank you very much to everybody for your quick response. I got it to work with this:

Code: Select all

  	oButton=ThisComponent.Sheets(0).Drawpage.Forms(0).getByName("RateA")
	  oButton.state = 1
:D
Apache OpenOffice 4.1.5
Windows 10 Home
Post Reply