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

Excel Office.js:右键上下文菜单子项启用禁用及分隔设置问询

Excel Office加载项:动态控制右键上下文菜单项启用/禁用及菜单样式问题

核心需求

  • 为现有Excel Office加载项开发新功能,新增右键上下文菜单及下属子菜单
  • 根据工作表、特定单元格或选区的状态,动态启用/禁用子菜单项

我了解自定义选项卡中的Ribbon控件支持动态启用/禁用,但未找到右键上下文菜单的相关配置文档。以下是当前的上下文菜单配置代码:

<ExtensionPoint xsi:type="ContextMenu">
    <OfficeMenu id="ContextMenuCell">
        <Control xsi:type="Menu" id="Menu">
            <Label resid="Dropdown.Label" />
            <Supertip>
                <Title resid="Dropdown.Label" />
                <Description resid="Dropdown.Tooltip" />
            </Supertip>
            
            <Items>
                <Item id="Menu.Item1">
                    <Label resid="Item1.Label"/>
                    <Supertip>
                        <Title resid="Item1.Label" />
                        <Description resid="Item1.Tooltip" />
                    </Supertip>
                    
                    <Action xsi:type="ExecuteFunction">
                        <FunctionName>signOff</FunctionName>
                    </Action>
                    <Enabled>false</Enabled>
                </Item>
                <Item id="Menu.Item2">
                    <Label resid="Item2.Label"/>
                    <Supertip>
                        <Title resid="Item2.Label" />
                        <Description resid="Item2.Tooltip" />
                    </Supertip>
                    
                    <Action xsi:type="ExecuteFunction">
                        <FunctionName>signOff2</FunctionName>
                    </Action>
                </Item>
            </Items>
        </Control>
    </OfficeMenu>
</ExtensionPoint>

尝试过程及问题

曾尝试使用OfficeJS中控制Ribbon按钮的代码来修改上下文菜单项状态,但报错:

尝试的代码:

async function signOff(args) {
  console.log("SignOff 1 started");
  Office.ribbon.requestUpdate({
    tabs: [
        {
            id: 'ContextMenuCell',
            controls: [
                {
                    id: 'Menu.Item1',
                    enabled: true
                }
            ]
        }
    ]
});
  args.completed();
}
Office.actions.associate("signOff", signOff);

错误信息:

Uncaught (in promise) RichApi.Error: ControlIdNotFound
    at new n (excel-win32-16.01.js:25:257638)
    at i.processRequestExecutorResponseMessage (excel-win32-16.01.js:25:321804)
    at excel-win32-16.01.js:25:319867

显然Office.ribbon.requestUpdate仅针对Ribbon控件,无法用于右键上下文菜单的状态控制。

额外问题

  • 能否像Excel粘贴选项那样为子菜单添加分组标签?
  • 能否在子菜单项之间添加分隔线?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 05:25:28