如何从ExcelDNA重新触发GetCustomUI() [英] How to re-trigger GetCustomUI() from ExcelDNA

查看:624
本文介绍了如何从ExcelDNA重新触发GetCustomUI()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ExcelDNA中创建了一个自定义功能区(扩展ExcelRibbon)并覆盖了GetCustomUI()方法,以从字符串列表中创建一个菜单控件。基本上:

  public override string GetCustomUI(string RibbonID)
{
string customUIXml =
@< customUI xmlns ='http://schemas.microsoft.com/office/2006/01/customui'loadImage ='LoadImage'onLoad ='OnRibbonLoaded'>
< ribbon>
< tabs>
< tab id ='CustomTab'label ='我的动态标签'>
< group id ='SampleGroup'label ='我的样本组'>
< menu description ='description'enabled ='true'id ='menuItem'visible ='true'size ='normal'>;

foreach(_ItemNameList中的string itemName)
customUIXml + = $< button id ='btn_tool_ {itemName}'label ='{itemName}'onAction ='MyMethod'/> ;

customUIXml + =
@< / menu>
< / group>
< / tab>
< / tabs>
< / ribbon>
< / customUI>;

return customUIXml;
}

因为 _ItemNameList 从不同的文件/系统检索,我不能将这个customUI标签直接放在.dna文件(据我所知),因此我通过GetCustomUI()加载它。



作为概念证明, onAction 方法将在 _ItemNameList 中添加一个新项目用户点击菜单项。



但是如何让Excel再次调用GetCustomUI以重建XML?



我有尝试使功能对象本身无效,但这不会触发Excel再次调用GetCustomUI。



我可以想到的最好的事情(尽管我仍然要测试这个)是创建一个包含不可见的占位符按钮的整个菜单的菜单(通过指定getVisible回调),当用户点击按钮时,只需要使现在变得可见的按钮无效,将新项添加到 _ItemNameList 。我仍然需要考虑如何获得正确的按钮参考虽然...这也感觉有点脏。



任何想法如何使Excel重建功能区?
还可以打开其他想法,让我在ExcelRibbon菜单中添加项目。

解决方案

我想你正在寻找 dynamicMenu 控件,这将触发一个事件,让您动态设置菜单的内容,每次点击它。



这样的东西:

 <?xml version =1.0encoding =utf- 8?> 
< customUI xmlns =http://schemas.microsoft.com/office/2006/01/customuionLoad =OnLoad>
< ribbon>
< tabs>
< tab id =MyAwesomeRibbongetLabel =GetLabelkeytip = - >
< group id =MyDynamicGrouplabel =Hello Dynamic!>
< dynamicMenu id =MyDynamicMenu
label =点击Awesome
getContent =OnGetContent
invalidateContentOnDrop =true
size =large
imageMso =HappyFace/>
< / group>
< / tab>
< / tabs>
< / ribbon>
< / customUI>

您的 OnGetContent 方法每次都会被触发用户点击功能区按钮,所以您可以动态地组合项目。



例如

  public string OnGetContent(IRibbonControl control)
{
var menuXml = @
< menu xmlns ='http://schemas.microsoft.com/office/2006 / 01 / customui'itemSize ='large'>
< button id ='SaveButton'
label ='保存'
onAction ='OnSave'
imageMso ='FileSave '/>

< button id ='AboutButton'
label ='关于...'
onAction ='OnAbout'
imageMso ='FileDocumentInspect' />
< / menu>;

return menuXml;
}

当然,您必须从列表中动态构建此字符串,等等。


I have, in ExcelDNA, created a custom ribbon (extending ExcelRibbon) and overridden the GetCustomUI() method to create a menu control from a list of strings. Basically:

public override string GetCustomUI(string RibbonID)
{
    string customUIXml =
    @"<customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui' loadImage='LoadImage' onLoad='OnRibbonLoaded' >
        <ribbon>
        <tabs>
        <tab id='CustomTab' label='My Dynamic Tab'>
        <group id='SampleGroup' label='My Sample Group'>
        <menu description='description' enabled='true' id='menuItem' visible='true' size='normal' >";

    foreach (string itemName in _ItemNameList)
        customUIXml += $"<button id='btn_tool_{itemName}' label='{itemName}' onAction='MyMethod' />";

    customUIXml +=
        @"</menu>
        </group >
        </tab>
        </tabs>
        </ribbon>
      </customUI>";

    return customUIXml;
}

Because _ItemNameList is retrieved from a different file/system, I can't place this customUI tag directly into the .dna file (as far as I know) and hence I build it on load via the GetCustomUI().

As a proof of concept, the onAction method will add a new item to _ItemNameList when the user clicks on a menu item.

But how do I get Excel to call GetCustomUI again in order to have the XML rebuilt?

I have tried invalidating the ribbon object itself, but this does not trigger Excel to call GetCustomUI again.

The next best thing I can think of (although I'm still to test this) is to create a menu with a whole load of invisible 'placeholder' buttons (by specifying the getVisible callbacks) and when the user clicks on a button, just invalidate the button that needs to now become visible, showing the new item added to _ItemNameList. I still need to think through how to get the correct button reference though... This also feels a little dirty.

Any ideas how to have Excel rebuild the ribbon? Also open to other ideas to allow me to add items to a menu in an ExcelRibbon.

解决方案

I think you are looking for the dynamicMenu control, which will trigger an event to let you dynamically set the contents of the menu, every time you click on it.

Something like this:

<?xml version="1.0" encoding="utf-8" ?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad">
  <ribbon>
    <tabs>
      <tab id="MyAwesomeRibbon" getLabel="GetLabel" keytip="-">
        <group id="MyDynamicGroup" label="Hello Dynamic!">
          <dynamicMenu id="MyDynamicMenu"
                       label="Click for Awesome"
                       getContent="OnGetContent"
                       invalidateContentOnDrop="true"
                       size="large"
                       imageMso="HappyFace" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

Your OnGetContent method will get fired every time the user clicks on the ribbon button, so you can dynamically assemble the items.

e.g.

public string OnGetContent(IRibbonControl control)
{
    var menuXml = @"
        <menu xmlns='http://schemas.microsoft.com/office/2006/01/customui' itemSize='large'>
            <button id='SaveButton'
                    label='Save'
                    onAction='OnSave'
                    imageMso='FileSave' />

            <button id='AboutButton'
                    label='About...'
                    onAction='OnAbout'
                    imageMso='FileDocumentInspect' />
        </menu>";

    return menuXml;
}

Of course, you'd have to build this string dynamically from your list, etc.

这篇关于如何从ExcelDNA重新触发GetCustomUI()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