Listener - how to check which one launch event

Hi everyone.

As you know, Sheet has Sheet_Events. The list of them is:

  • Activate Document
  • Deactivate Document
  • Selection changed
  • Double click
  • Right click
  • Formulas calculated
  • Content change

Let’s assume that for everyone of those events has been assingned the same macro, for example

Sub sSheetEvent(event As Object)

’ here check listener type has been used to launch
End Sub

How to check inside subroutine, which one listener type launched an event?

Greetings,
nexpron

Install one of the excellent object inspection tools (XrayTool, MRI), then you will able to examine the event inside your routine, and you will able to determite the properties of it.

I just tried it:

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

Sub MySub(optional oEvent as object)

	If ismissing(oEvent) then
		Print "There is not Event object"
	else
		xray oEvent
	end if
End Sub

The “Formulas calculated” event will not pass any oEvent object. In this case the the Print command will be activated.
Unfortunately the most of the other Events will pass the actual Cell or Range object.

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

    Sub MySub(optional oEvent as object)
    
    	If ismissing(oEvent) then
    		Print "There is not Event object"
    	else
    		xray oEvent
    	end if
    End Sub

I just tried it:

The “Formulas calculated” event will not pass any oEvent object. In this case the the Print command will be activated.
Unfortunately the most of the other Events will pass the Cell object.

LO 7.1 version, MRI 1.3.3 installed. But imagine you, MRI Extension not working. It’s not starting, remove and install again failed. And it’s external tool, not working inside routing code. For inspection it will be good tool, but I need to know event type on procedure running.

I installed Xray tool and launch your code (sheet event - double click).

oEvent is ScCellObj.
Initial object is com.sun.star.uno.XInterface , but base interface of all UNO interfaces.
When I clicked The parent object, nothing happened. Would you tell me, where in XRay I’ll find, which listener launched this event? I need an interface name.

The "Selection changed Event causes some error (use an error handler):

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

	Sub MySub(optional oEvent as object)
		
		On Error GoTo Errorhandler
		If ismissing(oEvent) then
			Print "There is not Event object"
		else
			xray oEvent
		end if
		Exit sub
	Errorhandler:
		Print "There was some error."
	End Sub

I launched procedure by double click event, but Xray showed me the same result, as I descripted above.
Link to Xray view in below link

https://ibb.co/NLD9gr0

I experienced three types of behaviors:

  • No passed event object: for the “Recalculated” event (by Shift-Ctrl-F9)
  • Passed a Cell object (or a Cell Range): for more than one other Event. I can not see how to determine the actual one.
  • Error raised: for the “Selection changed” event.

I little has checked a behaviour of those events.

Activate Document

This event applies to sheets, no documents. If we change document selection, nothing happens.
Usual sheet → sheet with event
Sheet with event tab is selected → sheet with event is activated; show its content → event is launched.
Count of event launched: 1
Event procedure takes no arguments.

Sub SheetEvent_ActivateDocument()
' code here

End Sub

If we define event procedure with parameter/s

Sub SheetEvent_ActivateDocument(oEvent As Object)

LO throw below error
A Scripting Framework error occured while running the Basic script Standard.Module1.SheetEvent_ActivateDocument.
Message: wrong number of parameters!

Deactivate Document

This event applies to sheets, no documents. If we change document selection, nothing happens.
Sheet with event → usual sheet
Usual sheet tab is selected → usual sheet is activated; show its content → event is launched.
Count of event launched: 1
Event procedure takes no arguments.

Sub SheetEvent_DeactivateDocument()
	' code here
End Sub

If we define event procedure with parameter/s

Sub SheetEvent_DectivateDocument(oEvent As Object)

LO throw below error
A Scripting Framework error occured while running the Basic script Standard.Module1.SheetEvent_DeactivateDocument.
Message: wrong number of parameters!

Formulas calculated

Event launched when formulas recalculated. Or user click SHIFT + CTRL + F9
Count of event launched: 1
Event procedure takes no arguments.

Sub SheetEvent_FormulasCalculated()
	' code here
End Sub

f.e.

Global l As Long

Sub SheetEvent_FormulasCalculated()
	l = l + 1
End Sub

