[Dropped] Change cell with event listener without crashing

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
idris.cheikh
Posts: 8
Joined: Mon Apr 11, 2022 2:53 pm

[Dropped] Change cell with event listener without crashing

Post by idris.cheikh »

I am trying to create two tables which mirror changes made to any of them to one another automatically.

To that end, I added event listeners which are triggered when the cells of these tables are edited by the user.

Unfortunately, editing one of the tables causes LibreOffice to crash, even though the changes are indeed reflected correctly, as seen upon reopening the file.
I thought the crash might be due to a never-ending circular reference, but it still crashes after it has been made non-circular (by commenting out the relevant parts of the code so that changes are reflected only one way rather than both ways).
I noticed the code worked fine when writing to a cell that didn't have an event listener set to it.

How can I write to one of the cells with event listeners set to them without causing LibreOffice to crash?

You may want to download the following file. Please run Main and then try editing the cell C3 of the Planning sheet. The arbitrary string "C" should be written in the cell C4 of the Services sheet.

Here is a simplified version of the code :

Code: Select all

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

const SERVICESSHEET_NUMBER = 2
const SERVICESSHEET_SERVICES_COLUMN = 2

Type cellStruct
	columnNumber As Integer
	rowNumber As Integer
End Type



Sub UpdateServicesSheet(editedCell As cellStruct, newValue As String)
	
	Dim oSheets
	Dim servicesSheet
		
	oSheets = ThisComponent.getSheets()
	servicesSheet = oSheets.getByIndex(SERVICESSHEET_NUMBER)

	servicesSheet.getCellByPosition(SERVICESSHEET_SERVICES_COLUMN, 3).setString(newValue)
	
End Sub


Private oListener, cellRange as Object

Sub AddListener
	Dim sheet, cell as Object

	sheet = ThisComponent.Sheets.getByIndex(0)  'get leftmost sheet
	servicesSheet = ThisComponent.Sheets.getByIndex(2)

	cellRange = sheet.getCellrangeByName("C3")
	oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener")  'create a listener
	cellRange.addModifyListener(oListener)  'register the listener


	cellRange = servicesSheet.getCellrangeByName("C4")
	oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener")  'create a listener
	cellRange.addModifyListener(oListener)  'register the listener
End Sub

global CircularReferenceAllowed As boolean

Sub Modify_modified(oEv)

	Dim editedCell As cellStruct
	Dim newValue As String
	
	editedCell.columnNumber = 2
	editedCell.rowNumber = 2
	

	If CircularReferenceAllowed Then
		CircularReferenceAllowed = false
		UpdateServicesSheet(editedCell, "C")
	End If

End Sub

Sub Modify_disposing(oEv)
End Sub

Sub RmvListener
	cellRange.removeModifyListener(oListener)
End Sub


Sub Main

	CircularReferenceAllowed = true
	AddListener

End Sub

Crossposted to :

- StackOverflow
- LibreOffice discourse platform
Attachments
live_mirrored_tables.ods
(24.49 KiB) Downloaded 110 times
Last edited by MrProgrammer on Fri May 27, 2022 8:59 pm, edited 1 time in total.
Reason: Dropped: No attachment provided when requested -- MrProgrammer, forum moderator
LibreOffice 7.0.6.2 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11358
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Change value of cell with event listener without crashin

Post by Zizi64 »

Why you pass a Cellstructure variable with the row/column number,

Code: Select all

Sub UpdateServicesSheet(editedCell As cellStruct, newValue As String)
when you use constants in the called .getCellByPosition:

Code: Select all

servicesSheet.getCellByPosition(SERVICESSHEET_SERVICES_COLUMN, 3).setString(newValue)
Can you upload an ODF type sample file with the embedded macro code 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.
Post Reply