处理AHK中的错误并更改csv文件的格式

时间:2014-08-02 13:05:17

标签: csv automation autohotkey

很多时候,我似乎无法在我查询地址的网站上找到一个邮政编码,这会使脚本停止运行,并且需要我在一系列两个对话框窗口中单击是以进行上。

e.g。

enter image description here

enter image description here

enter image description here

1。如何让我的代码忽略这些错误消息并继续执行而不停止代码并要求用户干预,即只输入邮政编码,使数据输出看起来像这样:

Levens Hall Drive,Westcroft,MK4 4FL
,52.003,-0.798
Slatepits Croft,Olney,MK46 5EF
,52.163,-0.708
Water Eaton Road,Water Eaton,MK2 2RD
,51.99,-0.728
Southern Way,Hodge Lea,MK12 5EG
,52.054,-0.811
Saxon Street,Stantonbury,MK13 7BX
,52.067,-0.776
Tongwell Street,Brinklow,MK9 2ZQ
,52.032,-0.692
,,MK17 0FE <---------- error occurred here, ideally we would just make an entry for MK17 0FE and blank the other fields and then carry on...
Bury Street,Green Park,MK16 8EU
,52.086,-0.726
Bletcham Way,Tilbrook,MK7 7DT
,52.016,-0.689
High Street,Buckingham CP,MK18 1JL
,52.001,-0.986
Dunstable Road,Flitwick,MK45 1JB
,52,-0.496
Amway UK Ltd,Caldecotte Lake Drive,MK7 8JU
,52.003,-0.703

2。每当我在Excel中打开它时,我的CSV输出似乎都是错误的公式,任何关于如何将所有内容都放在一行上的想法?

enter image description here

以下代码:

;// Fileread, test, somefilename
fileread, AddressList, test.csv


IE := ComObjCreate("InternetExplorer.Application")
IE.Visible := true


loop, parse, AddressList, `n
{
    PostCode := A_LoopField
    IE.Navigate("http://nominatim.openstreetmap.org/search.php?q=" PostCode)


    ToolTip, Now looking up %PostCode%


    while IE.readyState!=4 || IE.document.readyState != "complete" || IE.busy
        continue

    ; Collect results 1
    Sleep 2000

    ToolTip, ; remove tooltip

    Name_Elements       := IE.document.getElementsByClassName("name")
    Loop, 1
    {
        Addr_text       := Name_Elements[A_Index-1].innertext

        Latlon_element  := Name_Elements[A_Index-1].parentElement.getElementsByClassName("latlon")[0]
        Latlon_text     := Latlon_element.innertext

        String_Object   := StrSplit(Addr_text, "`,")
        LatLon_Object   := StrSplit(Latlon_text, "`,")

        If (Substr(Addr_text, 1, 2) = "MK")
        {
            Addr := Trim(String_Object[2]) . "," . Trim(String_Object[3]) . "," . PostCode . "," . LatLon_Object[1] . "," . LatLon_Object[2]
        }
        Else
        {
            Addr := Trim(String_Object[1]) . "," . trim(String_Object[2]) . "," . PostCode . "," . LatLon_Object[1] . "," . LatLon_Object[2]
        }

     Filename_text := Substr(PostCode, 1, 2)
StringLower Filename_text, Filename_text

FileAppend,%Addr%`n,%Filename_text%_addresslist.csv

    }
}
IE.quit() 

感谢。

1 个答案:

答案 0 :(得分:2)

您是否尝试过使用Try / Catch?

http://ahkscript.org/docs/commands/Try.htm

如果您正在使用COM对象,则可以在需要时使用以下方法打开或关闭COM错误:

http://ahkscript.org/docs/commands/ComObjError.htm

错误级别已过时,但可能很有用:

http://ahkscript.org/docs/misc/ErrorLevel.htm