Type of user defined property

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Type of user defined property

Post by Villeroy »

The following Basic snippet adds a new user defined property of type "String":

Code: Select all

udp = ThisComponent.DocumentProperties.getUserDefinedProperties()
udp.addProperty("FOO", 128, "23")
128 is the flag for a removable property.
I get a wrong type error when using 23 instead of "23". How can I add a new property value that is not a string? The GUI (menu:File>Properties>tab:Custom Properties) offers several types.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Type of user defined property

Post by B Marcelly »

Since the third argument in method addProperty is declared as of type any, the binding of the Basic value is ambiguous for simple types. You have to specify a Uno type.

Code: Select all

.addProperty("FOO", com.sun.star.beans.PropertyAttribute.REMOVEABLE, CreateUnoValue("double", 23))
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Type of user defined property

Post by Villeroy »

Thank you very much. I forgot this issue over the years. This is required when you need to declare the type of a new UNO property. In Basic we have helper function CreateUnoValue, for other languages the proceedings should be documented in the documentation of the respective language bridge.

Python: http://www.openoffice.org/udk/python/python-bridge.html

Code: Select all

import uno
from com.sun.star.PropertyAttribute import REMOVEABLE

'''Add a new property "MyProperty" of type double with default 0.0'''

if not obj.PropertySetInfo.hasPropertyByName("MyProperty"):
    defaultVal = uno.Any("double", 0.0)
    uno.invoke(obj, "addProperty", ("MyProperty", REMOVEABLE, defaultVal))

# once we have declared the property with its type and default value, everything is easy:
obj.MyProperty = 3.14159
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply