如何使用C#和Excel-DNA包在Excel中合并两个RibbonUI?

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

How to merge two ribbonui in excel by using c# with excel-dna package

问题

我有两个用C#编写的Excel插件项目,使用excel-dna包编写,第一个插件具有以下自定义UI:

<?xml version="1.0" encoding="utf-8" ?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon>
        <tabs>
            <tab id='tab1' label='MyAddIn'>
                <!--其他分组-->
            </tab>
        </tabs>
    </ribbon>
    <backstage>
        <tab id='myTab' label='myTab' title='myTabTitle' insertBeforeMso='TabInfo'>
        </tab>
    </backstage>
</customUI>

而第二个插件更像是第一个插件的扩展插件,它具有以下自定义UI:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
    <ribbon startFromScratch="false">
        <tabs>
            <tab id="tab1">
                <group id="Report_x" label="Report1">
                    <button id="reports_btn" size="large" label="Reports" onAction="on_api_rpts_btn_click" tag="Report" imageMso="AdpDiagramTableModesMenu"/>
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

这两个插件都通过excel-dna包加载自定义UI,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

using ExcelDna.Integration.CustomUI;

namespace SomeNameSpace
{
    public class MyRibbonClass: ExcelRibbon
    {
        public override string GetCustomUI(string RibbonID)
        {
            string customUiStr = string.empty;
            // 读取资源
            return customUiStr;
        }
    }
}

但是我不知道为什么我只能在Excel的功能区中看到MyAddIn选项卡以及在第一个插件中定义的所有分组,第二个插件的选项卡只显示在"Excel选项" -> "自定义功能区" -> "空标签"分组中。

英文:

I have two excel-addin project is wroted by C# with excel-dna package and the first add-in have a customui like follow:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;customUI xmlns=&quot;http://schemas.microsoft.com/office/2009/07/customui&quot;&gt;
	&lt;ribbon&gt;
		&lt;tabs&gt;
			&lt;tab id=&#39;tab1&#39; label=&#39;MyAddIn&#39;&gt;
				&lt;!--Other group--&gt;
			&lt;/tab&gt;
		&lt;/tabs&gt;
	&lt;/ribbon&gt;
	&lt;backstage&gt;
		&lt;tab id=&#39;myTab&#39; label=&#39;myTab&#39; title=&#39;myTabTitle&#39; insertBeforeMso=&#39;TabInfo&#39;&gt;
		&lt;/tab&gt;
	&lt;/backstage&gt;
&lt;/customUI&gt;

And the second add-in are more like an extension add-in from the first add-in, it have a customui like follow:

&lt;customUI xmlns=&quot;http://schemas.microsoft.com/office/2009/07/customui&quot;&gt;
	&lt;ribbon startFromScratch=&quot;false&quot;&gt;
		&lt;tabs&gt;
			&lt;tab id=&quot;tab1&quot;&gt;
				&lt;group id=&quot;Report_x&quot; label=&quot;Report1&quot;&gt;
					&lt;button id=&quot;reports_btn&quot; size=&quot;large&quot; label=&quot;Reports&quot; onAction=&quot;on_api_rpts_btn_click&quot; tag=&quot;Report&quot; imageMso=&quot;AdpDiagramTableModesMenu&quot;/&gt;
				&lt;/group&gt;
			&lt;/tab&gt;
		&lt;/tabs&gt;
	&lt;/ribbon&gt;
&lt;/customUI&gt;

Both of two addin loads the customui by using excel-dna package like follow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

using ExcelDna.Integration.CustomUI;

namespace SomeNameSpace
{
    public class MyRibbonClass: ExcelRibbon
    {
        public override string GetCustomUI(string RibbonID)
        {
            string customUiStr = string.empty;
            // read resources
            return customUiStr;
        }
    }
}

But i don't know why i only can see the MyAddIn tab and all group which is definied in the first add-in on Excel ribbon, the second add-in's tab only show in "ExcelOptions" -> "CustomRibbon" -> "Empty Tag" group

答案1

得分: 1

idQ属性存在的目的是为了允许多个加载项共享容器,如自定义选项卡和分组。

在以下的VBA示例中,两个Excel加载项共享添加项选项卡上的相同的“Contoso”分组;每个加载项都向其添加一个按钮。关键在于在<customUI>标签中指定相同的唯一命名空间。然后,控件可以使用idQ引用此命名空间。

对于第一个加载项,您可以使用以下标记:

