2019-02-13

VBA - HttpRequest: Get Data from google sheet

1. Google sheet에 표와 같은 데이터가 있다.

타임스탬프금칙어표준어담당자성명
2018. 10. 24 오전 10:41:29stainless steel스테인리스강최치원
2018. 11. 13 오전 9:58:14스테인리스스틸연결유니언스테인리스유니언박나래
2018. 11. 13 오전 10:07:24스테인리스스틸앵글밸브스테인리스앵글밸브박나래
2018-09-05 17:51:00특수아크릴(인장강동특수아크릴(인장강도최치원
2018. 11. 13 오전 9:58:42스테인리스스틸유니언스테인리스유니언박나래
2018. 11. 20 오후 5:39:45알루미늄 - D.C알루미늄다이캐스팅김치국

2. 구글시트 데이터를 엑셀에서 읽어와서 작업 및 가공을 하려 한다.

3. 코드
Function webservice()
    ''First, Add a reference to MSXML (Tools > references)
    Dim req As Object
    Dim sheetUrl As String
    sheetUrl = "https://docs.google.com/spreadsheets/d/e/(Your-Google-Sheet-ID)/pub?gid=(Your-gid)&single=true&output=csv"
    
    Set req = CreateObject("MSXML2.XMLHTTP")
    With req
        .Open "GET", sheetUrl, False
        .Send
    End With
    Dim response As String
    response = req.ResponseText
    ''Debug.Print response
    Dim table() As String
    table() = Split(response, vbCrLf)
    Debug.Print LBound(table) & ":" & UBound(table) ''0~128..count=129
    ''
'    Dim 타임스탬프, 금칙어, 표준어, 담당자 As String
'    Dim row() As String
'    For Each wordrow In table
'        ''Debug.Print wordrow
'        row() = Split(wordrow, ",")
'        타임스탬프 = row(0)
'        금칙어 = row(1)
'        표준어 = row(2)
'        담당자 = row(3)
'    Next
    Set req = Nothing
    webservice = table
End Function

※ MSXML을 도구/참조에 등록해야 XMLHTTP Request를 사용할 수 있습니다.





 

No comments:

Post a Comment