How to write to a “Google Spreadsheet” from Excel 2003 VBA

前端 未结 4 543
旧时难觅i
旧时难觅i 2020-12-05 08:59

I Have an Excel 2003 file with a line similar to this:

\"enter

<
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 09:27

    You don't need OAuth or the spreadsheet API. Google Spreadsheet allows data entry with a simple form, which means also that a HTTP POST will do the trick. You just need to prepare your spreadsheet to accept data entries via a form as follows:

    • Login to your Google Docs account
    • Create a spreadsheet or open an existing one
    • Click on Tools / Create a form
    • Add anything in the form description just to enable the Save button
    • Save the form
    • Copy the formkey value displayed in the link at the bottom of the form creation page
    • Now you can issue a simple post into the spreadsheet without OAuth

    You can test the entry now with curl if you have it on your system (replace the formkey placeholder with the formkey from your table):

    curl.exe -v -k "http://spreadsheets.google.com/formResponse?formkey=" -d "entry.0.single=test&entry.1.single=test2&pageNumber=0&backupCache=&submit=Submit"
    

    Next we try to execute the form POST from our Excel sheet via the following code. Add a reference to "Microsoft XML, v3.0" before. Replace column1 with your desired values.

    Dim httpRequest as XMLHTTP
    Set httpRequest = New XMLHTTP
    httpRequest.Open "POST", "http://spreadsheets.google.com/formResponse?formkey=&ifq", False
    httpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    httpRequest.Send "entry.0.single=" + column1 + "&entry.1.single=" + column2 + "&pageNumber=0&backupCache&submit=Submit"
    
    'Check result in the following vars
    httpRequest.status
    httpRequest.statusText
    

    Hope that helps

提交回复
热议问题