我的Excel在运行宏时变得无响应,它使用分页通过API从JIRA获取所有问题。

huangapple go评论69阅读模式
英文:

my excel becomes unresponsive while running a macro, it gets all the issues from JIRA using api using pagination

问题

Here's the translated code without the comments:

Sub Jira_for_all_issues_v6()
    Dim http As New MSXML2.XMLHTTP60, url As String, response As String, json As Object, startAt As Integer, batchSize As Integer, totalIssues As Integer, _
     issuesCount As Integer, has_subtsk As Boolean, print_row As Long, data As Object, ok_yes As Object, totalIssues5 As Integer, last_row_print As Long, _
     http1 As New MSXML2.XMLHTTP60, url1 As String, response1 As String, json1 As Object, user_name_password_in_base64_encoding As String, i As Integer
    Dim issues As Collection, StartTime As Double, SecondsElapsed As Double
    StartTime = Timer
    user_name_password_in_base64_encoding = UserPassBase64()
    Application.ScreenUpdating = False
    
    Worksheets("Sheet2").Cells(1, 1).Value = "Issue id"
    Worksheets("Sheet2").Cells(1, 2).Value = "Issue key"
    Worksheets("Sheet2").Cells(1, 3).Value = "Status"
    Worksheets("Sheet2").Cells(1, 4).Value = "Priority"
    Worksheets("Sheet2").Cells(1, 5).Value = "Reporter"
    Worksheets("Sheet2").Cells(1, 6).Value = "Reporter Id"
    Worksheets("Sheet2").Cells(1, 7).Value = "Creator"
    Worksheets("Sheet2").Cells(1, 8).Value = "Creator"
    
    url1 = "https://jira/rest/api/2/search?jql=project=project AND issuetype in (Bug)&maxResults=" & 1 & "&startAt=" & 0
    
    With http1
    .Open "GET", url1, False
    .setRequestHeader "Content-Type", "application/json"
    .setRequestHeader "Accept", "application/json"
    .setRequestHeader "Authorization", "Basic " & user_name_password_in_base64_encoding
    
    .send
    End With
    response1 = http1.responseText
    
    Set json1 = JsonConverter.ParseJson(response1)
    
    totalIssues = json1("total")
    
    startAt = 0
    batchSize = 100
    
    Set ok_yes = json1("issues")
    
    Do While startAt < totalIssues
        url = "https://jira-/rest/api/2/search?jql=project=project AND issuetype in (Bug)&maxResults=" & batchSize & "&startAt=" & startAt
        
        http.Open "GET", url, False
        
        http.setRequestHeader "Content-Type", "application/json"
        http.setRequestHeader "Authorization", "Basic " & user_name_password_in_base64_encoding
        
        http.send
        response = http.responseText
        
        Set json = JsonConverter.ParseJson(response)
        
        issuesCount = json("issues").Count
        totalIssues5 = json("total")
        last_row_print = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        
        Set issues = json("issues")
        
        For i = 1 To issuesCount
            Worksheets("Sheet2").Cells(last_row_print, 1).Value = issues(i)("id")
            Worksheets("Sheet2").Cells(last_row_print, 2).Value = issues(i)("key")
            Worksheets("Sheet2").Cells(last_row_print, 3).Value = issues(i)("fields")("status")("name")
            Worksheets("Sheet2").Cells(last_row_print, 4).Value = issues(i)("fields")("priority")("name")
            Worksheets("Sheet2").Cells(last_row_print, 5).Value = issues(i)("fields")("reporter")("displayName")
            Worksheets("Sheet2").Cells(last_row_print, 6).Value = issues(i)("fields")("reporter")("name")
            Worksheets("Sheet2").Cells(last_row_print, 7).Value = issues(i)("fields")("creator")("name")
            Worksheets("Sheet2").Cells(last_row_print, 8).Value = issues(i)("fields")("creator")("displayName")
            
            last_row_print = last_row_print + 1
        Next i
        
        startAt = startAt + batchSize
    Loop
    SecondsElapsed = Round(Timer - StartTime, 2)
    
    MsgBox "All issues have been retrieved."
    
    Application.ScreenUpdating = True
    MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation

End Sub

To prevent Excel from becoming unresponsive, you can consider using background threading for the HTTP requests, so they don't block the main application. You can use multi-threading libraries available in VBA, like vbWatchdog or Async VBA, to achieve this. Additionally, you've already mentioned turning off Application.ScreenUpdating, which is a good practice to improve performance. Increasing the batch size can also help optimize the code further.

英文:
Sub Jira_for_all_issues_v6()
    Dim http As New MSXML2.XMLHTTP60, url As String, response As String, json As Object, startAt As Integer, batchSize As Integer, totalIssues As Integer, _
     issuesCount As Integer, has_subtsk As Boolean, print_row As Long, data As Object, ok_yes As Object, totalIssues5 As Integer, last_row_print As Long, _
     http1 As New MSXML2.XMLHTTP60, url1 As String, response1 As String, json1 As Object, user_name_password_in_base64_encoding As String, i As Integer
    Dim issues As Collection, StartTime As Double, SecondsElapsed As Double
