在 Excel 中致电 Google 财务 API
Call Google Finance API in Excel
我想通过 Google Finance API 在 Excel 中获取股票价格。
资源:https://finance.google.com/finance/info
方法:GET
参数:
- 客户=ig
- q =
<exchange code>:<stock code>
示例:
https://finance.google.com/finance/info?client=ig&q=TPE:2330
此请求将获取台积电的股票价格。
是否有解决方案,无论是做一些工作表的设置还是编写一些VBA代码,都能达到我的目标?
是的。这些方法是 XMLHTTPRequest,https://msdn.microsoft.com/en-us/library/ms759148%28v=vs.85%29.aspx for getting the response. And parsing JSON with VBA for parsing the response since it is JSON. Simplest method for parsing JSON with VBA is described here Parsing JSON in Excel VBA 但有更舒适的库可用搜索关键字 Excel VBA parse JSON
。
您的用例的简单示例:
Sub testGetJSON()
sExchangeCode = "TPE"
sStockCode = "2330"
sURL = "https://finance.google.com/finance/info?client=ig&q=" & sExchangeCode & ":" & sStockCode
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
oXMLHTTP.Open "GET", sURL, False
oXMLHTTP.send
sJSONResp = oXMLHTTP.responseText
sJSONResp = Mid(sJSONResp, 4, Len(sJSONResp))
Set oScript = CreateObject("ScriptControl")
oScript.Language = "jscript"
oScript.AddCode "function getProperty(jsonObj, propertyName) { return jsonObj[propertyName]; } "
Set oJSObj = oScript.eval("(" & sJSONResp & ")")
Set oProp0 = oScript.Run("getProperty", oJSObj, "0")
sLCur = oProp0.l_cur
sLT = oProp0.lt
MsgBox sLT & " : " & sLCur
End Sub
我想通过 Google Finance API 在 Excel 中获取股票价格。
资源:https://finance.google.com/finance/info
方法:GET
参数:
- 客户=ig
- q =
<exchange code>:<stock code>
示例:
https://finance.google.com/finance/info?client=ig&q=TPE:2330
此请求将获取台积电的股票价格。
是否有解决方案,无论是做一些工作表的设置还是编写一些VBA代码,都能达到我的目标?
是的。这些方法是 XMLHTTPRequest,https://msdn.microsoft.com/en-us/library/ms759148%28v=vs.85%29.aspx for getting the response. And parsing JSON with VBA for parsing the response since it is JSON. Simplest method for parsing JSON with VBA is described here Parsing JSON in Excel VBA 但有更舒适的库可用搜索关键字 Excel VBA parse JSON
。
您的用例的简单示例:
Sub testGetJSON()
sExchangeCode = "TPE"
sStockCode = "2330"
sURL = "https://finance.google.com/finance/info?client=ig&q=" & sExchangeCode & ":" & sStockCode
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
oXMLHTTP.Open "GET", sURL, False
oXMLHTTP.send
sJSONResp = oXMLHTTP.responseText
sJSONResp = Mid(sJSONResp, 4, Len(sJSONResp))
Set oScript = CreateObject("ScriptControl")
oScript.Language = "jscript"
oScript.AddCode "function getProperty(jsonObj, propertyName) { return jsonObj[propertyName]; } "
Set oJSObj = oScript.eval("(" & sJSONResp & ")")
Set oProp0 = oScript.Run("getProperty", oJSObj, "0")
sLCur = oProp0.l_cur
sLT = oProp0.lt
MsgBox sLT & " : " & sLCur
End Sub