我有一个sub打开excel,创建一个连接到我的google工作表,然后使用insert语句将数据添加到我的Access数据库。
这有效,但速度非常慢(需要约30秒才能获得6条记录)
Debug.Print "before wait while " & Now
'Wait for google-doc data to be downloaded.
lTimer = 0
Do While Left(wst.cells(1, 1), 12) = "ExternalData" And lTimer < 40
Debug.Print "Wait loop " & lTimer
Debug.Print "during wait while " & Now
Sleep 250 ' Wait 0.25 sec before re-checking data
DoEvents
lTimer = lTimer + 1
Loop
Debug.Print "after wait while" & Now
我尝试删除第一个while循环,但后来没有导入任何内容
代码中有什么东西会减慢它或者这只是一个缓慢的过程吗?
*我知道这可能适合代码审查,但我更感兴趣的是为什么它比它的错误代码慢?
**编辑以添加调试输出和新的
start time 18/07/2017 9:06:58 a.m.
before connect 18/07/2017 9:06:58 a.m.
before wait while 18/07/2017 9:07:00 a.m.
Wait loop 0
during wait while 18/07/2017 9:07:00 a.m.
Wait loop 1
during wait while 18/07/2017 9:07:00 a.m.
Wait loop 2
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 3
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 4
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 5
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 6
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 7
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 8
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 9
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 10
during wait while 18/07/2017 9:07:03 a.m.
Wait loop 11
during wait while 18/07/2017 9:07:03 a.m.
after wait while 18/07/2017 9:07:28 a.m.
代码结束
class Api::V1::AbilitiesController < API::V1::BaseController
before_action :authenticate_api_v1_user!
def index
@resources = User.first.roles.map{|role| role.grants}.flatten!
render json: @resources.group_by{|x| x.action}
end
end
答案 0 :(得分:1)
发现另一个问题,即只下载了100条记录,并且解决方案也解决了速度问题。
我找到了解决方案here。
它表示您需要更改链接的格式
https://docs.google.com/spreadsheets/d/YOURSPREADSHEET/edit?usp=sharing
到
https://spreadsheets.google.com/tq?tqx=out:html&tq=&key=YOURSPREADSHEET&gid=1
将其从可编辑视图更改为更基本的视图
答案 1 :(得分:0)
使用Debug.Print
查看实际需要这么长时间的部分。 How to debug VBA code
Timer
是Access中的一项功能,请勿将其用作变量名称。
您等待循环必须进行Sleep
调用和DoEvents
调用,以避免占用CPU。
Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
'Wait for google-doc data to be downloaded.
lTimer = 0
Do While Left(wst.cells(1, 1), 12) = "ExternalData" And lTimer < 40
Debug.Print "Wait loop " & lTimer ' Ctrl+g shows output
Sleep 250 ' Wait 0.25 sec before re-checking data
DoEvents
lTimer = lTimer + 1
Loop
如果这没有帮助,你可能不得不忍受它。