1. CHAR函数
函数可将计算机其他类型的数字代码转换为字符。
2. 函数用法
CHAR(number)
CHAR 函数可将计算机其他类型的数字代码转换为字符。
Number:用于指定字符的数字。
3. 函数示例
CHAR(10),即返回值为换行字符"\n"。
目前仿简道云公式函数实战仅支持以下数字代码:
Number | 字符 | 含义 |
9 | \t | 制表符tab idea控制台打印不出来 用postman测试可以返回 |
10 | \n | 换行字符 idea控制台打印不出来 用postman测试可以返回 |
34 | " | 双引号 |
39 | ’ | 单引号 |
92 | \ | 反斜杠 |
4. 代码实战
首先我们在function包下创建text包,在text包下创建CharFunction类,代码如下:
package com.ql.util.express.self.combat.function.text;import com.ql.util.express.Operator;
import com.ql.util.express.self.combat.exception.FormulaException;/*** 类描述: CHAR函数** @author admin* @version 1.0.0* @date 2023/11/24 11:18*/
public class CharFunction extends Operator {public CharFunction(String name) {this.name = name;}@Overridepublic Object executeInner(Object[] list) throws Exception {// 参数校验if (list.length == 0) {throw new FormulaException("操作数异常");}// 获取参数Object obj = list[0];char c;if (obj instanceof Number) {c = (char)((Number) obj).intValue();} else {throw new FormulaException("参数类型异常");}return c;}
}
把CharFunction类注册到公式函数入口类中,代码如下:
package com.ql.util.express.self.combat.ext;import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressResourceLoader;
import com.ql.util.express.parse.NodeTypeManager;
import com.ql.util.express.self.combat.function.logic.*;
import com.ql.util.express.self.combat.function.math.*;
import com.ql.util.express.self.combat.function.text.CharFunction;/*** 类描述: 仿简道云公式函数实战入口类** @author admin* @version 1.0.0* @date 2023/11/21 15:29*/
public class FormulaRunner extends ExpressRunner {public FormulaRunner() {super();}public FormulaRunner(boolean isPrecise, boolean isTrace) {super(isPrecise,isTrace);}public FormulaRunner(boolean isPrecise, boolean isStrace, NodeTypeManager nodeTypeManager) {super(isPrecise,isStrace,nodeTypeManager);}public FormulaRunner(boolean isPrecise, boolean isTrace, IExpressResourceLoader iExpressResourceLoader, NodeTypeManager nodeTypeManager) {super(isPrecise,isTrace,iExpressResourceLoader,nodeTypeManager);}@Overridepublic void addSystemFunctions() {// ExpressRunner 的内部系统函数super.addSystemFunctions();// 扩展公式函数this.customFunction();}/**** 自定义公式函数*/public void customFunction() {// 逻辑公式函数this.addLogicFunction();// 数学公式函数this.addMathFunction();// 文本函数this.addTextFunction();}public void addTextFunction() {// CHAR函数this.addFunction("CHAR",new CharFunction("CHAR"));}public void addLogicFunction() {// AND函数this.addFunction("AND",new AndFunction("AND"));// IF函数this.addFunction("IF",new IfFunction("IF"));// IFS函数this.addFunction("IFS",new IfsFunction("IFS"));// XOR函数this.addFunction("XOR",new XorFunction("XOR"));// TRUE函数this.addFunction("TRUE",new TrueFunction("TRUE"));// FALSE函数this.addFunction("FALSE",new FalseFunction("FALSE"));// NOT函数this.addFunction("NOT",new NotFunction("NOT"));// OR函数this.addFunction("OR",new OrFunction("OR"));}public void addMathFunction() {// ABS函数this.addFunction("ABS",new AbsFunction("ABS"));// AVERAGE函数this.addFunction("AVERAGE",new AvgFunction("AVERAGE"));// CEILING函数this.addFunction("CEILING",new CeilingFunction("CEILING"));// RADIANS函数this.addFunction("RADIANS",new RadiansFunction("RADIANS"));// COS函数this.addFunction("COS",new CosFunction("COS"));// COT函数this.addFunction("COT",new CotFunction("COT"));// COUNT函数this.addFunction("COUNT",new CountFunction("COUNT"));// COUNTIF函数this.addFunction("COUNTIF",new CountIfFunction("COUNTIF"));// FIXED函数this.addFunction("FIXED",new FixedFunction("FIXED"));// FLOOR函数this.addFunction("FLOOR",new FloorFunction("FLOOR"));// INT函数this.addFunction("INT",new IntFunction("INT"));// LARGE函数this.addFunction("LARGE",new LargeFunction("LARGE"));// LOG函数this.addFunction("LOG",new LogFunction("LOG"));// MAX函数this.addFunction("MAX",new MaxFunction("MAX"));// MIN函数this.addFunction("MIN",new MinFunction("MIN"));// MOD函数this.addFunction("MOD",new ModFunction("MOD"));// POWER函数this.addFunction("POWER",new PowerFunction("POWER"));// PRODUCT函数this.addFunction("PRODUCT",new ProductFunction("PRODUCT"));// RAND函数this.addFunction("RAND",new RandFunction("RAND"));// ROUND函数this.addFunction("ROUND",new RoundFunction("ROUND"));// SIN函数this.addFunction("SIN",new SinFunction("SIN"));// SMALL函数this.addFunction("SMALL",new SmallFunction("SMALL"));// SQRT函数this.addFunction("SQRT",new SqrtFunction("SQRT"));// SUM函数this.addFunction("SUM",new SumFunction("SUM"));// SUMIF函数this.addFunction("SUMIF",new SumIfFunction("SUMIF"));// SUMIFS函数this.addFunction("SUMIFS",new SumIfsFunction("SUMIFS"));// SUMPRODUCT函数this.addFunction("SUMPRODUCT",new SumProductFunction("SUMPRODUCT"));// TAN函数this.addFunction("TAN",new TanFunction("TAN"));}
}
创建测试用例
package com.ql.util.express.self.combat;import com.ql.util.express.DefaultContext;
import com.ql.util.express.self.combat.ext.FormulaRunner;
import org.junit.Test;/*** 类描述: 实战测试类** @author admin* @version 1.0.0* @date 2023/11/21 15:45*/
public class CombatTest {@Testpublic void CHAR() throws Exception{FormulaRunner formulaRunner = new FormulaRunner(true,true);// 创建上下文DefaultContext<String, Object> context = new DefaultContext<>();String express = "CHAR(92)";Object object = formulaRunner.execute(express, context, null, true, true);System.out.println(object);}}
运行结果