Wednesday, June 23, 2010

calling a web service from Excel

Recently I evaluated the possibility to call a web service from Excel. The call is encapsulated within a function which can be used as a formula from within Excel.

Here ist the code:

Option Explicit
Option Base 0

Private wsClientGmd As SoapClient30

Public Function PredictSingle(ri As Single, spectrum As String, MiningModelId As String, Optional property As Variant) As Variant
    On Error GoTo soapError
    If wsClientGmd Is Nothing Then
        Set wsClientGmd = New SoapClient30
        wsClientGmd.MSSoapInit par_Wsdlfile:="http://gmd.mpimp-golm.mpg.de/webservices/wsPrediction.asmx?WSDL", par_Port:="wsPredictionSoap", par_Servicename:="wsPrediction"
        wsClientGmd.ConnectorProperty("ProxyServer") = "<CURRENT_USER>"
        wsClientGmd.ConnectorProperty("EnableAutoProxy") = True
    End If
    
    Dim res As IXMLDOMNodeList
    Set res = wsClientGmd.PredictSingle(ri, spectrum, MiningModelId)
    
    Dim xmlDoc As DOMDocument
    Set xmlDoc = res(1).OwnerDocument
    xmlDoc.setProperty "SelectionNamespaces", "xmlns:gmd='http://gmd.mpimp-golm.mpg.de/FunctionalGroupPrediction/'"
    xmlDoc.setProperty "SelectionLanguage", "XPath"
    
    If IsMissing(property) Or Len(CStr(property)) = 0 Then
        property = "PredictionGroupIsPresent"
    End If
    
    On Error Resume Next
    Dim v As String
    'v = xmlDoc.SelectSingleNode("//gmd:PredictionGroupIsPresent").FirstChild.Text
    v = xmlDoc.SelectSingleNode("//gmd:" & property).FirstChild.Text
        
    'only some of th properties
    Select Case property
        Case "PredictionGroupIsPresent"
            PredictSingle = IIf(v = "true", True, False)
        Case "Probability"
            PredictSingle = CSng(v)
        Case "AdjustedProbability"
            PredictSingle = CSng(v)
        Case "Support"
            PredictSingle = CLng(v)
        Case Else    'ERROR!!!!
            PredictSingle = "property '" & property & "' is unknown!"
    End Select
    GoTo Ende
soapError:
    PredictSingle = Err.Description
Ende:
End Function
 
Here is a screenshot how this function might be used in Excel (see cell E8):



In this case, the user can paste several mass spectra, assign the Mining models he/she is interested in and go and fetch a coffee.

No comments: