From 1a52430f4d198fd464bc61f38e5f1719bc853dcd Mon Sep 17 00:00:00 2001 From: richie Date: Sat, 31 Aug 2019 09:51:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=85=AC=E5=BC=8FAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/fanruan/api/cal/FormulaKit.java | 12 ++++++++++++ .../java/com/fanruan/api/cal/FormulaKitTest.java | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/main/java/com/fanruan/api/cal/FormulaKit.java b/src/main/java/com/fanruan/api/cal/FormulaKit.java index 10bb4fa..93cd52a 100644 --- a/src/main/java/com/fanruan/api/cal/FormulaKit.java +++ b/src/main/java/com/fanruan/api/cal/FormulaKit.java @@ -1,7 +1,9 @@ package com.fanruan.api.cal; import com.fanruan.api.err.KitError; +import com.fanruan.api.util.GeneralKit; import com.fr.base.BaseFormula; +import com.fr.base.ScriptFormula; import com.fr.log.FineLoggerFactory; import com.fr.script.Calculator; import com.fr.stable.FormulaProvider; @@ -58,6 +60,16 @@ public class FormulaKit { return BaseFormula.createFormulaBuilder().build(content); } + /** + * 生成脚本公式对象 + * + * @param content 公式的内容,是一段javascript脚本 + * @return 脚本公式 + */ + public @NotNull static BaseFormula newScriptFormula(Object content) { + return new ScriptFormula(GeneralKit.objectToString(content)); + } + /** * 检查公式内容合法性 * diff --git a/src/test/java/com/fanruan/api/cal/FormulaKitTest.java b/src/test/java/com/fanruan/api/cal/FormulaKitTest.java index c54ec7f..abed6cb 100644 --- a/src/test/java/com/fanruan/api/cal/FormulaKitTest.java +++ b/src/test/java/com/fanruan/api/cal/FormulaKitTest.java @@ -2,9 +2,11 @@ package com.fanruan.api.cal; import com.fanruan.api.Prepare; import com.fanruan.api.err.KitError; +import com.fr.base.BaseFormula; import com.fr.base.Formula; import com.fr.base.ParameterMapNameSpace; import com.fr.script.Calculator; +import com.fr.stable.UtilEvalError; import com.fr.stable.script.CalculatorProvider; import org.junit.Assert; import org.junit.Test; @@ -43,6 +45,19 @@ public class FormulaKitTest extends Prepare { Assert.fail(); } } + + @Test + public void testScriptFormula() { + BaseFormula formula = FormulaKit.newScriptFormula("=return Math.abs(-1)"); + CalculatorProvider provider = CalculatorKit.createCalculator(); + try { + Object r = formula.evalValue(provider); + Assert.assertEquals(1, r); + } catch (Exception e) { + e.printStackTrace(); + } + } + @Test public void testCheckFormulaContent() { assertEquals(true, FormulaKit.checkFormulaContent(new Formula(""))); From ef1ab0edaa99e0e3a87be2dfe5051523dbcfec0c Mon Sep 17 00:00:00 2001 From: richie Date: Sat, 31 Aug 2019 14:01:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=85=AC=E5=BC=8FAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fanruan/api/cal/CalculatorKit.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/fanruan/api/cal/CalculatorKit.java b/src/main/java/com/fanruan/api/cal/CalculatorKit.java index 4a370e5..5cdfb6b 100644 --- a/src/main/java/com/fanruan/api/cal/CalculatorKit.java +++ b/src/main/java/com/fanruan/api/cal/CalculatorKit.java @@ -5,24 +5,25 @@ import com.fanruan.api.session.SessionKit; import com.fr.base.ParameterMapNameSpace; import com.fr.base.TableDataNameSpace; import com.fr.data.TableDataSource; +import com.fr.report.core.namespace.SimpleCellValueNameSpace; import com.fr.report.report.Report; import com.fr.script.Calculator; import com.fr.stable.StringUtils; import com.fr.stable.script.CalculatorProvider; import com.fr.stable.script.NameSpace; import com.fr.stable.web.SessionProvider; -import org.jetbrains.annotations.Nullable; import com.fr.web.core.ReportSessionIDInfor; import com.fr.web.core.TemplateSessionIDInfo; import com.fr.web.session.SessionIDInfo; +import org.jetbrains.annotations.Nullable; import java.util.Map; /** * @author zack - * @date 2019/8/23 * @version 10.0 * 帆软算子工具类(主要用于公式计算) + * @date 2019/8/23 */ public class CalculatorKit { /** @@ -79,9 +80,19 @@ public class CalculatorKit { /** * 返回服务器数据集的算子空间(可以通过调用calculator.pushNameSpace()将算子空间塞进算子实例,从而支持服务器数据集相关的函数计算) - * @return 算子空间对象 + * + * @return 服务器数据集名字空间 */ - public static NameSpace getServerTableDataNameSpace(){ + public static NameSpace getServerTableDataNameSpace() { return TableDataNameSpace.getInstance(); } + + /** + * 用于计算单元格的名字空间 + * + * @return 单元格名字空间 + */ + public static NameSpace createSimpleCellValueNameSpace() { + return SimpleCellValueNameSpace.getInstance(); + } } \ No newline at end of file