&lt;customUI xmlns=&quot;http://schemas.microsoft.com/office/2006/01/customui&quot; 
  xmlns:x=&quot;myNameSpace&quot; &gt;
  &lt;ribbon&gt;
    &lt;tabs&gt;
      &lt;tab idMso=&quot;TabAddIns&quot;&gt;
        &lt;group idQ=&quot;x:Contoso&quot; label=&quot;Contoso&quot;&gt;
          &lt;button id=&quot;C1&quot; label=&quot;Contoso Button 1&quot; size=&quot;large&quot; 
            imageMso=&quot;FileSave&quot; onAction=&quot;c_action1&quot; /&gt;
        &lt;/group&gt;
      &lt;/tab&gt;
    &lt;/tabs&gt;
  &lt;/ribbon&gt;
&lt;/customUI&gt;

对于第二个加载项:

&lt;customUI xmlns=&quot;http://schemas.microsoft.com/office/2006/01/customui&quot; 
  xmlns:x=&quot;myNameSpace&quot; &gt;
  &lt;ribbon&gt;
    &lt;tabs&gt;
      &lt;tab idMso=&quot;TabAddIns&quot;&gt;
        &lt;group idQ=&quot;x:Contoso&quot; label=&quot;Contoso&quot;&gt;
          &lt;button id=&quot;C1&quot; label=&quot;Contoso Button 1&quot; size=&quot;large&quot; 
            imageMso=&quot;FileSave&quot; onAction=&quot;c_action1&quot; /&gt;
        &lt;/group&gt;
      &lt;/tab&gt;
    &lt;/tabs&gt;
  &lt;/ribbon&gt;
&lt;/customUI&gt;

如果您开发了一个COM加载项来自定义Fluent UI,命名空间名称必须是COM加载项的ProgID值,但行为是相同的(建议如此)。

注意,默认情况下,如果加载项尝试操作Microsoft Office用户界面(UI)并失败,将不会显示错误消息。但是,您可以配置Microsoft Office应用程序以显示与UI相关的错误消息。您可以使用这些消息来帮助确定为什么自定义功能区不显示,或者为什么功能区显示但没有控件显示。有关更多信息,请参阅如何:显示加载项用户界面错误

英文:

The idQ property of controls exists to enable multiple add-ins to share containers, such as custom tabs and groups.

In the following VBA example, two Excel add-ins share the same "Contoso" group on the add-ins tab; each adds one button to it. The key is specifying the same unique namespace in the <customUI> tag. Then, controls can reference this namespace by using idQ.

For the first add-in you could use the following markup:

&lt;customUI xmlns=&quot;http://schemas.microsoft.com/office/2006/01/customui&quot; 
  xmlns:x=&quot;myNameSpace&quot; &gt;
  &lt;ribbon&gt;
    &lt;tabs&gt;
      &lt;tab idMso=&quot;TabAddIns&quot;&gt;
        &lt;group idQ=&quot;x:Contoso&quot; label=&quot;Contoso&quot;&gt;
          &lt;button id=&quot;C1&quot; label=&quot;Contoso Button 1&quot; size=&quot;large&quot; 
            imageMso=&quot;FileSave&quot; onAction=&quot;c_action1&quot; /&gt;
        &lt;/group&gt;
      &lt;/tab&gt;
    &lt;/tabs&gt;
  &lt;/ribbon&gt;
&lt;/customUI&gt;

And for the second add-in:

&lt;customUI xmlns=&quot;http://schemas.microsoft.com/office/2006/01/customui&quot; 
  xmlns:x=&quot;myNameSpace&quot; &gt;
  &lt;ribbon&gt;
    &lt;tabs&gt;
      &lt;tab idMso=&quot;TabAddIns&quot;&gt;
        &lt;group idQ=&quot;x:Contoso&quot; label=&quot;Contoso&quot;&gt;
          &lt;button id=&quot;C1&quot; label=&quot;Contoso Button 1&quot; size=&quot;large&quot; 
            imageMso=&quot;FileSave&quot; onAction=&quot;c_action1&quot; /&gt;
        &lt;/group&gt;
      &lt;/tab&gt;
    &lt;/tabs&gt;
  &lt;/ribbon&gt;
&lt;/customUI&gt;

If you develop a COM add-in to customize the Fluent UI, the namespace name must be the ProgID value of the COM add-in, but the behavior is otherwise the same (it is recommended).

Note, by default, if a an add-in attempts to manipulate the Microsoft Office user interface (UI) and fails, no error message is displayed. However, you can configure Microsoft Office applications to display messages for errors that relate to the UI. You can use these messages to help determine why a custom ribbon does not appear, or why a ribbon appears but no controls appear. See How to: Show Add-in user interface errors for more information.

huangapple
  • 本文由 发表于 2023年6月19日 08:30:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503007.html
匿名

发表评论

匿名网友

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

确定