StartTime = Timer
    user_name_password_in_base64_encoding = UserPassBase64()
    Application.ScreenUpdating = False
    
        Worksheets(&quot;Sheet2&quot;).Cells(1, 1).Value = &quot;Issue id&quot;
        Worksheets(&quot;Sheet2&quot;).Cells(1, 2).Value = &quot;Issue key&quot;
        Worksheets(&quot;Sheet2&quot;).Cells(1, 3).Value = &quot;Status&quot;
        Worksheets(&quot;Sheet2&quot;).Cells(1, 4).Value = &quot;priority&quot;
        Worksheets(&quot;Sheet2&quot;).Cells(1, 5).Value = &quot;reporter&quot;
        Worksheets(&quot;Sheet2&quot;).Cells(1, 6).Value = &quot;reporter Id&quot;
        Worksheets(&quot;Sheet2&quot;).Cells(1, 7).Value = &quot;creator&quot;
        Worksheets(&quot;Sheet2&quot;).Cells(1, 8).Value = &quot;creator&quot;
        
        url1 = &quot;https://jira/rest/api/2/search?jql=project=project AND issuetype in (Bug)&amp;maxResults=&quot; &amp; 1 &amp; &quot;&amp;startAt=&quot; &amp; 0
    
    &#39;Set the HTTP request properties
    With http1
    .Open &quot;GET&quot;, url1, False
    .setRequestHeader &quot;Content-Type&quot;, &quot;application/json&quot;
    .setRequestHeader &quot;Accept&quot;, &quot;application/json&quot;
    .setRequestHeader &quot;Authorization&quot;, &quot;Basic &quot; &amp; user_name_password_in_base64_encoding
 
    
    &#39;Send the HTTP request and retrieve the response
    .send
    End With
    response1 = http1.responseText
    
    &#39;Parse the JSON data using a JSON parser library
    Set json1 = JsonConverter.ParseJson(response1)
    
    totalIssues = json1(&quot;total&quot;)
    
    startAt = 0
    batchSize = 100
&#39;    print_row = 2
     Set ok_yes = json1(&quot;issues&quot;)
    
    Do While startAt &lt; totalIssues
        &#39;Set the URL to the JIRA REST API endpoint with the startAt and maxResults parameters
        url = &quot;https://jira-/rest/api/2/search?jql=project=project AND issuetype in (Bug)&amp;maxResults=&quot; &amp; batchSize &amp; &quot;&amp;startAt=&quot; &amp; startAt
        
        &#39;Set the HTTP request properties
        http.Open &quot;GET&quot;, url, False
        
      
        http.setRequestHeader &quot;Content-Type&quot;, &quot;application/json&quot;
        http.setRequestHeader &quot;Authorization&quot;, &quot;Basic &quot; &amp; user_name_password_in_base64_encoding
        &#39;Send the HTTP request and retrieve the response
        http.send
        response = http.responseText
        
        &#39;Parse the JSON data using a JSON parser library
        Set json = JsonConverter.ParseJson(response)
        
        &#39;Extract the desired data from the parsed JSON data and insert it into your Excel worksheet
        
        issuesCount = json(&quot;issues&quot;).Count
        totalIssues5 = json(&quot;total&quot;)
        last_row_print = Worksheets(&quot;Sheet2&quot;).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        
        Set issues = json(&quot;issues&quot;)
        
        For i = 1 To issuesCount
            Worksheets(&quot;Sheet2&quot;).Cells(last_row_print, 1).Value = issues(i)(&quot;id&quot;)
            Worksheets(&quot;Sheet2&quot;).Cells(last_row_print, 2).Value = issues(i)(&quot;key&quot;)
            Worksheets(&quot;Sheet2&quot;).Cells(last_row_print, 3).Value = issues(i)(&quot;fields&quot;)(&quot;status&quot;)(&quot;name&quot;)
            Worksheets(&quot;Sheet2&quot;).Cells(last_row_print, 4).Value = issues(i)(&quot;fields&quot;)(&quot;priority&quot;)(&quot;name&quot;)
            Worksheets(&quot;Sheet2&quot;).Cells(last_row_print, 5).Value = issues(i)(&quot;fields&quot;)(&quot;reporter&quot;)(&quot;displayName&quot;)
            Worksheets(&quot;Sheet2&quot;).Cells(last_row_print, 6).Value = issues(i)(&quot;fields&quot;)(&quot;reporter&quot;)(&quot;name&quot;)
            Worksheets(&quot;Sheet2&quot;).Cells(last_row_print, 7).Value = issues(i)(&quot;fields&quot;)(&quot;creator&quot;)(&quot;name&quot;)
            Worksheets(&quot;Sheet2&quot;).Cells(last_row_print, 8).Value = issues(i)(&quot;fields&quot;)(&quot;creator&quot;)(&quot;displayName&quot;)
            
            last_row_print = last_row_print + 1
        Next i
        
        &#39;Update the startAt parameter for the next batch of search results
        startAt = startAt + batchSize
    Loop
      SecondsElapsed = Round(Timer - StartTime, 2)

    MsgBox &quot;All issues have been retrieved.&quot;
    
    Application.ScreenUpdating = True
  MsgBox &quot;This code ran successfully in &quot; &amp; SecondsElapsed &amp; &quot; seconds&quot;, vbInformation

End Sub

so this is my code. there are about 1000 issues in all and it takes about 16 mins to run. while this runs the excel app becomes unresponsive, how can prevent this and also any tips on how to optimize this code?

i tried turning off the appliction.screenupdating and it kinda works brought it down from 18 mins to 16 mins, also i had increased the batch size from 50 to 100.

答案1

得分: 1

在循环中适当加入 DoEvents,如下所示

...
    startAt = startAt + batchSize
    DoEvents
Loop
...
英文:

Squeeze in a DoEvents somewhere in the loop, like this

...
    startAt = startAt + batchSize
    DoEvents
Loop
...

huangapple
  • 本文由 发表于 2023年4月17日 19:25:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定