找回密码
 立即注册

QQ登录

只需一步,快速开始

Derrick.Jiao 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2021-11-26 11:38  /   查看:1791  /  回复:0
本帖最后由 Derrick.Jiao 于 2021-11-26 11:43 编辑

有小伙伴想要在设计器的插入函数模板中添加自己的自定义函数。那么我们今天就来讲一下这部分应该如何实现。
image.png465754088.png


首先,我们先自定义一个函数,如何自定义函数可以参考学习指南这个demo
https://demo.grapecity.com.cn/sp ... om-functions/purejs
  1. function FactorialFunction() {
  2.                         this.name = "FACTORIAL";
  3.                         this.maxArgs = 1;
  4.                         this.minArgs = 1;
  5.                         this.description = function () {
  6.                                 return (
  7.                                         {
  8.                                                 description: "菲波那切数列",
  9.                                                 parameters: [
  10.                                                         {
  11.                                                                 name: 'number01',
  12.                                                                 repeatable: false,
  13.                                                                 optional: false
  14.                                                         }
  15.                                                 ]
  16.                                         }
  17.                                 )
  18.                         }
  19.                 }
  20.                 FactorialFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
  21.                 FactorialFunction.prototype.evaluate = function (arg) {
  22.                         var result = 1;
  23.                         if (arguments.length === 1 && !isNaN(parseInt(arg))) {
  24.                                 for (var i = 1; i <= arg; i++) {
  25.                                         result = i * result;
  26.                                 }
  27.                                 return result;
  28.                         }
  29.                         return "#VALUE!";
  30.                 };

  31.                 var factorial = new FactorialFunction();
  32.                 GC.Spread.CalcEngine.Functions.defineGlobalCustomFunction("FACTORIAL", new FactorialFunction());
  33.                
  34.                 spread.addCustomFunction(factorial)
复制代码

定义完这个函数后,我们需要找这个弹框对应的template。对应的是GC.Spread.Sheets.Designer.TemplateNames.InsertFunctionDialogTemplate。
image.png250490721.png

我们要添加函数的位置在这个数组中
image.png627873839.png

于是我们可以通过bindingPath找到这个位置,并且添加我们的自定义函数对象
  1. function customFontFamilyInFormatDialogTemplate(templateNode) {
  2.                         
  3.                         if (templateNode.bindingPath && templateNode.bindingPath === 'functionDesc.allFunction' && templateNode.items) {

  4.                                 templateNode.items.unshift({ text: "FACTORIAL", value: "FACTORIAL" })

  5.                                 return;
  6.                         }
  7.                         let nodes = templateNode.content || templateNode.children;
  8.                         if (nodes && nodes instanceof Array) {
  9.                                 nodes.forEach((subNode) => customFontFamilyInFormatDialogTemplate(subNode));
  10.                         }
  11.                 }
复制代码


最后把这个template注册回去
  1. GC.Spread.Sheets.Designer.registerTemplate(GC.Spread.Sheets.Designer.TemplateNames.InsertFunctionDialogTemplate, template);
复制代码

可以看到已经成功加上了 image.png643191527.png

下载附件即可查看完整demo

insert_formular.html

3.71 KB, 下载次数: 41

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部