global type gf_dw2json from function_object
end type
forward prototypes
global function string gf_dw2json (powerobject dataobj_sur, string as_cols)
end prototypes
global function string gf_dw2json (powerobject dataobj_sur, string as_cols);//====================================================================
// Declare: gf_dw2json
//--------------------------------------------------------------------
// 描述: dw/ds按指定列導出json,結合vdn使用
//--------------------------------------------------------------------
// 參數:
// dataobj_sur :原 dw/ds
// as_cols :列名多個用逗號分開 如 col1,col2
//--------------------------------------------------------------------
// 返回:
//--------------------------------------------------------------------
// 作者: tom (QQ88954322) 日期: 2019/10/09 13:31:45
//--------------------------------------------------------------------
// CopyRight(c)
//--------------------------------------------------------------------
// 修改歷史:
//
//====================================================================
Long i = 0, llPos, llBegin = 1,ll_arrcnt,ll_rowcnt
string ls_colarry[],asTag=',',ls_Ret,ls_type[],ls_json
String ls_cols[],ls_colsType[]
Int li_colsSize[]
datawindow ldw_sur
datastore lds_sur,lds_tmp
dwobject dwo_sur,dwo_tar
//取窗口數據類型
Choose Case dataobj_sur.TypeOf()
Case datastore!
lds_sur = dataobj_sur
ll_rowcnt=lds_sur.rowcount()
Case datawindow!
ldw_sur = dataobj_sur
ll_rowcnt=ldw_sur.rowcount()
Case Else
SignalError(221,"-2,Parameter DataObj type error")
return ''
End Choose
if ll_rowcnt=0 then
SignalError(221,"-1,無數據")
return ''
end if
//取列名
as_cols=trim(as_cols)
if right(as_cols,1)=',' then as_cols=mid(as_cols,1,len(as_cols) - 1)
llPos = Pos(as_cols, asTag, llBegin)
Do While llPos > 0
i ++
ls_colarry[i] = Mid(as_cols, llBegin, llPos - llBegin)
llBegin = llPos + Len(asTag)
llPos = Pos(as_cols, asTag, llBegin)
Loop
If llBegin <= Len(as_cols) Or i > 0 Then
i ++
ls_colarry[i] = Mid(as_cols, llBegin, Len(as_cols) - llBegin + 1)
End If
ll_arrcnt=i
//創建導出窗口
for i=1 to ll_arrcnt
if dataobj_sur.TypeOf()=datawindow! then
ls_Ret =ldw_sur.Describe(ls_colarry[i]+".width")
else
ls_Ret =lds_sur.Describe(ls_colarry[i]+".width")
end if
if ls_Ret="!" then
SignalError(221,'-3,源窗口沒有列'+ls_colarry[i])
return ''
end if
if dataobj_sur.TypeOf()=datawindow! then
ls_type[i]=ldw_sur.Dynamic Describe(ls_colarry[i]+".Coltype")
else
ls_type[i]=lds_sur.Dynamic Describe(ls_colarry[i]+".Coltype")
end if
li_colsSize[i]=long(mid(ls_type[i],pos(ls_type[i],'(') + 1,pos(ls_type[i],')') - pos(ls_type[i],'(') - 1))
ls_colsType[i]=left(ls_type[i],pos(ls_type[i],'(') - 1)
if ls_colsType[i]='char' then ls_colsType[i]='string'
next
ls_cols= ls_colarry
guo_vdn.createdw(ls_cols, ls_colsType, li_colsSize, lds_tmp)
//複製數據
for i=1 to ll_rowcnt
lds_tmp.insertrow(0)
next
for i=1 to ll_arrcnt
if dataobj_sur.TypeOf()=datawindow! then
dwo_sur=ldw_sur.Object.__get_attribute(ls_colarry[i],False)
else
dwo_sur=lds_sur.Object.__get_attribute(ls_colarry[i],False)
end if
dwo_tar=lds_tmp.Object.__get_attribute(ls_colarry[i],False)
dwo_tar.Current=dwo_sur.Current
next
//導出
nvo_json json
json=create nvo_json
ls_json=json.dw2json(lds_tmp)
destroy json
return ls_json
end function