华表CELL组件的应用技能提升( 二 )


strFun := '"天相函数" Double txGetDb(double n)';
Cell1.DefineFunctions(strFun);
End;
在Cell的自定义函数事件CalcFunc中定义函数的实现功能 。 CalcFunc事件根据表格中输入内容确认时立即触发的机制实现与开发环境的互动 。 而Cell内置函数对CalcFunc事件无效, 华表公司在这点上设计的非常合理 。
procedure TForm1.Cell1CalcFunc(Sender: TObject; const name: WideString;
rettype, paranum: Integer);
begin
if name=’ txInterFace’ then //执行界面操作的自定义函数
begin
Application.CreateForm(TForm2, Form2);
Form2.ShowModal;
Form2.Free;
Cell1.SetFuncResult(1, '调用窗体成功!', 1);
//由于函数定义返回类型为string, 所以实际返回‘调用窗体成功!’
end;
if name='txGetDb' then //执行数据库操作的自定义函数
with DataModal do
begin
if spTmp.Active then spTmp.Close;
// spTmp是ADOStoredProc的实例 。 返回从存储过程获取的数据
spTmp.ProcedureName:= 'PJC_SET_TRPT';
spTmp.Parameters.Refresh;
spTmp.Parameters.ParamValues['@XBH']:= '1010';
spTmp.Parameters.ParamByName('@RBH').Direction:= pdOutput;
spTmp.ExecProc;
Cell1.SetFuncResult(spTmp.Parameters.ParamValues['@RBH'], '', 0);
spTmp.Close;
end;
end;
下面是实际使用情况 。

华表CELL组件的应用技能提升

文章插图

在Cell表格中输入“=”号, 弹出公式框, 通过“?”按钮选择txInterFace函数或直接输入txInterFace函数, 最后选择“√” 按钮, 将立即触发CalcFunc事件中的txInterFace函数执行体, 弹出Form2窗体, 在Form2窗体你可以尽情地做自己的事啦 。
使用自定义函数的另一个好处就是别人没法拷走你的表格, 因为没有你的程序配合就无法实现表格独特的功能 。
总而言之, 我们已经利用Cell组件的强大功能解决了许多实际问题 。

推荐阅读