Excel Office.js – 在右键单击时启用/禁用多个上下文菜单项

huangapple go评论56阅读模式
英文:

Excel Office.js - Enable/Disable Multiple Context Menu Items on right click

问题

我正在为现有的Excel办公室插件开发一个新功能。这个功能需要在菜单项下添加一个新的上下文菜单项和子菜单项。

我想根据工作表或工作表中的特定单元格启用/禁用子菜单项。

请问是否有Excel Office JS中的这样一个功能?我知道在自定义选项卡内的功能区中有这样的选项,但我在右键单击的上下文菜单(Contextual Menu)没有看到任何文档。

<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>

编辑 1

根据下面的建议,我尝试使用与OfficeJS文档建议的启用/禁用功能区按钮相同的代码。

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

OfficeJS的requestupdate()方法似乎是为功能区而设计的,因为它应该使用 Office.ribbon.requestUpdate(input: RibbonUpdaterData) 调用。

只要我的需求不太清晰。

我在我的上下文菜单中有下面的"子菜单 1"和"子菜单 2"项。
Excel Office.js – 在右键单击时启用/禁用多个上下文菜单项

我想根据不同的业务需求启用/禁用这些子菜单,例如基于工作表、基于单元格或单元格范围等。

编辑 2

我们是否可以像Excel提供粘贴选项一样分开子菜单项? 参考下面的图像。

Excel Office.js – 在右键单击时启用/禁用多个上下文菜单项

我们是否可以在两个子菜单项之间添加分隔符,就像Excel提供菜单项一样? 参考下面的图像。

Excel Office.js – 在右键单击时启用/禁用多个上下文菜单项

英文:

I'm working on a new feature for an existing Excel office add-in. This feature requires a new context menu item and Sub-menu Items under the menu item.

I want to enable/disable the Sub-Menu Item based on the worksheet or a specific cell within a sheet.

Can someone please let me know if there is such a feature available with Excel Office JS?

I am aware that there is such an option for Ribbons inside a Custom Tab but I dont see any documentation for Contextual Menu(Right Click)

    &lt;ExtensionPoint xsi:type=&quot;ContextMenu&quot;&gt;
                      &lt;OfficeMenu id=&quot;ContextMenuCell&quot;&gt;
                        &lt;Control xsi:type=&quot;Menu&quot; id=&quot;Menu&quot;&gt;
        									&lt;Label resid=&quot;Dropdown.Label&quot; /&gt;
        									&lt;Supertip&gt;
        										&lt;Title resid=&quot;Dropdown.Label&quot; /&gt;
        										&lt;Description resid=&quot;Dropdown.Tooltip&quot; /&gt;
        									&lt;/Supertip&gt;
        									
        									&lt;Items&gt;
        										&lt;Item id=&quot;Menu.Item1&quot;&gt;
        											&lt;Label resid=&quot;Item1.Label&quot;/&gt;
        											&lt;Supertip&gt;
        												&lt;Title resid=&quot;Item1.Label&quot; /&gt;
        												&lt;Description resid=&quot;Item1.Tooltip&quot; /&gt;
        											&lt;/Supertip&gt;
        											
                                                &lt;Action xsi:type=&quot;ExecuteFunction&quot;&gt;
        										    &lt;FunctionName&gt;signOff&lt;/FunctionName&gt;
        									    &lt;/Action&gt;
                                           &lt;Enabled&gt;false&lt;/Enabled&gt;
        										&lt;/Item&gt;
                                                &lt;Item id=&quot;Menu.Item2&quot;&gt;
                                                &lt;Label resid=&quot;Item2.Label&quot;/&gt;
                                                &lt;Supertip&gt;
                                                    &lt;Title resid=&quot;Item2.Label&quot; /&gt;
                                                    &lt;Description resid=&quot;Item2.Tooltip&quot; /&gt;
                                                &lt;/Supertip&gt;
                                                
                                           &lt;Action xsi:type=&quot;ExecuteFunction&quot;&gt;
                                                &lt;FunctionName&gt;signOff2&lt;/FunctionName&gt;
                                            &lt;/Action&gt;
                                            &lt;/Item&gt;
                            
        									&lt;/Items&gt;
        								&lt;/Control&gt;
                      &lt;/OfficeMenu&gt;
                  &lt;/ExtensionPoint&gt;

EDIT 1

AS per the suggestion below, I tried using the same code which OfficeJS documentation is suggesting for Enabling/Disabling Ribbon Buttons.

My Code:

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

But I get the following error

  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

The requestupdate() method of the OfficeJS, which on the first look makes it pretty clear that this is for Ribbon as it is supposed to be called using Office.ribbon.requestUpdate(input: RibbonUpdaterData)

Just in case my requirement isn't very clear.

I have the below 'Sub-Menu 1' and 'Sub-Menu 2' items in my Contextual Menu.
Excel Office.js – 在右键单击时启用/禁用多个上下文菜单项

I want to enable/disable these submenus based on different business requirements i.e. based on worksheet or based on cell or cell range etc.

EDIT 2

Can we at least separate the submenu items like how Excel provides for Paste options? refer the image below

Excel Office.js – 在右键单击时启用/禁用多个上下文菜单项

Can we add a separator between 2 sub-menu items like how excel provides for menu items? refer image below

Excel Office.js – 在右键单击时启用/禁用多个上下文菜单项

答案1

得分: 1

不管您处理上下文菜单还是自定义功能区选项卡,都可以自定义Office Web加载项中的上下文菜单:

<ExtensionPoint xsi:type="ContextMenu">
  <OfficeMenu id="ContextMenuCell">
    <Control xsi:type="Menu" id="ContextMenu2">
      <!-- 控件信息 -->
    </Control>
    <!-- 其他所需的控件 -->
  </OfficeMenu>
</ExtensionPoint>

如果您希望在Office应用程序启动时禁用自定义按钮或菜单项,请在清单中指定。只需在控件声明的Action元素下方(而不是内部)立即添加一个Enabled元素(其值为false)。

如果您想动态更改状态,需要使用Office.Ribbon接口的requestUpdate方法,请参阅响应事件更改状态以获取更多信息。在启用和禁用加载项命令在Office加载项中创建自定义上下文选项卡文章中了解有关受支持的功能区扩展性的更多信息。

请注意,您可以在Tech Community上发布或投票支持现有的功能请求,在Office开发团队进行规划过程时会考虑这些请求。

英文:

It doesn't matter whether you deal with a context menu or custom ribbon tab. You can customize context menus in Office web add-ins:

&lt;ExtensionPoint xsi:type=&quot;ContextMenu&quot;&gt;
  &lt;OfficeMenu id=&quot;ContextMenuCell&quot;&gt;
    &lt;Control xsi:type=&quot;Menu&quot; id=&quot;ContextMenu2&quot;&gt;
            &lt;!-- information about the control --&gt;
    &lt;/Control&gt;
    &lt;!-- other controls, as needed --&gt;
  &lt;/OfficeMenu&gt;

If you want a custom button or menu item to be disabled when the Office application launches, you specify this in the manifest. Just add an Enabled element (with the value false) immediately below (not inside) the Action element in the declaration of the control.

If you want to change the state dynamically you need to use the requestUpdate method of the Office.Ribbon interface, see Change the state in response to an event for more information. Read more about supported ribbon extensibility in the Enable and Disable Add-in Commands and Create custom contextual tabs in Office Add-ins articles.

Note, you can post or vote for an existing feature request on Tech Community where they are considered when the Office dev team goes through the planning process.

huangapple
  • 本文由 发表于 2023年6月9日 00:20:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定