[Solved] Properly linking with object definitions

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
UbuntuGeezer
Posts: 5
Joined: Mon May 11, 2020 4:12 am

[Solved] Properly linking with object definitions

Post by UbuntuGeezer »

I'm brand new to using the SDK objects from OOoBasic within Calc. I am trying to instantiate a Sheet object within a sub so that I can reuse the same object when accessing different sheets. The following statement:
oCat1Sheet = new com.sun.star.sheet.Spreadsheet
throws the error "Object not accessible. Invalid object reference."
What do I need to do to ensure that the runtime connects to the SDK objects? I can't find anything in the documentation about how to make this simple connection. :crazy:
Last edited by MrProgrammer on Sat May 23, 2020 4:59 pm, edited 2 times in total.
Reason: Remove Known Issues icon; Tagged ✓ [Solved]
Coderman
OpenOffice 4.1.5 Windows 10
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Properly linking with object definitions

Post by RoryOF »

Look at the works of Andrew Pitonyak; in his books he gives excellent code for most common (and often uncommon) requirements.

http://www.pitonyak.org/oo.php and http://www.pitonyak.org/database/

These will probably tell you as much as you need to know about OO macros and databases - do not miss his works on databases at the second URL if you are using a database.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Properly linking with object definitions

Post by JeJe »

Code: Select all

mysheet = thiscomponent.sheets.getbyname("Sheet1")
msgbox mysheet.name
mysheet = thiscomponent.sheets.getbyname("Sheet2")
msgbox mysheet.name
mysheet = thiscomponent.sheets.getbyindex(0)
msgbox mysheet.name
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
UbuntuGeezer
Posts: 5
Joined: Mon May 11, 2020 4:12 am

Re: Properly linking with object definitions

Post by UbuntuGeezer »

UbuntuGeezer wrote:focusing on this part of my query... The following statement:
oCat1Sheet = new com.sun.star.sheet.Spreadsheet
throws the error "Object not accessible. Invalid object reference."
...
What do I need to do to ensure that the runtime connects to the SDK objects? I can't find anything in the documentation about how to make this simple connection. :crazy:
Thank you for the responses. I have used the same form as the example in response 2, just re-instantiating the object with the assignment statement into a var of type Object.
But I am curious as to why my proposed alternative..
Dim oCat1Sheet as Object
.
oCat1Sheet = new.com.sun.star.sheet.Spreadsheet
is throwing a runtime error.
I may be under the erroneous assumption that every time we use the oCat1Sheet = oSheets.getSheetByIndex()
method, any existing oCat1Sheet instance will be discarded, and a new instance created. It seems to me that it could be a bit more streamlined in the code to allocate oCat1Sheet as static (e.g. Static oCat1Sheet = new com.sun.etc), since I am using it in a sub that may have repeated calls to it and I would like the object to persist.
The piece I appear to be missing is WHERE OOoBasic obtains the .com.sun.star.sheet etc. definitions. My guess is that my Windows10 system is missing either an environment variable or PATH that the ooO Basic runtime is using to locate these definitions within the SDK. I checked the documents referenced with reply 1 and did not see anything that jumped out at me...
??? :knock:
Coderman
OpenOffice 4.1.5 Windows 10
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Properly linking with object definitions

Post by JeJe »

But I am curious as to why my proposed alternative..
Dim oCat1Sheet as Object
.
oCat1Sheet = new.com.sun.star.sheet.Spreadsheet
is throwing a runtime error.
We had a similar question recently with someone wanting to create a paragraph on its own. My guess is a paragraph or a sheet don't make much sense except as part of a document.
I am using it in a sub that may have repeated calls to it and I would like the object to persist.
Declare a variable at the top of the module. You can declare it private, public or global

https://wiki.openoffice.org/wiki/Docume ... _Variables

Code: Select all

REM  *****  BASIC  *****
dim mysheet 

Sub anysub
mysheet = thiscomponent.sheets.getbyname("Sheet1")
End Sub
The piece I appear to be missing is WHERE OOoBasic obtains the .com.sun.star.sheet etc. definitions. My guess is that my Windows10 system is missing either an environment variable or PATH that the ooO Basic runtime is using to locate these definitions within the SDK. I checked the documents referenced with reply 1 and did not see anything that jumped out at me
The Api:

https://www.openoffice.org/api/docs/com ... le-ix.html

Edit: if you want autocomplete where OO suggests something after you type the dot - OO doesn't have that. LibreOffice had an 'experimental' version last time I looked.

You may find MRI extremely useful

https://extensions.openoffice.org/en/pr ... ction-tool
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
UbuntuGeezer
Posts: 5
Joined: Mon May 11, 2020 4:12 am

Re: Properly linking with object definitions

Post by UbuntuGeezer »

[SOLVED]As it turns out, the answer I was looking for is this:
I am using Windows10 with the SDK installed. The SDK for OOo is organized into the directory path
c:\Program Files (x86)\OpenOffice 4\sdk\idl\com\sun\star\<object-class>
where <object-class> is the subdirectory named after the object class definition (e.g. table). Within
that subdirectory are .idl files that provide the C++ definitions for the properties and methods within
the scope of the object class.
OOoBasic does not implement "new" services for all the object classes (and understandably so). When you
invoke a utility like XRay or MRI on an object from within OOoBasic, it exposes the object fields and methods.
Quite likely, a review of XRay or MRI source code would reveal that it is examining the .idl files
to produce a more readable list for display.
Part of the XRay/MRI expose may include field type references that go down the SDK rabbit hole. Those references
may be to objects that do not supply a "new" service for OOoBasic for idiots like myself that sometimes prefer
explicit declarations of objects. For those objects, the only accessibility from OOoBasic will be to declare
the object as type Object, and let the runtime routines instantiate it. For the curious, I have included a code sample
of how to investigate whether or not OOoBasic supplies the "new" service for objects the user wishes to manipulate
more directly. Note: If you're already ranting about "why in the world would you want to do this in Basic anyway? -
why not just use C++ or JAVA!!?", I can only agree. This is all pretty academic for most Basic programmers.

Code Sample: place this in a macro definition; to see the "not accessible" messages, just remove the ' from
the Dim lines that invoke "new"

'// template lines.
'Dim var as new com.sun.star.beans.
'XRay var

'Dim oAmb as new com.sun.star.beans.Ambiguous '// not accessible
'Dim oDef as new com.sun.star.beans.Defaulted '// not accessible

Dim ovar as new com.sun.star.beans.GetDirectPropertyTolerantResult
XRay ovar

Dim oProp as new com.sun.star.beans.Property
XRay oProp

dim oCellAdd as new com.sun.star.table.CellAddress
XRay oCellAdd

dim oRange as new com.sun.star.table.CellRangeAddress
XRay oRange

'dim Doc as new com.sun.star.frame.XModel '// not accessible
Coderman
OpenOffice 4.1.5 Windows 10
Post Reply