Ask Your Question
0

Changing cell data on button click.

asked 2016-01-23 17:30:00 +0100

Chris UK gravatar image

Im using two buttons and assigning a recorded macro to them, the only way i could get this to work was by assigning the same macro to both mouse down and mouse up events otherwise a double click was required.

I created two macros, ToggleOn and ToggleOff both are essentially the same code with just one edit, here is the code:

REM  *****  BASIC  *****

sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SelectObject", "", 0, Array())

rem ----------------------------------------------------------------------
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "ToPoint"
args2(0).Value = "$C$4"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args2())

rem ----------------------------------------------------------------------
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "StringName"
args3(0).Value = "n"

dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args3())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())

end sub

The change i referred to is just a "y" or "n".

args3(0).Value = "n"

Can anybody tell me is there a better way to do this, Im not fussy about the form control, button check box or otherwise, i would just prefer to have only one form control that toggles the cell data if possible.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2016-01-25 16:07:41 +0100

Lupp gravatar image

updated 2016-01-25 16:12:26 +0100

Recorded "macros" are rarely of much use.
If I understood correctly you want to switch from "y" to "n" and back the content of a specific cell when clicking on an OK-button. To do this for cell C4 of the current sheet you may use the following code:

option explicit

sub toggle_yn_Xis2_Yis3()   
' Internally X (left to right) and Y (top down) are starting with (X=0,Y=0) for A1.
    dim theDoc as Object, theSheet as Object, theCellC4 as Object
theDoc = ThisComponent
theSheet = theDoc.Sheets(theDoc.GetCurrentSelection.RangeAddress.Sheet)
theCellC4 = theSheet.GetCellByPosition(2, 3)
select case theCellC4.String
    case "y": theCellC4.String = "n"
    case "n": theCellC4.String = "y"
end select
end sub ..

If C4 is containing something else but "y" or "n", no action will be taken.
See attached: ask63443ToggleSpecificCellBySub001.ods
The included sub is assigned to the event 'MouseButtonReleased' for the button showing "y/n"

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-01-23 17:30:00 +0100

Seen: 870 times

Last updated: Jan 25 '16