如何通过UNO存储每个打开文档的瞬态数据?

时间:2018-05-23 08:22:43

标签: java libreoffice 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);

1 个答案:

答案 0 :(得分:0)

在下面的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,

当文档关闭然后再次运行代码时,会出现完全相同的结果,证明该属性未存储。