OpenOffice API - 表的最佳宽度选项(所有列)

发布于 2024-08-03 11:45:33 字数 744 浏览 4 评论 0原文

我正在研究 Java API,它通过 UNO 与 OpenOffice(swriter) 交互。 对于 TextTable,我很难设置 TableColumn 的“OptimalWidth”属性。

我已尝试以下代码,似乎 getColumns() 方法无法将我带到 TableColumn 的属性,而只能让您插入和删除列。

XTableColumns xColumns = xTextTable.getColumns();
XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
        XIndexAccess.class, xColumns);

for (int i = 0; i < xIndexAccess.getCount(); i++) {
    XPropertySet xColumnProps = (XPropertySet) UnoRuntime
            .queryInterface(XPropertySet.class,
                    (Any) xIndexAccess.getByIndex(i));
    if (xColumnProps != null) {
        xColumn.setPropertyValue("OptimalWidth", new Boolean(true));
    }
}

任何人都可以帮助我或给我任何设置表格 OptimalWidth 属性的提示吗? 预先非常感谢您!

I am working on Java API which interacts with OpenOffice(swriter) through UNO.
For TextTable, I am having hard time setting TableColumn's "OptimalWidth" property.

I have tried the following code and it seems that getColumns() method cannot take me to TableColumn's property and let you only insert and remove columns.

XTableColumns xColumns = xTextTable.getColumns();
XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
        XIndexAccess.class, xColumns);

for (int i = 0; i < xIndexAccess.getCount(); i++) {
    XPropertySet xColumnProps = (XPropertySet) UnoRuntime
            .queryInterface(XPropertySet.class,
                    (Any) xIndexAccess.getByIndex(i));
    if (xColumnProps != null) {
        xColumn.setPropertyValue("OptimalWidth", new Boolean(true));
    }
}

Can anyone help me out or give me any tips setting OptimalWidth property for a table?
Thank you very much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⊕婉儿 2024-08-10 11:45:33

这是一个相当艰辛的过程,但我最终成功地创建了一个函数来完成这个任务:

private void optimizeTableColumnWidths(XTextTable table)
throws Exception
{
    XTextViewCursorSupplier cursorSupplier = (XTextViewCursorSupplier)
            UnoRuntime.queryInterface(XTextViewCursorSupplier.class,
                    document.getCurrentController());
    XTextViewCursor viewCursor = cursorSupplier.getViewCursor();

    String cellName = "A1";
    XText cellText = (XText)UnoRuntime.queryInterface(
            XText.class, table.getCellByName(cellName));
    XTextCursor cursor = cellText.createTextCursor();

    viewCursor.gotoRange(cursor, false);
    viewCursor.gotoEnd(true);
    viewCursor.gotoEnd(true);

    XController controller = document.getCurrentController();
    XFrame frame = controller.getFrame();
    XDispatchProvider dispatchProvider = (XDispatchProvider)
        UnoRuntime.queryInterface(XDispatchProvider.class, frame);

    String unoAction = ".uno:SetOptimalColumnWidth";
    String targetFrameName = "";
    int searchFlags = 0;
    PropertyValue[] properties = new PropertyValue[0];

    dispatchHelper.executeDispatch(
            dispatchProvider,
            unoAction,
            targetFrameName,
            searchFlags,
            properties);
}

重要的是,dispatchHelper 来自 OpenOffice 上下文,而不是来自当前文档。我这样检索了dispatchHelper:

  XComponentContext context = Bootstrap.bootstrap();
  XMultiComponentFactory factory =
        context.getServiceManager();
  Object dispatchHelperObject = factory.createInstanceWithContext(
        "com.sun.star.frame.DispatchHelper", ooContext);
  this.dispatchHelper = (XDispatchHelper)UnoRuntime.queryInterface(
        XDispatchHelper.class, dispatchHelperObject);

It was quite an odyssey to find out, but I finally managed to create a function which does exactly that:

private void optimizeTableColumnWidths(XTextTable table)
throws Exception
{
    XTextViewCursorSupplier cursorSupplier = (XTextViewCursorSupplier)
            UnoRuntime.queryInterface(XTextViewCursorSupplier.class,
                    document.getCurrentController());
    XTextViewCursor viewCursor = cursorSupplier.getViewCursor();

    String cellName = "A1";
    XText cellText = (XText)UnoRuntime.queryInterface(
            XText.class, table.getCellByName(cellName));
    XTextCursor cursor = cellText.createTextCursor();

    viewCursor.gotoRange(cursor, false);
    viewCursor.gotoEnd(true);
    viewCursor.gotoEnd(true);

    XController controller = document.getCurrentController();
    XFrame frame = controller.getFrame();
    XDispatchProvider dispatchProvider = (XDispatchProvider)
        UnoRuntime.queryInterface(XDispatchProvider.class, frame);

    String unoAction = ".uno:SetOptimalColumnWidth";
    String targetFrameName = "";
    int searchFlags = 0;
    PropertyValue[] properties = new PropertyValue[0];

    dispatchHelper.executeDispatch(
            dispatchProvider,
            unoAction,
            targetFrameName,
            searchFlags,
            properties);
}

It is important that the dispatchHelper comes from the OpenOffice context, and not from the current document. I retrieved the dispatchHelper this way:

  XComponentContext context = Bootstrap.bootstrap();
  XMultiComponentFactory factory =
        context.getServiceManager();
  Object dispatchHelperObject = factory.createInstanceWithContext(
        "com.sun.star.frame.DispatchHelper", ooContext);
  this.dispatchHelper = (XDispatchHelper)UnoRuntime.queryInterface(
        XDispatchHelper.class, dispatchHelperObject);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文