Sub FormulasCalculated_SetStartValue()
	l = 1
End Sub

Sub FormulasCalculated_GetFinishValue()
	MsgBox l
End Sub

If we define event procedure with parameter/s

Sub SheetEvent_FormulasCalculated(oEvent As Object)

LO throw below error
A Scripting Framework error occured while running the Basic script Standard.Module1.SheetEvent_FormulasCalculated.
Message: wrong number of parameters!

Double click

Parameter oEvent is not required
Count of event launched: 1
Parameter oEvent is not required

Sub SheetEvent_DoubleClick()
Sub SheetEvent_DoubleClick(Event As Object) ' ScCellObj

Right click

Parameter oEvent is not required
First event is launched, then show context menu
Count of event launched: 1
Parameter oEvent is not required
If range is selected, f.e. D4:G12 and inside of this range right mouse button is clicked, ScCellObj is the cell which was clicked, f.e. “$Sheet1.$G$9”

Sub SheetEvent_RightClick()
Sub SheetEvent_RightClick(oEvent As Object) ' ScCellObj

Content changed

Event is launched then content has changed
Count of event launched: 1
Parameter oEvent is not required

Sub SheetEvent_ContentChanged()
Sub SheetEvent_ContentChanged(oEvent As Object) ' ScCellObj
Sub SheetEvent_ContentChanged(oEvent As Object) ' ScCellRangeObj

f.e.

Global l as Long
Sub SheetEvent_ContentChanged(oEvent As Object)
	l = l + 1
End Sub

Sub ContentChanged_SetStartValue()
	l = 0
End Sub

Sub ContentChanged_GetFinishValue()
	MsgBox l
End Sub

Selection changed
Most complex.
This event applies to:

  • change sheet selection
  • change range selection
  • change cell selection

This event not applies to document selection.

Sheet with event → usual sheet
Usual sheet tab is selected → Calc still show sheet with event content → event is launched → usual sheet is activated; show its content
Procedure declaration (as you see, parameter is not required)

Sub SheetEvent_SelectionChanged()
Sub SheetEvent_SelectionChanged(oEvent As Object) ' ScCellObj object, AbsoluteName f.e. "$Sheet1.$C$31" - if single cell in sheet with event was selected
Sub SheetEvent_SelectionChanged(oEvent As Object) ' ScCellRangeObj object, AbsoluteName f.e. "$Sheet1.$C$31

Usual sheet → sheet (which this event is linked) → event is launched → sheet is activated; show its content
Procedure declaration (as you see, parameter is not required)

Sub SheetEvent_SelectionChanged()
Sub SheetEvent_SelectionChanged(oEvent As Object) ’ ScCellObj object located in sheet with event; if only single cell selected → AbsoluteName f.e. “$Sheet1.$C$31”; if range selected - return first left top single cell of this range, f.e. D4:G12 → AbsoluteName f.e. “$Sheet1.$D$4”

Range selection
If selection has changed by SHIFT + arrow - example: two additional cells selected (three cells selected) - four events are launched one by one!
If selection has changed by mouse, four event are launched one by one!

Sub SheetEvent_SelectionChanged()
Sub SheetEvent_SelectionChanged(oEvent As Object) ' ScCellRangeObj

Cell selection
If selection has changed by arrow, one event is launched.
If selection has changed by mouse, four events are launched one by one!

Sub SheetEvent_SelectionChanged()
Sub SheetEvent_SelectionChanged(oEvent As Object) ' ScCellObj

Furhtermore

If LibreOffice Basic is open and breakpoint is set inside procedure
If cell has changed by arrow - procedure is launched once
If cell has changed by mouse - procedure is launched twice, but code inside procedure is executed once

Let’s assume.
For below event list it’s impossible to check which one listener type has been used, because no object / variable is passed as argument.

  • Activate Document
  • Deactivate Document
  • Formulas calculated

Does someone know, which listeners are used for these events? Especially for Selection changed, because user don’t know which event launched it.
LO has really problem with event-driven style.

Let’s assume. For below event list it’s impossible to check which one listener type has been used, because no object / variable is passed as argument.

  • Activate Document
  • Deactivate Document
  • Formulas calculated

Thank you for help.
nexpron