Well, I made an example extension. However you probably wouldn’t want to actually do this. If you are looking for something like a dictionary to use in Basic, then there are many better ways. See libreoffice - simplest Unostructure that supports he getByName - Stack Overflow.
Anyway, here is the python code for my example extension. Of course, it would make more sense to use a python dictionary, but the question asked about a struct, so here it is.
import uno
import unohelper
from com.sun.star.container import XMap
class FlexibleStruct:
pass
class StructDemo(unohelper.Base, XMap):
def __init__(self, ctx):
self.ctx = ctx
self.values = FlexibleStruct()
self.KeyType = str
self.ValueType = str
def clear(self):
self.values = FlexibleStruct()
def get(self, Key):
return getattr(self.values, Key)
def put(self, Key, Value):
setattr(self.values, Key, Value)
def remove(self, Key):
delattr(self.values, Key)
def containsKey(self, Key):
return hasattr(self.values, Key)
def containsValue(self, Value):
return False
def testdemo(ctx=None):
if ctx is None:
ctx = XSCRIPTCONTEXT.getComponentContext()
demo = StructDemo(ctx)
demo.put("a", 3)
display_quick_message(demo.get("a"))
def display_quick_message(message):
raise Exception("[message={}]".format(message))
g_exportedScripts = (
testdemo,
)
IMPL_NAME = "name.JimK.StructDemo"
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
StructDemo, IMPL_NAME, (IMPL_NAME,),)
And here is the Basic code that calls it.
demo = CreateUnoService("name.JimK.StructDemo")
demo.put("animal1", "hamster")
demo.put("animal2", "guinea pig")
MsgBox(demo.get("animal2"))
There are also some XML files which may be needed, such as manifest.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">
<manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">
<!-- Files that describe the extension -->
<manifest:file-entry
manifest:full-path="Addons.xcu"
manifest:media-type="application/vnd.sun.star.configuration-data"/>
<manifest:file-entry
manifest:full-path="pkg-desc/pkg-description.txt"
manifest:media-type="application/vnd.sun.star.package-bundle-description"/>
<manifest:file-entry
manifest:full-path="StructDemo.py"
manifest:media-type="application/vnd.sun.star.uno-component;type=Python"/>
</manifest:manifest>
Also description.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<description xmlns="http://openoffice.org/extensions/description/2006"
xmlns:xlink="http://www.w3.org/1999/xlink">
<version value="1.0" />
<identifier value="name.JimK.StructDemo"/>
<dependencies>
<OpenOffice.org-minimal-version value="3.0" d:name="OpenOffice.org 3.0"/>
</dependencies>
<display-name>
<name lang="en">Example Struct</name>
</display-name>
</description>