json - 从大数据库中将数据从 api 提取到 excel
问题描述
我使用一个API
来提取数据Excel
并JSON
解析以格式化数据。我有 2 个站点可以从中获取这些数据。1 是较小的数据库,有 3000 行,但另一个更大,有数十万行。使用较小的我没有错误,一切正常,但在更大的数据库上使用相同的代码,一切都会出现崩溃/永远挂起。我使用的代码肯定是非常错误的,有人告诉我我使用了太多的解析,但我是新手,所以现在无法真正弄清楚。
我正在使用的代码:
Option Explicit
Sub Times()
Dim sJSONString As String
Dim vJSON
Dim sState As String
Dim aData()
Dim aHeader()
Dim vResult
Dim sName
Dim authKey As String
authKey = "my_auth_key"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://my_site_url", True
.SetRequestHeader "Authorization", "Bearer " & authKey
.send
Do Until .readyState = 4: DoEvents: Loop
sJSONString = .responseText
End With
JSON.Parse sJSONString, vJSON, sState
If sState = "Error" Then
MsgBox "Invalid JSON"
End
End If
vResult = vJSON("data")
JSON.ToArray vResult, aData, aHeader
With ThisWorkbook.Sheets(1)
.Cells.Delete
.Cells.WrapText = False
OutputArray .Cells(1, 1), aHeader
Output2DArray .Cells(2, 1), aData
.Columns.AutoFit
End With
vJSON.Remove "data"
Application.DisplayAlerts = False
With ThisWorkbook.Sheets
Do Until .Count = 1
.Item(.Count).Delete
Loop
End With
Application.DisplayAlerts = True
For Each sName In vJSON
If IsArray(vJSON(sName)) Or IsObject(vJSON(sName)) Then
JSON.ToArray vJSON(sName), aData, aHeader
With ThisWorkbook.Sheets.Add
OutputArray .Cells(1, 1), aHeader
Output2DArray .Cells(2, 1), aData
.Columns.AutoFit
End With
vJSON.Remove sName
End If
Next
JSON.ToArray vJSON, aData, aHeader
JSON.Parse sJSONString, vJSON, sState
JSON.Flatten vJSON, vResult
JSON.ToArray vResult, aData, aHeader
MsgBox "Completed"
End Sub
Sub OutputArray(oDstRng As Range, aCells As Variant)
With oDstRng
.Parent.Select
With .Resize(1, UBound(aCells) - LBound(aCells) + 1)
.NumberFormat = "@"
.Value = aCells
End With
End With
End Sub
Sub Output2DArray(oDstRng As Range, aCells As Variant)
With oDstRng
.Parent.Select
With .Resize( _
UBound(aCells, 1) - LBound(aCells, 1) + 1, _
UBound(aCells, 2) - LBound(aCells, 2) + 1)
.NumberFormat = "@"
.Value = aCells
End With
End With
End Sub
也许通过简化和更正代码,我更大的数据库宏也可以工作。
解决方案
推荐阅读
- powershell - 安装 ktools.powershell.sftp
- c++ - 颤振,从c ++ opencv获取图像作为数据
- android - 将 RecyclerView 适配器的结果返回给 MainActvity.kt?
- c# - 为什么从 HttpClient.GetAsync 调用时,.Net 5 中的 Azure 函数 Http 触发器中缺少我的标头数据
- node.js - 如何用 es6(没有打字稿)开玩笑地模拟 Prisma 客户端?
- c++ - 四元数旋转忽略偏航
- git - Phabricator:HEAD 与 dc0f 分离
- javascript - Main.js 文件导致整个页面崩溃
- html - 右侧的下拉菜单
- reactjs - Expo AuthSession 避免要求您登录其他服务