【套打】针对部分数据源取部分行数套打原创
金蝶云社区-_Tmp
_Tmp
10人赞赏了该文章 1618次浏览 未经作者许可,禁止转载编辑于2021年09月29日 21:08:42

<0>背景:某些子表体数据过多,客户想要显示前X行数据即可;

更新记录:套打2103切换为后台任务生成页面(进度条打印),导致插件无法访问Http请求问题,如果使用异常可下载新版的插件使用。(后续修复针对后台任务传入Http上下文)


<1>使用数据表格/简单数据表格过滤得到有效数字(字段非空),而后取前X行(插件实现)

表单插件C#代码:

image.png


代码调整:只需要调整三个位置,即装即用(python代码调整逻辑同理)

<1>TargetDataSourceKey——干预的目标实体

<2>TargetRowCnt——数据行数

<3>EnableTemplate——启用模板集合

image.png

Python插件:使用动态插件编译【工具分享】C#插件转Python工具,C#高效开发 + Python快速部署

image.png


<2>效果

image.png



附录C#代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Kingdee.BOS.Core.DynamicForm.PlugIn;

using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;

using Kingdee.BOS.Orm.DataEntity;

using Kingdee.BOS.Util;


namespace NotePrintPlugin.Sample

{

    [Kingdee.BOS.Util.HotUpdate]

    public class DisplayMaxRowCntSample : AbstractDynamicFormPlugIn

    {

        /*

         * 功能:当分录大于目标数目时仅显示前X个行

         */


        /// <summary>

        /// 目标数据源标识

        /// </summary>

        private static readonly string TargetDataSourceKey = "FEntity";


        /// <summary>

        /// 目标行数

        /// </summary>

        private static readonly  int TargetRowCnt = 5;


        /// <summary>

        /// 启用插件模板集合

        /// </summary>

        private static readonly HashSet<string> EnableTemplate = new HashSet<string>() {"c5fe55cb-b53b-4be7-8fbe-ffa8cde9254d"};


        public override void OnPrepareNotePrintData(PreparePrintDataEventArgs e)

        {

            if (!EnableTemplate.Contains(e.NotePrintTplId))

                return;

            if (!e.DataSourceId.EqualsIgnoreCase(TargetDataSourceKey))

                return;

            if (e.DataObjects == null || e.DataObjects.Length <= TargetRowCnt)

                return;

            List<DynamicObject> filterRows = new List<DynamicObject>();

            for (int curRowCnt = 0; curRowCnt < TargetRowCnt; ++curRowCnt)

            {

                filterRows.Add(e.DataObjects[curRowCnt]);

            }

            e.DataObjects = filterRows.ToArray();

        }

    }

}

附件Python代码:

code='''

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Kingdee.BOS.Core.DynamicForm.PlugIn;

using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;

using Kingdee.BOS.Orm.DataEntity;

using Kingdee.BOS.Util;


namespace NotePrintPlugin.Sample

{

    [Kingdee.BOS.Util.HotUpdate]

    public class DisplayMaxRowCntSample : AbstractDynamicFormPlugIn

    {

        /*

         * 功能:当分录大于目标数目时仅显示前X个行

         */


        /// <summary>

        /// 目标数据源标识

        /// </summary>

        private static readonly string TargetDataSourceKey = "FEntity";


        /// <summary>

        /// 目标行数

        /// </summary>

        private static readonly  int TargetRowCnt = 5;


        /// <summary>

        /// 启用插件模板集合

        /// </summary>

        private static readonly HashSet<string> EnableTemplate = new HashSet<string>() {"c5fe55cb-b53b-4be7-8fbe-ffa8cde9254d"};


        public override void OnPrepareNotePrintData(PreparePrintDataEventArgs e)

        {

            if (!EnableTemplate.Contains(e.NotePrintTplId))

                return;

            if (!e.DataSourceId.EqualsIgnoreCase(TargetDataSourceKey))

                return;

            if (e.DataObjects == null || e.DataObjects.Length <= TargetRowCnt)

                return;

            List<DynamicObject> filterRows = new List<DynamicObject>();

            for (int curRowCnt = 0; curRowCnt < TargetRowCnt; ++curRowCnt)

            {

                filterRows.Add(e.DataObjects[curRowCnt]);

            }

            e.DataObjects = filterRows.ToArray();

        }

    }

}

'''


refDlls = '''

Kingdee.BOS

Kingdee.BOS.App

Kingdee.BOS.App.Core

Kingdee.BOS.Business.Bill

Kingdee.BOS.Core

Kingdee.BOS.DataEntity

Kingdee.BOS.Model

NotePrintPlugin.Sample

System

System.Core

System.Data

System.Web

'''


clr.AddReference('System')

clr.AddReference('System.Web')

clr.AddReference('Kingdee.BOS')

from System import *

from System.IO import FileInfo

from System.Web import HttpContext

from System.Reflection import Assembly

from Microsoft.CSharp import CSharpCodeProvider

from System.CodeDom.Compiler import CompilerParameters

from Kingdee.BOS.Cache import KCacheManagerFactory

from System.Reflection import BindingFlags

def CompileCode(code, refDlls): #动态编译C#代码,放入缓存

    if(HttpContext.Current is  None):

        return;

    refAsmNames = filter(lambda y: y <> '', map(lambda x: x.strip(), refDlls.split()))

    kcmgr = KCacheManagerFactory.Instance.GetCacheManager('PyCodeGeneratorCache', 'PyCodeGeneratorCache')

    cacheKey = code + '-'.join(refAsmNames)

    if(kcmgr.Get(cacheKey) is not None):

        return kcmgr.Get(cacheKey)

    cSharpCodePrivoder = CSharpCodeProvider({'CompilerVersion':'v4.0'})

    codeCompiler = cSharpCodePrivoder.CreateCompiler()

    compilerParameters = CompilerParameters()

    compilerParameters.GenerateExecutable = False; 

    compilerParameters.GenerateInMemory = True; 

    for refAsmName in refAsmNames:

        asms = filter(lambda asm: asm.GetName().Name == refAsmName, AppDomain.CurrentDomain.GetAssemblies())

        if(len(asms) > 0):

            compilerParameters.ReferencedAssemblies.Add(FileInfo(asms[0].CodeBase.Substring(8)).DirectoryName.Replace('\\', '/')+'/' + refAsmName + '.dll')

        else:

            try:

                compilerParameters.ReferencedAssemblies.Add(FileInfo(Assembly.Load(refAsmName).CodeBase.Substring(8)).DirectoryName.Replace('\\', '/') + '/' + refAsmName + '.dll');

            except:

                pass

        compilerResults = codeCompiler.CompileAssemblyFromSource(compilerParameters, code);

    if (compilerResults.Errors.HasErrors):

        raise Exception('\r\n'.join(map(lambda err: err.ErrorText, compilerResults.Errors)))

    compiledAsm = compilerResults.CompiledAssembly;

    kcmgr.Put(cacheKey, compiledAsm)

    return compiledAsm


assembly = CompileCode(code, refDlls)

csPlugin = assembly.CreateInstance('NotePrintPlugin.Sample.DisplayMaxRowCntSample') if assembly is not None else None


def getPlugIn():

    csPlugin.SetContext(this.Context, this.View)

    return csPlugin


def OnPrepareNotePrintData(e):

    getPlugIn().OnPrepareNotePrintData(e)


赞 10