LibreOffice UNO Java API:如何打开文档、执行宏并关闭它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10848734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LibreOffice UNO Java API: how to open a document, execute a macro and close it?
提问by MarcoS
I'm working on LibreOffice server-side: on the server I run
我在 LibreOffice 服务器端工作:在我运行的服务器上
soffice --accept=...
Then I use Java LibreOffice client API's to apply a macro on a document (calc or writer). The java execution does not give any error, but I do not get the job done (macro code is executed, but it's effects are not in the output file). More, after macro script is invoked, the Basic debugger window appears, apparently stopped on the first line of my macro; F5 does not restart it...
然后我使用 Java LibreOffice 客户端 API 在文档(calc 或 writer)上应用宏。java 执行没有给出任何错误,但我没有完成工作(执行了宏代码,但它的效果不在输出文件中)。更多的是,在调用宏脚本后,Basic debugger 窗口出现,显然停在我的宏的第一行;F5不重启它...
This is the relevant code I'm using:
这是我正在使用的相关代码:
try {
XComponentContext xLocalContext = Bootstrap.createInitialComponentContext(null);
System.out.println("xLocalContext");
XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();
System.out.println("xLocalServiceManager");
Object urlResolver = xLocalServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xLocalContext);
System.out.println("urlResolver");
XUnoUrlResolver xUrlResolver =
(XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, urlResolver);
System.out.println("xUrlResolve");
try {
String uno = "uno:" + unoMode + ",host=" + unoHost + ",port=" + unoPort + ";" + unoProtocol + ";" + unoObjectName;
Object rInitialObject = xUrlResolver.resolve(uno);
System.out.println("rInitialObject");
if (null != rInitialObject) {
XMultiComponentFactory xOfficeFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(
XMultiComponentFactory.class, rInitialObject);
System.out.println("xOfficeFactory");
Object desktop = xOfficeFactory.createInstanceWithContext("com.sun.star.frame.Desktop", xLocalContext);
System.out.println("desktop");
XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
XComponentLoader.class, desktop);
System.out.println("xComponentLoader");
PropertyValue[] loadProps = new PropertyValue[3];
loadProps[0] = new PropertyValue();
loadProps[0].Name = "Hidden";
loadProps[0].Value = Boolean.TRUE;
loadProps[1] = new PropertyValue();
loadProps[1].Name = "ReadOnly";
loadProps[1].Value = Boolean.FALSE;
loadProps[2] = new PropertyValue();
loadProps[2].Name = "MacroExecutionMode";
loadProps[2].Value = new Short(com.sun.star.document.MacroExecMode.ALWAYS_EXECUTE_NO_WARN);
try {
XComponent xComponent = xComponentLoader.loadComponentFromURL("file:///" + inputFile, "_blank", 0, loadProps);
System.out.println("xComponent from " + inputFile);
String macroName = "Standard.Module1.MYMACRONAME?language=Basic&location=application";
Object[] aParams = null;
XScriptProviderSupplier xScriptPS = (XScriptProviderSupplier) UnoRuntime.queryInterface(XScriptProviderSupplier.class, xComponent);
XScriptProvider xScriptProvider = xScriptPS.getScriptProvider();
XScript xScript = xScriptProvider.getScript("vnd.sun.star.script:"+macroName);
short[][] aOutParamIndex = new short[1][1];
Object[][] aOutParam = new Object[1][1];
@SuppressWarnings("unused")
Object result = xScript.invoke(aParams, aOutParamIndex, aOutParam);
System.out.println("xScript invoke macro" + macroName);
XStorable xStore = (XStorable)UnoRuntime.queryInterface(XStorable.class, xComponent);
System.out.println("xStore");
if (outputFileType.equalsIgnoreCase("pdf")) {
System.out.println("writer_pdf_Export");
loadProps[0].Name = "FilterName";
loadProps[0].Value = "writer_pdf_Export";
}
xStore.storeToURL("file:///" + outputFile, loadProps);
System.out.println("storeToURL to file " + outputFile);
xComponent.dispose();
xComponentLoader = null;
rInitialObject = null;
System.out.println("done.");
System.exit(0);
} catch(IllegalArgumentException e) {
System.err.println("Error: Can't load component from url " + inputFile);
}
} else {
System.err.println("Error: Unknown initial object name at server side");
}
} catch(NoConnectException e) {
System.err.println("Error: Server Connection refused: check server is listening..."); }
} catch(java.lang.Exception e) {
System.err.println("Error: Java exception:");
e.printStackTrace();
}
采纳答案by MarcoS
After long hours of trial and error, I did just find the problem (whose cause keeps to be quite obscure, to me...). The component shouldn't be executed in "Hidden" mode, so, just with:
经过长时间的反复试验,我确实找到了问题(其原因一直很模糊,对我来说......)。该组件不应在“隐藏”模式下执行,因此,只需:
loadProps[1].Name = "Hidden";
loadProps[1].Value = Boolean.FALSE;
everything is ok. I suppose it's not an issue, since the jar file is executed server-side...
一切都好。我想这不是问题,因为 jar 文件是在服务器端执行的...
Hope it can help... :-)
希望它可以帮助... :-)