如何通过 UNO 为每个打开的文档存储瞬态数据?
How to store transient data per open document via UNO?
我有一些配置值(整数和字符串)传递给启动程序,后来需要在扩展中更改。它们可以因文档而异,但不能与它们一起保存。
我可以将它们直接存储在 LibreOffice 进程中吗?
在任何一种情况下,我都有或可以获得对文档的引用 XStorable
。
到目前为止,我尝试在文档上使用 XPropertyContainer.addProperty(...)
,但我传入的值似乎是全局存储的,而不是按文档存储的:
XDocumentPropertiesSupplier xDocumentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, document);
XDocumentProperties xDocumentProperties = xDocumentPropertiesSupplier.getDocumentProperties();
XPropertyContainer xPropertyContainer = xDocumentProperties.getUserDefinedProperties();
xPropertyContainer.addProperty(propertyName, PropertyAttribute.TRANSIENT | PropertyAttribute.MAYBEDEFAULT, propertyValue);
在下面的python代码中,每个文档的值是不同的,并且没有被存储。
import uno
from com.sun.star.beans.PropertyAttribute import TRANSIENT
from com.sun.star.beans import UnknownPropertyException
def temp_prop():
DIR = "/path/to/dir"
LOG1 = DIR + "log1.txt"
LOG2 = DIR + "log2.txt"
PROPNAME = "myPropName"
oDesktop = XSCRIPTCONTEXT.getDesktop()
oDoc1 = oDesktop.loadComponentFromURL(
"file:///" + DIR + "1.odt", "_default", 0, ())
oDoc2 = oDesktop.loadComponentFromURL(
"file:///" + DIR + "2.odt", "_default", 0, ())
oDoc1props = oDoc1.getDocumentProperties().getUserDefinedProperties();
oDoc2props = oDoc2.getDocumentProperties().getUserDefinedProperties();
try:
appendToFile(LOG1, oDoc1props.getPropertyValue(PROPNAME))
appendToFile(LOG2, oDoc2props.getPropertyValue(PROPNAME))
except UnknownPropertyException:
appendToFile(LOG1, "Unknown Property")
appendToFile(LOG2, "Unknown Property")
oDoc1props.addProperty(PROPNAME, TRANSIENT, "a")
oDoc2props.addProperty(PROPNAME, TRANSIENT, "b")
appendToFile(LOG1, oDoc1props.getPropertyValue(PROPNAME))
appendToFile(LOG2, oDoc2props.getPropertyValue(PROPNAME))
oDoc1.store()
oDoc2.store()
oDoc1props = oDoc1.getDocumentProperties().getUserDefinedProperties();
oDoc2props = oDoc2.getDocumentProperties().getUserDefinedProperties();
appendToFile(LOG1, oDoc1props.getPropertyValue(PROPNAME))
appendToFile(LOG2, oDoc2props.getPropertyValue(PROPNAME))
def appendToFile(fname, s):
with open(fname, "a") as f:
f.write(s + ",")
结果:
logfile1: Unknown Property,a,a,
logfile2: Unknown Property,b,b,
当文档关闭然后代码再次为运行时,出现完全相同的结果,证明属性没有被存储。
我有一些配置值(整数和字符串)传递给启动程序,后来需要在扩展中更改。它们可以因文档而异,但不能与它们一起保存。
我可以将它们直接存储在 LibreOffice 进程中吗?
在任何一种情况下,我都有或可以获得对文档的引用 XStorable
。
到目前为止,我尝试在文档上使用 XPropertyContainer.addProperty(...)
,但我传入的值似乎是全局存储的,而不是按文档存储的:
XDocumentPropertiesSupplier xDocumentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, document);
XDocumentProperties xDocumentProperties = xDocumentPropertiesSupplier.getDocumentProperties();
XPropertyContainer xPropertyContainer = xDocumentProperties.getUserDefinedProperties();
xPropertyContainer.addProperty(propertyName, PropertyAttribute.TRANSIENT | PropertyAttribute.MAYBEDEFAULT, propertyValue);
在下面的python代码中,每个文档的值是不同的,并且没有被存储。
import uno
from com.sun.star.beans.PropertyAttribute import TRANSIENT
from com.sun.star.beans import UnknownPropertyException
def temp_prop():
DIR = "/path/to/dir"
LOG1 = DIR + "log1.txt"
LOG2 = DIR + "log2.txt"
PROPNAME = "myPropName"
oDesktop = XSCRIPTCONTEXT.getDesktop()
oDoc1 = oDesktop.loadComponentFromURL(
"file:///" + DIR + "1.odt", "_default", 0, ())
oDoc2 = oDesktop.loadComponentFromURL(
"file:///" + DIR + "2.odt", "_default", 0, ())
oDoc1props = oDoc1.getDocumentProperties().getUserDefinedProperties();
oDoc2props = oDoc2.getDocumentProperties().getUserDefinedProperties();
try:
appendToFile(LOG1, oDoc1props.getPropertyValue(PROPNAME))
appendToFile(LOG2, oDoc2props.getPropertyValue(PROPNAME))
except UnknownPropertyException:
appendToFile(LOG1, "Unknown Property")
appendToFile(LOG2, "Unknown Property")
oDoc1props.addProperty(PROPNAME, TRANSIENT, "a")
oDoc2props.addProperty(PROPNAME, TRANSIENT, "b")
appendToFile(LOG1, oDoc1props.getPropertyValue(PROPNAME))
appendToFile(LOG2, oDoc2props.getPropertyValue(PROPNAME))
oDoc1.store()
oDoc2.store()
oDoc1props = oDoc1.getDocumentProperties().getUserDefinedProperties();
oDoc2props = oDoc2.getDocumentProperties().getUserDefinedProperties();
appendToFile(LOG1, oDoc1props.getPropertyValue(PROPNAME))
appendToFile(LOG2, oDoc2props.getPropertyValue(PROPNAME))
def appendToFile(fname, s):
with open(fname, "a") as f:
f.write(s + ",")
结果:
logfile1: Unknown Property,a,a,
logfile2: Unknown Property,b,b,
当文档关闭然后代码再次为运行时,出现完全相同的结果,证明属性没有被存储。