You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建实现行列联动选择的LibreOffice/OpenOffice .oxt插件

实现LibreOffice Calc行列联动选择插件的开发指引

开发准备

  • 启用宏功能:打开Calc后,依次进入工具 -> 选项 -> LibreOffice -> 安全性 -> 宏安全性,设置为「中」,允许运行自签名或未签名宏(Linux下可通过GPG生成自签名证书)。
  • Python环境支持:Linux下安装libreoffice-script-provider-python包(Debian/Ubuntu用apt install,RHEL/CentOS用dnf install),确保LibreOffice能识别Python脚本。

核心功能实现思路(Python示例)

需要监听选择事件,判断是否选中整行,再匹配对应姓名的列并联动选中:

import uno
from com.sun.star.awt import XSelectionListener
from com.sun.star.lang import EventObject
from com.sun.star.table import CellRangeAddress

class RowColLinkListener(XSelectionListener):
    def __init__(self, doc):
        self.doc = doc
        self.active_sheet = doc.CurrentController.ActiveSheet
        # 可配置:姓名所在的行号(列标题行)、姓名所在的列号(行内姓名列)
        self.header_row = 0
        self.name_col_in_row = 0

    def selectionChanged(self, event):
        selection = self.doc.CurrentController.Selection
        # 判断是否为整行选中(点击行号触发)
        if hasattr(selection, "IsRowRange") and selection.IsRowRange:
            selected_row = selection.RangeAddress.StartRow
            # 获取当前行的玩家姓名
            name_cell = self.active_sheet.getCellByPosition(self.name_col_in_row, selected_row)
            player_name = name_cell.String.strip()
            if not player_name:
                return
            # 遍历标题行,找到匹配姓名的列
            target_col = -1
            for col_idx in range(self.active_sheet.Columns.Count):
                header_cell = self.active_sheet.getCellByPosition(col_idx, self.header_row)
                if header_cell.String.strip() == player_name:
                    target_col = col_idx
                    break
            # 联动选中行和列(模拟Ctrl点击)
            if target_col != -1:
                # 构造行、列的选中区域
                row_range = CellRangeAddress(
                    Sheet=0,
                    StartRow=selected_row, EndRow=selected_row,
                    StartColumn=0, EndColumn=self.active_sheet.Columns.Count-1
                )
                col_range = CellRangeAddress(
                    Sheet=0,
                    StartRow=0, EndRow=self.active_sheet.Rows.Count-1,
                    StartColumn=target_col, EndColumn=target_col
                )
                # 同时选中两个区域
                self.doc.CurrentController.Select((row_range, col_range))

    def disposing(self, event):
        pass

# 注册监听器(可绑定到文档打开事件)
def register_row_col_listener():
    ctx = uno.getComponentContext()
    desktop = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
    current_doc = desktop.getCurrentComponent()
    if current_doc.supportsService("com.sun.star.sheet.SpreadsheetDocument"):
        listener = RowColLinkListener(current_doc)
        current_doc.CurrentController.addSelectionListener(listener)

打包为.oxt插件

  1. 创建目录结构:
    RowColLink/
    ├── META-INF/
    │   └── manifest.xml
    ├── Scripts/
    │   └── python/
    │       └── row_col_link.py
    ├── description.xml
    └── description.txt
    
  2. 关键配置文件示例:
    • description.xml(插件元数据):
      <?xml version="1.0" encoding="UTF-8"?>
      <description xmlns="http://openoffice.org/extensions/description/2006"
                   xmlns:xlink="http://www.w3.org/1999/xlink">
          <version value="1.0"/>
          <identifier value="org.yourname.RowColLink"/>
          <display-name><name lang="en">Row-Column Link Selector</name></display-name>
          <publisher>Your Name</publisher>
          <dependencies><OpenOffice.org-minimal-version value="5.0"/></dependencies>
      </description>
      
    • manifest.xml(注册脚本与事件):
      <?xml version="1.0" encoding="UTF-8"?>
      <manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">
          <manifest:file-entry manifest:media-type="application/vnd.sun.star.uno-component;type=Python"
                               manifest:full-path="Scripts/python/row_col_link.py"/>
          <manifest:file-entry manifest:media-type="application/vnd.sun.star.configuration-data"
                               manifest:full-path="Config.xcu"/>
      </manifest:manifest>
      
  3. 打包:将整个目录压缩为ZIP包,改后缀为.oxt,在LibreOffice中通过工具 -> 扩展管理器安装。

实用资料来源

  • 本地内置帮助:按F1打开LibreOffice帮助,搜索「Python Scripting」「UNO API」「XSelectionListener」,包含官方接口定义与基础示例,无需联网。
  • 宏录制器:打开工具 -> 宏 -> 录制宏,手动操作选中行、Ctrl选中列,录制后查看生成的Basic代码,快速掌握API调用逻辑。
  • 系统内置示例:Linux下查看/usr/share/libreoffice/examples/scripting目录,包含Python和Basic的实战脚本示例。
  • LibreOffice SDK文档:安装libreoffice-sdk包后,查看/usr/share/doc/libreoffice-sdk目录下的完整UNO API参考手册,可查接口参数、返回值细节。
  • 社区论坛:LibreOffice官方论坛的「Macro and UNO API」板块,有大量用户分享的行列选择、事件监听等问题的解决方案。

内容的提问来源于stack exchange,提问作者Roelof Berkepeis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 11:53:13