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

D365自定义按钮:如何将自定义菜单项加入Export to Excel组或修改Lines逻辑

问题描述

我原本想创建一个「导出到Excel」按钮,结果误做成了「在Excel中打开」按钮。想请教两个问题:

  • 能不能把新的自定义菜单项添加到Export to Excel组里面,而不是组外面?
  • 有没有办法自定义Lines菜单项的逻辑?

以下是我在表单中使用的代码:

public void customizeMenuOptions(OfficeMenuOptions _menuOptions)
{ 
    List exportOptions = _menuOptions.customMenuItems();
        var exportOption = OfficeGeneratedExportMenuItem::construct(tableStr(MyEntity), CustomExportLinesToExcelOptionId);
        exportOption.displayName("123");
        exportOptions.addEnd(exportOption);
}

public ExportToExcelDataEntityContext getDataEntityContext(OfficeGeneratedExportMenuItem _menuItem)
{
    ExportToExcelDataEntityContext context = ExportToExcelDataEntityContext::construct(tableStr(MyEntity), tableFieldGroupStr(MyEntity, AutoReport));
    context.addEntityDefault(tableStr(MyEntity));
    ListEnumerator enumerator = context.entities().getEnumerator();
    while (enumerator.moveNext())
    {
        ExportToExcelDataEntityInfo entity = enumerator.current();

        if (entity.entityName() == tableStr(MyEntity))
        {
            ExportToExcelFilterTreeBuilder filterBuilder = new ExportToExcelFilterTreeBuilder(tableStr(MyEntity));
            FilterNode filterExpression = filterBuilder.areEqual(fieldStr(MyEntity, dataAreaId), curExt());               

            entity.filter(filterExpression);
        }
    }

    return context;
}

解决方案

1. 将自定义菜单项加入Export to Excel组

你当前调用的_menuOptions.customMenuItems()是往自定义菜单项组添加按钮,要加入系统自带的Export to Excel组,需改用getExportMenuItems()方法获取对应列表,再插入自定义项:

修改后的customizeMenuOptions方法:

public void customizeMenuOptions(OfficeMenuOptions _menuOptions)
{ 
    List exportOptions = _menuOptions.getExportMenuItems(); // 获取Export to Excel组的菜单项列表
    var exportOption = OfficeGeneratedExportMenuItem::construct(tableStr(MyEntity), CustomExportLinesToExcelOptionId);
    exportOption.displayName("123");
    exportOptions.addEnd(exportOption);
}

2. 自定义Lines菜单项的逻辑

系统默认Lines菜单项的逻辑绑定到标准导出流程,要自定义它的行为,可通过两种方式实现:

  • 替换默认菜单项:在customizeMenuOptions中先移除原有Lines菜单项,再添加自定义菜单项并绑定你的导出逻辑。
  • 重载上下文方法区分处理:如果仅需修改导出的过滤条件、字段组等,可在getDataEntityContext中通过菜单项ID判断,执行自定义逻辑:
public ExportToExcelDataEntityContext getDataEntityContext(OfficeGeneratedExportMenuItem _menuItem)
{
    ExportToExcelDataEntityContext context;

    if (_menuItem.menuItemId() == CustomExportLinesToExcelOptionId)
    {
        // 自定义导出逻辑:指定字段组、添加过滤条件等
        context = ExportToExcelDataEntityContext::construct(tableStr(MyEntity), tableFieldGroupStr(MyEntity, YourCustomFieldGroup));
        context.addEntityDefault(tableStr(MyEntity));
        
        ListEnumerator enumerator = context.entities().getEnumerator();
        while (enumerator.moveNext())
        {
            ExportToExcelDataEntityInfo entity = enumerator.current();
            if (entity.entityName() == tableStr(MyEntity))
            {
                ExportToExcelFilterTreeBuilder filterBuilder = new ExportToExcelFilterTreeBuilder(tableStr(MyEntity));
                FilterNode filterExpression = filterBuilder.areEqual(fieldStr(MyEntity, dataAreaId), curExt());               
                entity.filter(filterExpression);
                // 可在此添加更多自定义配置,比如指定导出字段、排序规则等
            }
        }
    }
    else
    {
        // 保留系统默认导出逻辑(如有需要)
        context = super(_menuItem);
    }

    return context;
}

注意:确保CustomExportLinesToExcelOptionId已在SysOfficeGeneratedExportMenuItemOptionId枚举中正确定义,且表单的Office Integration属性配置无误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 14:13:26