[Solved] Use of DCOUNTA - Function in OpenOffice Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
denvee
Posts: 2
Joined: Wed May 22, 2013 10:00 am

[Solved] Use of DCOUNTA - Function in OpenOffice Basic

Post by denvee »

Good morning everyone,

my question refers to the correct call of the DCOUNTA() - function in Basic using
OpenOffice calc.

I've been trying to call the DCOUNTA - function as follows:

Code: Select all

Function dCounta( database As Object, field As Object, criteria As Object )
 Dim Funktion as Object
 Funktion = createUnoService("com.sun.star.sheet.FunctionAccess")

Dim Argumente(2) As Variant
 Argumente(0) = database
 Argumente(1) = field
 Argumente(2) = criteria

 
dCounta = Funktion.callFunction( "DCOUNT", Argumente() )

End Function
By calling the function with 3 field - references as parameters (for example: database = currentWorksheet.getCellRangeByName("D18:D400"), field = currentWorksheet.getCellRangeByName("D17")
field = currentWorksheet.getCellRangeByName("A1:B2"))
I'm only getting an IllegalArgumentException without any commentary.

Could anyone please give an example on how to call the function correctly?

Thanks and regards
Den
Last edited by Hagar Delest on Wed May 22, 2013 4:06 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.1
FJCC
Moderator
Posts: 9555
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Use of DCOUNTA - Function in OpenOffice Basic

Post by FJCC »

This works for me

Code: Select all

Sub Main
Sheet = ThisComponent.Sheets.getByIndex(0)
DB = Sheet.getCellrangeByName("A1:E10")
F = "Name"
C = Sheet.getCellrangeByName("A13:E14")
Print My_dCounta(DB,F,C)
End sub

Function My_dCounta( database As Object, field As String, criteria As Object )
Dim Funktion as Object
Funktion = createUnoService("com.sun.star.sheet.FunctionAccess")

Dim Argumente(2) As Variant
Argumente(0) = database
Argumente(1) = field
Argumente(2) = criteria


My_dCounta = Funktion.callFunction( "DCOUNTA", Argumente() )

End Function
Notice that I pass the field name as a string. I also changed the name of the function to be distinct from the built in DCOUNTA function. Finally, your callFunction() specified DCOUNT but I assumed you wanted DCOUNTA.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
denvee
Posts: 2
Joined: Wed May 22, 2013 10:00 am

Re: Use of DCOUNTA - Function in OpenOffice Basic

Post by denvee »

Thank you, that's it.

I couldn't find a proper example for this anywhere, so hope it helps other people to be prevented
from the despair I was going through ;).

Regards
Den
OpenOffice 3.1
Post Reply