[Solved] Radio buttons on Calc spreadsheet

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
rogerl123
Posts: 13
Joined: Mon Nov 17, 2008 2:07 pm

[Solved] Radio buttons on Calc spreadsheet

Post by rogerl123 »

I have a small spreadsheet with a button and three radio buttons and one ordinary button hat I click to run a macro, PushButton.

The radio buttons all have the same name, Options, so that they are grouped together and hence mutually exclusive. They are labeled Option 1, Option 2, and Option 3 respectively.

I would like a small macro in OOBasic, so that after I have selected an option, I click on the button a message box is displayed stating which option has been chosen.

I have written some code, based on the documentation at http://wiki.services.openoffice.org/wik ... ment_Forms which gives an example in OOBasic, and on forum responses to similar requests.

However, when I click on the button I get the error message

An exception has occurred
Type.com.Sun.lang.IlleagalArgumentException
Message: arguments len differ


The code that I have developed is as follows


Global oDocument as Object
Global oDrawPage as Object
Global oForm as Object
Global oModelOpt as Object
Global oGroup as Object
Global res as Object

Sub Main

End Sub

Sub PushButton

dim msg as string

msg = "you have selectd option: "

oDocument = ThisComponent
oDrawPage = oDocument.DrawPages.getByIndex(0) ' for Sheet No 1 (every sheet has a drawpage)
oForm = oDrawPage.getForms.getByIndex(0) ' for the first form

if oForm.HasByName("Options") then
oModelOpt = oForm. GetGroupbyName("Options")
end if

msg = msg + res

msgbox msg

End sub


where PushButton is invoked by clicking on a button.

The line oModelOpt = oForm. GetGroupbyName("Options")

is the line that is highlighted as the error.

I would be grateful if anyone can suggest how I can eliminate the error message and return a result indicating which option has been selected.

Thanks for the help
Last edited by rogerl123 on Sun Nov 30, 2008 3:58 pm, edited 2 times in total.
OOo 3.0.X on Ms Windows XP
turtle47
Posts: 31
Joined: Tue Sep 16, 2008 3:54 pm

Re: radio buttons on calc spreadsheet

Post by turtle47 »

Hi Rogerl123,

here are two examlples:

Code: Select all

Sub Example_Opt_Button_1
oDocument = ThisComponent
oDrawPage = oDocument.DrawPages.getByIndex(0) ' for Sheet No 1 (every sheet has a drawpage)
oForm = oDrawPage.getForms.getByIndex(0) ' for the first form
	oForm.getGroupByName("RadioGroup1", oGroup)
	for i = 0 to UBound(oGroup)
       oCtl = oGroup(i)
       if oCtl.State = 1 Then
       msgbox  oCtl.Label
       end if
   next i
End Sub


Sub Example_Opt_Button_2
oDocument = ThisComponent
oDrawPage = oDocument.DrawPages.getByIndex(0) ' for Sheet No 1 (every sheet has a drawpage)
oForm = oDrawPage.getForms.getByIndex(0) ' for the first form
	oForm.getGroupByName("RadioGroup1", oGroup)
	msg=""
	for i = 0 to UBound(oGroup)
       oCtl = oGroup(i)
       msg = msg + oCtl.Label + " " + (str(oCtl.State)) + chr(10)
   next i
   MsgBox msg 
End Sub
Good luck.

JD
OOo 3.2.X on Ms Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: radio buttons on calc spreadsheet

Post by Villeroy »

Bind Option1 to (hidden) cell X1
Bind Option2 to (hidden) cell X3
Bind Option3 to (hidden) cell X3

Y1: =MATCH(TRUE();$X$1:$X$3;0)
Z1: ="Option #"&$Y$1&" is set"

Code: Select all

Msgbox oSheet.getCellRangeByName("Z1").getString()
You could get the form of the calling button like this:

Code: Select all

Sub push_button(oEv)
oCaller = oEv.Source
oForm = oEv.getContext()

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