From 31571b723064eedf00a7b30fe255314ec6e6909f Mon Sep 17 00:00:00 2001 From: Shlomo Morosow Date: Wed, 29 Jun 2022 21:34:13 +0300 Subject: [PATCH] Allow to dynamically add custom functions at runtime. This is very helpful for scenarios where the custom function is very domain-specific and does not make sense to add to the Luckysheet codebase. Developer will have to be cognizant of locals and needs to handle providing the appropriate text based on the current locale. --- src/core.js | 3 ++- src/function/functionlist.js | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index 8e28ba6..6230683 100644 --- a/src/core.js +++ b/src/core.js @@ -126,6 +126,7 @@ luckysheet.create = function (setting) { luckysheetConfigsetting.workbookCreateBefore = extendsetting.workbookCreateBefore; luckysheetConfigsetting.workbookCreateAfter = extendsetting.workbookCreateAfter; luckysheetConfigsetting.remoteFunction = extendsetting.remoteFunction; + luckysheetConfigsetting.customFunctions = extendsetting.customFunctions; luckysheetConfigsetting.fireMousedown = extendsetting.fireMousedown; luckysheetConfigsetting.forceCalculation = extendsetting.forceCalculation; @@ -156,7 +157,7 @@ luckysheet.create = function (setting) { initPlugins(extendsetting.plugins , extendsetting.data); // Store formula information, including internationalization - functionlist(); + functionlist(extendsetting.customFunctions); let devicePixelRatio = extendsetting.devicePixelRatio; if(devicePixelRatio == null){ diff --git a/src/function/functionlist.js b/src/function/functionlist.js index 15e1a17..59fa32c 100644 --- a/src/function/functionlist.js +++ b/src/function/functionlist.js @@ -3,10 +3,10 @@ import Store from '../store/index' import locale from '../locale/locale'; //{"0":"数学","1":"统计","2":"查找","3":"Luckysheet内置","4":"数据挖掘","5":"数据源","6":"日期","7":"过滤器","8":"财务","9":"工程计算","10":"逻辑","11":"运算符","12":"文本","13":"转换工具","14":"数组"} -const functionlist = function(){ +const functionlist = function(customFunctions){ let _locale = locale(); // internationalization,get function list - let functionListOrigin = _locale.functionlist; + let functionListOrigin = [..._locale.functionlist]; // add new property f for (let i = 0; i < functionListOrigin.length; i++) { @@ -14,6 +14,10 @@ const functionlist = function(){ func.f = functionImplementation[func.n]; } + if (customFunctions) { + functionListOrigin.push(...customFunctions); + } + Store.functionlist = functionListOrigin; // get n property @@ -29,4 +33,4 @@ const functionlist = function(){ Store.luckysheet_function = luckysheet_function; } -export default functionlist; \ No newline at end of file +export default functionlist;