我正在尝试使用Java与LibreOffice API进行交互。目前,我正在为诸如检索对象,插入文本等操作创建帮助程序类。但是,我在将验证应用于电子表格文档时遇到问题。我的功能如下:
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中提供的示例之后建模的。
我试图从测试类中调用该方法:
@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$1.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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");
答案 0 :(得分:1)
看起来你忘了把它正确地扔掉。代码应该像示例:
<amp-ad
layout="responsive"
height="336"
width="280"
type="adsense"
data-ad-format='auto'
data-ad-client="ca-pub-12345678xx"
data-ad-slot="9876xx"
></amp-ad>
......而不是:
XPropertySet xCellPropSet = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class, xCellRange);
修改强>:
显然,这个例子是不正确的。这是工作代码:
XPropertySet propSet = UnoRuntime.queryInterface(XPropertySet.class, range);
使用Java和UNO时,这些类型的问题很烦人。我更喜欢Python和UNO,其中一个原因是因为XPropertySet validProp = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class,
propSet.getPropertyValue("Validation"));
不是必需的。