为自己的软件增加脚本编写功能并自定义API
CBScript 自定义脚本语言简介
实现自定义关键字
1、执行While循环语句
string str =
@"//定义变量
var a=0
//设置标签
WHILE a<5a=a+1
ENDWHILE
";DataExcel grid = Grid;FunctionBody script = ScriptBuilder.BuilderScript(grid, grid.FocusedCell);object result = Feng.Utils.ConvertHelper.ToInt32(script.Exec(str));object target = 5;Assert.AreEqual(target, result);
string str = @"//定义ID
var i=0;
i=1;
var t=0;
while i<100if \
i%5==1t=t+i;endifi=i+1;
endwhile
return t;
";DataExcel grid = Grid;FunctionBody script = ScriptBuilder.BuilderScript(grid, grid.FocusedCell);object obj = script.Exec(str);var i = 0;i = 1;var t = 0;while (i < 100){ if (i % 5 == 1){t = t + i;}i = i + 1;}int target = t;string type = obj.GetType().FullName;int result = Feng.Utils.ConvertHelper.ToInt32(obj);Assert.AreEqual(target, result);
2、执行FOREACH语句
string str =
@"//定义变量 LIST 通过LISTNEW函数初为始为集合变量
VAR LIST=LISTNEW(1,2,3,4,5,6,7)
//设置标签
FOREACH A IN LISTIF A>5RETURN AENDIF
ENDFOREACH
";DataExcel grid = Grid;FunctionBody script = ScriptBuilder.BuilderScript(grid, grid.FocusedCell);object result = Feng.Utils.ConvertHelper.ToInt32(script.Exec(str));object target = 6;Assert.AreEqual(target, result);
3、执行IF语句 CBScript.Exec
string str =
@"//Return
VAR A=3;
IF A<2RETURN A;
ELSE 3<3RETURN 2
ELSE RETURN 3
ENDIF
";decimal target = 3;int result = Feng.Utils.ConvertHelper.ToInt32(Feng.Script.CBEexpress.CBScript.Exec(str));Assert.AreEqual(target, result);
4、执行FOR语句
string str =
@"//
var sum=0;
var i=0;
for i=0;i<10;i=i+1;if i%5==1 sum=sum+i; endif
endfor
return sum;
";string target = "7";string result = "";DataExcel grid = Grid;FunctionBody script = ScriptBuilder.BuilderScript(grid, grid.FocusedCell);script.StateMentFactory = new StateMentFactory();object value = script.Exec(str);result = Feng.Utils.ConvertHelper.ToString(value);Assert.AreEqual(target, result);
5、定义变量
string str =
@"//
VAR A=0
A=A+1;
RETURN A;
";DataExcel grid = Grid;FunctionBody script = ScriptBuilder.BuilderScript(grid, grid.FocusedCell);int target = 1; script.StateMentFactory = new StateMentFactory();int result = Feng.Utils.ConvertHelper.ToInt32(script.Exec(str));Assert.AreEqual(target, result);
6、在脚本语句中执行自定义脚本
string str =
@"//
VAR A=0
WHILE A<100VAR B=100A=ScriptExec(SCRIPT,""C"",A)B=A+B
ENDWHILE
RETURN B
";string script2 =
@"//
IF C<100 C=C+1
ENDIF
RETURN C
";DataExcel grid = Grid;FunctionBody script = ScriptBuilder.BuilderScript(grid, grid.FocusedCell);script.Script.ExecProxy.SetKeyValue("SCRIPT", script2);int target = 100;grid[2, 2].Value = target;grid[2, 2].ID = "INDEX";script.StateMentFactory = new StateMentFactory();int result = Feng.Utils.ConvertHelper.ToInt32(script.Exec(str)) - 100;Assert.AreEqual(target, result);
7、执行复杂表达式
string str = @"(0>9)&&(1<=2)&&2==5";bool value = (0 > 9) && (1 <= 2) && 2 == 5;bool result = Feng.Utils.ConvertHelper.ToBoolean(Feng.Script.CBEexpress.CBScript.Exec(str));Assert.IsTrue(value == result, "无法验证不返回值的方法。");
string str = @"9 - 0 - 9 * 8 - 7 ^ 5 ^ 2 % 2";int value = 9 - 0 - 9 * 8 - 7 ^ 5 ^ 2 % 2;int result = Feng.Utils.ConvertHelper.ToInt32(Feng.Script.CBEexpress.CBScript.Exec(str));Feng.Utils.TraceHelper.WriteTrace("", "", "", true, value.ToString());Assert.IsTrue(value == result, "无法验证不返回值的方法。");
string str = @"(0>9)||(1<=2)||2==5";bool value = (0 > 9) || (1 <= 2) || 2 == 5;bool result = Feng.Utils.ConvertHelper.ToBoolean(Feng.Script.CBEexpress.CBScript.Exec(str));Assert.IsTrue(value == result, "无法验证不返回值的方法。");
8、执行JSON函数
string text = @"{""KEY"":[1,""123"",3,false,true,null]}";FunctionBody function = new FunctionBody();ExcuteProxy excuteProxy = new ExcuteProxy();CBMethod cbmethod = new CBMethod();JsonFunctionContainer methodContainer = new JsonFunctionContainer();cbmethod.Methods.Add(methodContainer);excuteProxy.RunMethod = cbmethod;function.Script.ExecProxy = excuteProxy;string jsontext = "\"" + Feng.Utils.TextHelper.Trope(text) + "\"";string str = @"var json=jsonparse(" + jsontext + @");
var obj=jsonget(json,""key"");
obj=jsonget(obj,5);
return obj;
";object value = function.Exec(str);object target = null;Assert.AreEqual(target, value);
string text = @"{""KEY"":[1,""123"",3,false,true,null]}";FunctionBody function = new FunctionBody();ExcuteProxy excuteProxy = new ExcuteProxy();CBMethod cbmethod = new CBMethod();JsonFunctionContainer methodContainer = new JsonFunctionContainer();//添加json函数cbmethod.Methods.Add(methodContainer);excuteProxy.RunMethod = cbmethod;function.Script.ExecProxy = excuteProxy;string jsontext = "\"" + Feng.Utils.TextHelper.Trope(text) + "\"";string str = @"var json=jsonparse(" + jsontext + @");
var obj=jsonget(json,""key"");
obj=jsonget(obj,2);
return obj;
";object value = function.Exec(str);decimal target = 3;Assert.AreEqual(target, value);
string text = @"{""KEY"":[1,""123"",3,false,true,null]}";Feng.Json.FJsonBase obj = Feng.Json.FJsonParse.Parese(text);object taget = "123";object res = obj["KEY"][1].Value;Assert.AreEqual(taget, res);
string text = @"{""KEY"":[1,""123"",3,false,true,null]}";Feng.Json.FJsonBase obj = Feng.Json.FJsonParse.Parese(text);object taget = "123";object res = obj["KEY"][1].Value;Assert.AreEqual(taget, res);
9、执行DataTable相关函数
object target = "100";object result = null;DataExcel grid = Grid(target);FunctionBody script = ScriptBuilder.BuilderScript(grid, grid["d2"]);script.Script.ExecProxy.SetKeyValue("TARGET", target);string str = @"
VAR LIST=DataTableNew(""COLUMN1"",""COLUMN2"",""COLUMN3"",""COLUMN4"",""COLUMN5"",""COLUMN6"",""COLUMN7"")DataTableAdd(list,1,2,3,3,4,5);DataTableAdd(list,""a"",""b"",""c"",""d"",""e"",""f"");DataTableAdd(list,""a"",""b"",""c"",""d"",""e"",""f"");DataTableAdd(list,""a"",""b"",""c"",""d"",""e"",""f"");DataTableAdd(list,""a"",""b"",""c"",""d"",""e"",""f"");DataTableAdd(list,""a"",""b"",""c"",""d"",""e"",""f"");DataTableAdd(list,""a"",""b"",""c"",""d"",""e"",""f"");DataTableIndex(list,5,1,100); DataTableAddColumn(list,""COLUMN8"");var row=DataTableRow(LIST,4);DataTableRowRemove(list,row);row=DataTableRow(LIST,4);
RETURN DataTableRowValue(row,""COLUMN2"")
";object value = script.Exec(str);result = value;Assert.AreEqual(target, result);
10、执行数据库操作函数
string conection = @"server=.\ZSQL2016;database=test;user=sa;password=sql2016;";FunctionBody function = new FunctionBody();ExcuteProxy excuteProxy = new ExcuteProxy();CBMethod cbmethod = new CBMethod();CBMethodContainer methodContainer = new JsonFunctionContainer();cbmethod.Methods.Add(methodContainer);methodContainer = new SqlServerFunctionContainer();cbmethod.Methods.Add(methodContainer);methodContainer = new ListFunctionContainer();cbmethod.Methods.Add(methodContainer);excuteProxy.RunMethod = cbmethod;function.Script.ExecProxy = excuteProxy;System.Threading.Thread.Sleep(20);string sql = @"SELECT top 100 [ID],[NO],[NAME],[ADDRESS],[PRICE],[COUNT],[BIRTHDAY],
[SEX],[REMARK],[STATE],[STATENAME],[CTIME],[CUSERID],[CUSERNAME],[UTIME],[UUSERID],[UUSERNAME]
FROM[dbo].[table1] WHERE ID like '%'+@ARG1+'%'";string str = @"SqlFillArgs(""" + conection + @""",""" + sql + @""",""aa"")";object value = function.Exec(str);DataTable hash = value as DataTable;object target = 124;object result = Feng.Utils.ConvertHelper.ToInt32(hash.Rows[1]["COUNT"]);Assert.AreEqual(target, result);