No07.使用API获取SAP S/4 HANA Cloud中的凭证行项目(二)
使用API获取SAP S/4 HANA Cloud中的凭证行项目(二)
上述接口测试完成以后,在整个系统集成上具备了技术的可行性,因为日常工作用的比较多的就是Excel,在此处我们用VBA展示一下调用ODATA API的部分代码。
一 设置满足需求的URL字符串
,例如我们要查询2020-12-01到2020-12-31日会计科目为6001010000、6001020000、6001030000、6002010000、6002020000、6002030000的代码为1310公司,代码为1310F1001工厂的所有数据。因为字段比较多,我们只显示其中的部分字段。
下面展示一些 内联代码片
。
https://myXXXX.saps4hanacloud.cn/sap/opu/odata/sap/API_GLACCOUNTLINEITEM/GLAccountLineItem/?$filter= (GLAccount eq '6001010000' or GLAccount eq '6001020000' or GLAccount eq '6001030000'or GLAccount eq '6051010000' or GLAccount eq '6051020000' or GLAccount eq '6051030000') and Ledger eq '0L' and PostingDate ge datetime'2020-12-01T00:00:00' and PostingDate le datetime'2020-12-31T00:00:00' and ProfitCenter eq '1310F001' and CompanyCode eq '1310' &$orderby=PostingDate&$select=CompanyCode,GLAccount,ReferenceDocument,AccountingDocument,AccountingDocumentType,PostingDate,PostingKey,AmountInCompanyCodeCurrency,TaxCode,ClearingAccountingDocument,ProfitCenter,Segment,DocumentItemText,AccountingDocumentItem,OffsettingAccount,Product,AssignmentReference,SoldProductGroup,Customer
把上述URL复制到Postman中进行测试,测试通过成功,证明我们拼接的URL地址是正确的!注意其中过滤条件的写法。
&$filter=后面显示的是过滤条件,例如日期字段描述采用 PostingDate ge datetime‘2020-12-01T00:00:00’
指的是创建日期大于2020-12-01日. PostingDate le datetime‘2020-12-31T00:00:00’指的是创建日期小于2020-12-01日.
&$select=后面显示的是需要列示的字段,因为字段比较多,只选择需要的。
二 VBA中创建http请求
看代码
下面展示一些 内联代码片
。
Dim req As WinHttp.WinHttpRequest
Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
req.SetTimeouts 60000, 60000, 60000, 60000
req.Open "GET", url, False
req.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.SetRequestHeader "Username", "ODATAUSER"
req.SetRequestHeader "Password", "XXXXXXXXXX"
req.SetRequestHeader "Authorization", "Basic T0RBXXXXXXXXXXXXXXXXXXX="
req.SetRequestHeader "Accept", "application/json"
req.Send
getDataOfficial = req.ResponseText
三 使用正则表达式处理返回结果
'定义正则表达式,并清除其中的无用字符
Dim regexp As New regexp
Dim brr
With regexp
.Global = True
str1 = Trim(strJSON)
pt = """" & "([\s\S]+?)" & """" & ":" & """" & "([\s\S]*?)" & """"
.Pattern = pt
Set Match = .Execute(str1)
End With
四 小结
在接下来就是针对每条数据进行处理,包括字段的列表显示、修改显示的格式、把UNIX时间戳格式转为字符时间等。在此处就不再展示。
总之,利用Excel表中的VBA开发对接的接口,对于数据量不大的报表或业务单据的开发,开发速度和优势是明显的。
post SAP-Garson
原文链接:https://blog.csdn.net/m0_38101760/article/details/112139689文章来自于网络,如果侵犯了您的权益,请联系站长删除!