[英]How to store transient data per open document via UNO?
I have some configuration values (integers and strings) passed to a launcher program and later needed and changed in an extension. 我有一些配置值(整数和字符串)传递给启动程序,以后需要在扩展中进行更改。 They can differ between documents but must not be saved along with them.
它们在文档之间可以不同,但不能与文档一起保存。
Can I store these directly in the LibreOffice process? 我可以直接将这些存储在LibreOffice流程中吗?
I have or can get a reference to the document as XStorable
in either case. 无论哪种情况,我都有或可以以
XStorable
引用该文档。
So far, I tried to use XPropertyContainer.addProperty(...)
on the document, but the values I pass in seem to be stored globally istead of per document: 到目前为止,我尝试在文档上使用
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);
In the following python code, the values are different for each document, and they are not stored. 在以下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 + ",")
Results: 结果:
logfile1: Unknown Property,a,a,
logfile2: Unknown Property,b,b,
When the documents are closed and then the code is run again, the exact same results occur, proving that the property is not stored. 当关闭文档,然后再次运行代码时,会出现完全相同的结果,证明该属性没有存储。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.