gpt4 book ai didi

java - LibreOffice Java - 无法将 uno.Any 转换为 XPropertySet

转载 作者:行者123 更新时间:2023-11-30 06:26:26 25 4
gpt4 key购买 nike

我正在尝试使用 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");

最佳答案

您似乎忘记正确转换它。代码应该类似于示例:

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 Java - 无法将 uno.Any 转换为 XPropertySet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47116269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com