LibreOffice Java - 无法将 uno.Any 转换为 XPropertySet
LibreOffice Java - Cannot Cast uno.Any to XPropertySet
我正在尝试使用 Java 与 LibreOffice API 进行交互。目前,我正在为检索对象、插入文本等操作创建一个助手 class。但是,我在对电子表格文档应用验证时遇到了问题。我的函数如下:
public static void applyValidation(int x1, int y1, int x2, int y2, XSpreadsheet sheet, ValidationType type) throws UnknownPropertyException, WrappedTargetException, IndexOutOfBoundsException, IllegalArgumentException, PropertyVetoException {
XCellRange range = sheet.getCellRangeByPosition(x1, y1, x2, y2);
XPropertySet propSet = UnoRuntime.queryInterface(XPropertySet.class, range);
XPropertySet validProp =(XPropertySet) propSet.getPropertyValue("Validation");
validProp.setPropertyValue("ShowErrorMessage", new Boolean(true));
validProp.setPropertyValue("ErrorMessage", "Please enter a valid time");
validProp.setPropertyValue("ErrorAlertStyle", ValidationAlertStyle.INFO);
propSet.setPropertyValue("Validation", validProp);
}
它是根据 OpenOffice Developer's Guide 中提供的示例建模的。
我正在尝试从测试中调用该方法 class:
@Test
public void test() {
try {
XComponentLoader loader = LibreBootstrapper.getLoader();
assertTrue( (loader instanceof XComponentLoader));
XSpreadsheetDocument doc = SpreadsheetHelper.getSpreadsheetDoc(loader);
assertTrue( (doc instanceof XSpreadsheetDocument));
XSpreadsheet sheet = SpreadsheetHelper.getSheetByIndex(SpreadsheetHelper.getSheets(doc), 0);
SpreadsheetHelper.insertIntoCell(0, 0, sheet, 400);
SpreadsheetHelper.insertIntoCell(1, 0, sheet, 300);
SpreadsheetHelper.insertFormula(2, 0, sheet, "=SUM(A1:B1)");
SpreadsheetHelper.insertIntoCell(2, 2, sheet, "Hello World!");
SpreadsheetHelper.setProperty(0, 0, 8, 10, sheet, "CellStyle", "Heading 1");
int formatCode = SpreadsheetHelper.getFormatCode(doc, NumberFormat.TIME);
SpreadsheetHelper.setProperty(0, 0, 8, 10, sheet, "NumberFormat", formatCode);
SpreadsheetHelper.applyValidation(0, 0, 8, 10, sheet, ValidationType.TIME);
} catch (Exception e) {
e.printStackTrace();
}
}
但是,我收到以下错误:
java.lang.ClassCastException: com.sun.star.uno.Any cannot be cast to com.sun.star.beans.XPropertySet
其中有以下堆栈
at edu.cmu.office.SpreadsheetHelper.applyValidation(SpreadsheetHelper.java:125)
at HelperTests.test(HelperTests.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
我什至尝试从本质上复制和粘贴示例中的代码(链接到上面),但仍然收到相同的错误。
我对 LibreOffice 不太熟悉 API;任何帮助将不胜感激。
编辑:
实际抛出错误的行如下:
XPropertySet validProp =(XPropertySet) propSet.getPropertyValue("Validation");
即使直接从示例中复制粘贴,我也会收到同样的错误:
public static void applyValidation(int x1, int y1, int x2, int y2, XSpreadsheet sheet, ValidationType type) throws UnknownPropertyException, WrappedTargetException, IndexOutOfBoundsException, IllegalArgumentException, PropertyVetoException {
// XCellRange range = sheet.getCellRangeByPosition(x1, y1, x2, y2);
// XPropertySet propSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, range);
//
// XPropertySet validProp =(XPropertySet) propSet.getPropertyValue("Validation");
// validProp.setPropertyValue("ShowErrorMessage", new Boolean(true));
// validProp.setPropertyValue("ErrorMessage", "Please enter a valid time");
// validProp.setPropertyValue("ErrorAlertStyle", ValidationAlertStyle.INFO);
//
// propSet.setPropertyValue("Validation", validProp);
//
// --- Data validation ---
com.sun.star.table.XCellRange xCellRange = sheet.getCellRangeByName("A7:C7");
com.sun.star.beans.XPropertySet xCellPropSet = (com.sun.star.beans.XPropertySet)
UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xCellRange);
// validation properties
com.sun.star.beans.XPropertySet xValidPropSet = (com.sun.star.beans.XPropertySet)
xCellPropSet.getPropertyValue("Validation");
xValidPropSet.setPropertyValue("Type", com.sun.star.sheet.ValidationType.DECIMAL);
xValidPropSet.setPropertyValue("ShowErrorMessage", new Boolean(true));
xValidPropSet.setPropertyValue("ErrorMessage", "This is an invalid value!");
xValidPropSet.setPropertyValue("ErrorAlertStyle", com.sun.star.sheet.ValidationAlertStyle.STOP);
// condition
com.sun.star.sheet.XSheetCondition xCondition = (com.sun.star.sheet.XSheetCondition)
UnoRuntime.queryInterface(com.sun.star.sheet.XSheetCondition.class, xValidPropSet);
xCondition.setOperator(com.sun.star.sheet.ConditionOperator.BETWEEN);
xCondition.setFormula1("0.0");
xCondition.setFormula2("5.0");
// apply on cell range
xCellPropSet.setPropertyValue("Validation", xValidPropSet);
}
在以下行中:
com.sun.star.beans.XPropertySet xValidPropSet = (com.sun.star.beans.XPropertySet)
xCellPropSet.getPropertyValue("Validation");
你好像忘记正确投射了。代码应该像示例:
XPropertySet xCellPropSet = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class, xCellRange);
...而不是这个:
XPropertySet propSet = UnoRuntime.queryInterface(XPropertySet.class, range);
编辑:
显然,示例不正确。这是工作代码:
XPropertySet validProp = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class,
propSet.getPropertyValue("Validation"));
这些类型的问题在使用 Java 和 UNO 时很烦人。我更喜欢 Python 和 UNO,其中一个原因是因为 queryInterface
不是必需的。
我正在尝试使用 Java 与 LibreOffice API 进行交互。目前,我正在为检索对象、插入文本等操作创建一个助手 class。但是,我在对电子表格文档应用验证时遇到了问题。我的函数如下:
public static void applyValidation(int x1, int y1, int x2, int y2, XSpreadsheet sheet, ValidationType type) throws UnknownPropertyException, WrappedTargetException, IndexOutOfBoundsException, IllegalArgumentException, PropertyVetoException {
XCellRange range = sheet.getCellRangeByPosition(x1, y1, x2, y2);
XPropertySet propSet = UnoRuntime.queryInterface(XPropertySet.class, range);
XPropertySet validProp =(XPropertySet) propSet.getPropertyValue("Validation");
validProp.setPropertyValue("ShowErrorMessage", new Boolean(true));
validProp.setPropertyValue("ErrorMessage", "Please enter a valid time");
validProp.setPropertyValue("ErrorAlertStyle", ValidationAlertStyle.INFO);
propSet.setPropertyValue("Validation", validProp);
}
它是根据 OpenOffice Developer's Guide 中提供的示例建模的。
我正在尝试从测试中调用该方法 class:
@Test
public void test() {
try {
XComponentLoader loader = LibreBootstrapper.getLoader();
assertTrue( (loader instanceof XComponentLoader));
XSpreadsheetDocument doc = SpreadsheetHelper.getSpreadsheetDoc(loader);
assertTrue( (doc instanceof XSpreadsheetDocument));
XSpreadsheet sheet = SpreadsheetHelper.getSheetByIndex(SpreadsheetHelper.getSheets(doc), 0);
SpreadsheetHelper.insertIntoCell(0, 0, sheet, 400);
SpreadsheetHelper.insertIntoCell(1, 0, sheet, 300);
SpreadsheetHelper.insertFormula(2, 0, sheet, "=SUM(A1:B1)");
SpreadsheetHelper.insertIntoCell(2, 2, sheet, "Hello World!");
SpreadsheetHelper.setProperty(0, 0, 8, 10, sheet, "CellStyle", "Heading 1");
int formatCode = SpreadsheetHelper.getFormatCode(doc, NumberFormat.TIME);
SpreadsheetHelper.setProperty(0, 0, 8, 10, sheet, "NumberFormat", formatCode);
SpreadsheetHelper.applyValidation(0, 0, 8, 10, sheet, ValidationType.TIME);
} catch (Exception e) {
e.printStackTrace();
}
}
但是,我收到以下错误:
java.lang.ClassCastException: com.sun.star.uno.Any cannot be cast to com.sun.star.beans.XPropertySet
其中有以下堆栈
at edu.cmu.office.SpreadsheetHelper.applyValidation(SpreadsheetHelper.java:125)
at HelperTests.test(HelperTests.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
我什至尝试从本质上复制和粘贴示例中的代码(链接到上面),但仍然收到相同的错误。
我对 LibreOffice 不太熟悉 API;任何帮助将不胜感激。
编辑:
实际抛出错误的行如下:
XPropertySet validProp =(XPropertySet) propSet.getPropertyValue("Validation");
即使直接从示例中复制粘贴,我也会收到同样的错误:
public static void applyValidation(int x1, int y1, int x2, int y2, XSpreadsheet sheet, ValidationType type) throws UnknownPropertyException, WrappedTargetException, IndexOutOfBoundsException, IllegalArgumentException, PropertyVetoException {
// XCellRange range = sheet.getCellRangeByPosition(x1, y1, x2, y2);
// XPropertySet propSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, range);
//
// XPropertySet validProp =(XPropertySet) propSet.getPropertyValue("Validation");
// validProp.setPropertyValue("ShowErrorMessage", new Boolean(true));
// validProp.setPropertyValue("ErrorMessage", "Please enter a valid time");
// validProp.setPropertyValue("ErrorAlertStyle", ValidationAlertStyle.INFO);
//
// propSet.setPropertyValue("Validation", validProp);
//
// --- Data validation ---
com.sun.star.table.XCellRange xCellRange = sheet.getCellRangeByName("A7:C7");
com.sun.star.beans.XPropertySet xCellPropSet = (com.sun.star.beans.XPropertySet)
UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xCellRange);
// validation properties
com.sun.star.beans.XPropertySet xValidPropSet = (com.sun.star.beans.XPropertySet)
xCellPropSet.getPropertyValue("Validation");
xValidPropSet.setPropertyValue("Type", com.sun.star.sheet.ValidationType.DECIMAL);
xValidPropSet.setPropertyValue("ShowErrorMessage", new Boolean(true));
xValidPropSet.setPropertyValue("ErrorMessage", "This is an invalid value!");
xValidPropSet.setPropertyValue("ErrorAlertStyle", com.sun.star.sheet.ValidationAlertStyle.STOP);
// condition
com.sun.star.sheet.XSheetCondition xCondition = (com.sun.star.sheet.XSheetCondition)
UnoRuntime.queryInterface(com.sun.star.sheet.XSheetCondition.class, xValidPropSet);
xCondition.setOperator(com.sun.star.sheet.ConditionOperator.BETWEEN);
xCondition.setFormula1("0.0");
xCondition.setFormula2("5.0");
// apply on cell range
xCellPropSet.setPropertyValue("Validation", xValidPropSet);
}
在以下行中:
com.sun.star.beans.XPropertySet xValidPropSet = (com.sun.star.beans.XPropertySet)
xCellPropSet.getPropertyValue("Validation");
你好像忘记正确投射了。代码应该像示例:
XPropertySet xCellPropSet = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class, xCellRange);
...而不是这个:
XPropertySet propSet = UnoRuntime.queryInterface(XPropertySet.class, range);
编辑:
显然,示例不正确。这是工作代码:
XPropertySet validProp = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class,
propSet.getPropertyValue("Validation"));
这些类型的问题在使用 Java 和 UNO 时很烦人。我更喜欢 Python 和 UNO,其中一个原因是因为 queryInterface
不是必需的。