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

Creating a macro - Writing a Script - Using the API

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

Postby alb21 » Tue Mar 12, 2019 12:32 pm

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
alb21
 
Posts: 6
Joined: Tue Apr 10, 2018 8:06 am

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

Postby JeJe » Tue Mar 12, 2019 1:58 pm

.state = 1 or .state = 0
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 574
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby Zizi64 » Tue Mar 12, 2019 3:34 pm

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; LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.5; AOO4.1.6
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.
User avatar
Zizi64
Volunteer
 
Posts: 8224
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

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

Postby alb21 » Tue Mar 12, 2019 8:41 pm

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
alb21
 
Posts: 6
Joined: Tue Apr 10, 2018 8:06 am

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

Postby Zizi64 » Tue Mar 12, 2019 9:11 pm

Please, upload your macro code and an ODF type sample file here.
Tibor Kovacs, Hungary; LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.5; AOO4.1.6
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.
User avatar
Zizi64
Volunteer
 
Posts: 8224
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

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

Postby mikele » Tue Mar 12, 2019 9:38 pm

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   Expand viewCollapse view
oButton=ThisComponent.Sheets(0).Drawpage.Forms(0).getByName("radio1")
LibreOffice 5.4.7 on LinuxMint/WinXP/Win7
mikele
 
Posts: 45
Joined: Wed Nov 21, 2018 11:11 am
Location: Germany

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

Postby Zizi64 » Tue Mar 12, 2019 10:17 pm

Your macro works for me in my AOO portable 4.1.5, and LibreOffice 6.1.5.

Radio.ods
(11.98 KiB) Downloaded 47 times


Please upload your sample file here.
Tibor Kovacs, Hungary; LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.5; AOO4.1.6
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.
User avatar
Zizi64
Volunteer
 
Posts: 8224
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

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

Postby JeJe » Tue Mar 12, 2019 11:46 pm

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

Code: Select all   Expand viewCollapse view

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.
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 574
Joined: Wed Mar 09, 2016 2:40 pm

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

Postby RPG » Wed Mar 13, 2019 12:44 am

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   Expand viewCollapse view
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 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2170
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

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

Postby alb21 » Wed Mar 13, 2019 5:55 am

Thank you very much to everybody for your quick response. I got it to work with this:
Code: Select all   Expand viewCollapse view
     oButton=ThisComponent.Sheets(0).Drawpage.Forms(0).getByName("RateA")
     oButton.state = 1

:D
Apache OpenOffice 4.1.5
Windows 10 Home
alb21
 
Posts: 6
Joined: Tue Apr 10, 2018 8:06 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 7 guests