过去几天,我一直在尝试从Web服务中获取api数据。我无法以更有效的方式进行操作,我也无法从请求中获得帮助。尝试了很长时间后,我设法通过录制宏从Excel Power Query(2016年版)中获取了它。但是,我有两个问题需要解决,需要帮助。
我已在范围D1(1152843)中保存了唯一ID,并在范围D2中保存了api键(rrWedCKeclf1GqiY0pCBy05mdd)。我已经记录了一个宏,并希望在代码中提供单元格值,以便每当更改唯一ID或api键时,都不必在代码中进行更改。我尝试了range(d1).value
,但是语法太长了,引号太多了,我感到很困惑。请帮我编辑该行。
记录的宏非常大,需要删除一些不必要的内容,并且在需要水平滚动以读取整行的地方也很长。像我这样的初学者很难理解这种布局。请帮助我对齐代码,使其可以为可读格式。
PS:请运行代码,查看结果并建议是否有更好的方法来获取相同的结果。感谢我能得到一些建议。
谢谢
Public apiKey As String
Public matchID As String
Sub Macro5()
Range("$A$4").Select
ActiveWorkbook.Queries.Add Name:="Teams", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Json.Document(Web.Contents(""https://cricapi.com/api/fantasySummary?apikey=" & "rrWedCKeclf1GqiY0pCBy05mdd" & "&unique_id=" & "1152843""))," & Chr(13) & "" & Chr(10) & " data = Source[data]," & Chr(13) & "" & Chr(10) & " team = data[team]," & Chr(13) & "" & Chr(10) & " #""Converted to Table"" = Table.FromList(team, Splitter.SplitByNothing(), null, null, ExtraValues.Ignore)," & Chr(13) & "" & Chr(10) & " #""Renamed Columns"" = Table.RenameColumns(#""Converted " & _
"to Table"",{{""Column1"", ""Teams""}})," & Chr(13) & "" & Chr(10) & " #""Expanded Teams"" = Table.ExpandRecordColumn(#""Renamed Columns"", ""Teams"", {""players"", ""name""}, {""Teams.players"", ""Teams.name""})," & Chr(13) & "" & Chr(10) & " #""Expanded Teams.players"" = Table.ExpandListColumn(#""Expanded Teams"", ""Teams.players"")," & Chr(13) & "" & Chr(10) & " #""Expanded Teams.players1"" = Table.ExpandRecordColumn(#""Expanded Teams.pl" & _
"ayers"", ""Teams.players"", {""name"", ""pid""}, {""Teams.players.name"", ""Teams.players.pid""})," & Chr(13) & "" & Chr(10) & " #""Reordered Columns"" = Table.ReorderColumns(#""Expanded Teams.players1"",{""Teams.name"", ""Teams.players.pid"", ""Teams.players.name""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Reordered Columns"""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Teams;Extended Properties=""""" _
, Destination:=Range("$A$4")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Teams]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Teams"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
ActiveSheet.ListObjects("Teams").TableStyle = ""
Range("$A$4").Select
End Sub