2013-04-12 6 views
11

Mac 용 Excel에서 웹 서비스에 대한 쿼리 문자열로 Get Get을 보내야합니다. QueryTables (How can I send an HTTP POST request to a server from Excel using VBA?)를 사용하여 답변을 보았지만 GET 메서드가 아닌 POST 메서드를 사용합니다. 나는 또한 Windows 컴퓨터에서 쉽게 볼 수 있지만 Mac에 붙어 있습니다.Mac 용 Excel VBA 2011에서 HTTP GET을 발급하는 방법

의견이 있으시면 언제든지 알려주십시오.

답변

18

더 많은 조사를 해본 결과,이 질문에 대한 Robert Knight의 의견이 VBA Shell function in Office 2011 for Mac이고 execShell 함수를 사용하여 curl을 호출하는 HTTPGet 함수를 만들었습니다. VBA를를 열고

Option Explicit 

' execShell() function courtesy of Robert Knight via StackOverflow 
' https://stackoverflow.com/questions/6136798/vba-shell-function-in-office-2011-for-mac 

Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long 
Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long 
Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long 
Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long 

Function execShell(command As String, Optional ByRef exitCode As Long) As String 
    Dim file As Long 
    file = popen(command, "r") 

    If file = 0 Then 
     Exit Function 
    End If 

    While feof(file) = 0 
     Dim chunk As String 
     Dim read As Long 
     chunk = Space(50) 
     read = fread(chunk, 1, Len(chunk) - 1, file) 
     If read > 0 Then 
      chunk = Left$(chunk, read) 
      execShell = execShell & chunk 
     End If 
    Wend 

    exitCode = pclose(file) 
End Function 

Function HTTPGet(sUrl As String, sQuery As String) As String 

    Dim sCmd As String 
    Dim sResult As String 
    Dim lExitCode As Long 

    sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl 
    sResult = execShell(sCmd, lExitCode) 

    ' ToDo check lExitCode 

    HTTPGet = sResult 

End Function  

는, 이것을 사용 위의 코드를 복사하려면 : 나는 맥 2011 여기에 VBA 코드가 위해 Excel에서 맥 OS X 10.8.3 (산 사자)를 실행하는 Mac에서이 테스트를했습니다 편집기가 필요합니다. 모듈이없는 경우 삽입 -> 모듈을 클릭하십시오. 코드를 모듈 파일에 붙여 넣습니다. VBA 편집기 (clover-Q)를 닫습니다.

다음은 일기 예보 웹 서비스 (http://openweathermap.org/wiki/API/JSON_API)

셀 A1이 도시의 이름으로 예약됩니다를 사용하여 특정 예입니다. http://api.openweathermap.org/data/2.1/forecast/city 쿼리 문자열을 구축 할 것입니다 셀 A3에서

입력 :

셀 A2에서

, URL 문자열 입력 ="q=" & A1

을 A4 셀에 다음을 입력하십시오 =HTTPGet(A2, A3) 이제

A 형을 도시 이름 (예 : London)을 입력하면 셀 A4에 런던의 일기 예보가 포함 된 JSON 응답이 표시됩니다. A1의 값을 London에서 Moscow으로 변경하십시오. A4가 JSON 형식의 모스크바 예측으로 변경됩니다.

분명히 VBA를 사용하여 JSON 데이터를 구문 분석하고 다시 포맷하고 필요한 경우 워크 시트에 배치 할 수 있습니다.

성능이나 확장성에 대한 클레임은 없지만 Mac 2011 용 Excel에서 웹 서비스에 대한 간단한 원할 액세스는이 트릭을 수행 한 것으로 보이며 원래 질문을 올렸습니다. YMMV!

0

또 다른 옵션 (당신의 컬이/옵션/지방/빈/컬에없는 경우에 따라 업데이트) :

VBA :

Public Function getUrlContents(url) As String 
    Dim command As String 
    command = "do shell script ""/path_to/getUrl.sh " + url + """" 
    getUrlContents = VBA.MacScript(command) 
End Function 

/path_to/getUrl.sh :

#!/bin/sh 

if [ -z "$1" ] 
    then 
    echo "missing url argument" 
else 
    /opt/local/bin/curl "$1" 
fi 
당신이 getUrl.sh이 실행 있는지 확인해야 할 것

참고 :

chmod u+x getUrl.sh 
1

위의 답변은 환상적입니다. (부탁합니다.)하지만 더 최근의 엑셀 : mac 2016에서 더 이상 작동하지 않습니다. 64 비트 시스템에서 사용하기 위해 코드를 업데이트해야한다는 오류가 있습니다. .

an issue I found in a related repository에서 몇 가지 팁을 촬영, 나는 Excel에서 제대로 작동하려면 요한의 스크립트에서 데이터 유형을 조정 할 수 있었다 : 맥 2016 :

Option Explicit 

' execShell() function courtesy of Robert Knight via StackOverflow 
' http://stackoverflow.com/questions/6136798/vba-shell-function-in-office-2011-for-mac 

Private Declare PtrSafe Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As LongPtr 
Private Declare PtrSafe Function pclose Lib "libc.dylib" (ByVal file As LongPtr) As Long 
Private Declare PtrSafe Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As LongPtr, ByVal items As LongPtr, ByVal stream As LongPtr) As Long 
Private Declare PtrSafe Function feof Lib "libc.dylib" (ByVal file As LongPtr) As LongPtr 

Function execShell(command As String, Optional ByRef exitCode As Long) As String 
    Dim file As LongPtr 
    file = popen(command, "r") 

    If file = 0 Then 
     Exit Function 
    End If 

    While feof(file) = 0 
     Dim chunk As String 
     Dim read As Long 
     chunk = Space(50) 
     read = fread(chunk, 1, Len(chunk) - 1, file) 
     If read > 0 Then 
      chunk = Left$(chunk, read) 
      execShell = execShell & chunk 
     End If 
    Wend 

    exitCode = pclose(file) 
End Function 

Function HTTPGet(sUrl As String, sQuery As String) As String 

    Dim sCmd As String 
    Dim sResult As String 
    Dim lExitCode As Long 

    sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl 
    sResult = execShell(sCmd, lExitCode) 

    ' ToDo check lExitCode 

    HTTPGet = sResult 

End Function