User Defined Calc Function That Accepts a Range (=myFun(A1:A5))?

I’ve tried googling this, but unfortunately, the top search result was never answered, got marked as a duplicate, and was locked.

If I want a user to call this function in Calc:
=myFun(A1:A5)

What gets sent in to the Basic function?

Function myFun(range as variant)
  'what is the data type of range???

End Function  

Everything I try returns an error:

Function myFun(range as variant)
	dim qry as variant 
	dim cells as variant
	dim ce as variant
	dim cell as variant
		
	qry = range.queryContentCells(com.sun.star.sheet.CellFlags.VALUE)
	cells = qry.getCells()
	ce = cells.createEnumeration()
	
	Dim retVal as single

	Do while ce.hasMoreElements
		cell = ce.NextElement
		retval = retval + myConverter(cell.Value)
	
	Loop
	
	myFun = retVal
End Function

When you call an user defined function like the
=myFun(A1:A5)
the the Libreoffice will pass a two dimensional DataArray to your function, but not the Cell (Range) object.
Only the data and the data type, and the size of the array will be passed, but not the other informations about the passed cells/range (not will be passed the position, not the cell objects, not the formatting properties…)

You can get the size of the Array (for the programming loops what will process the data)

Just install one of the excellent Object inspection tools: XrayTool or MRI. Then you will able to list the existing properties and methods of the programming object like the passed DataArray.

1 Like

Welcome!
There is no single answer to your question, there are two options
Just write a test function, set a breakpoint, and examine the value.
If this is a function of a regular, standard, traditional basic, then inside the range function it is a two-dimensional array with values from the specified range.

image

And if you enabled VBA compatibility with Option VbaSupport 1, then range is a range of cells.
image

2 Likes