Sign in to follow this  
Followers 0
SorryButImaNewbie

AutoIt script turns off, because of array is out of bound

1 post in this topic

Hello all!

I wrote a little script to automate some of my work (for which I'm greatfull).

I have little problem now. My script get data of currency values from specified time period. The problem, if XY day is a holiday or something, then the bank doesn't have new values for that day, thus my script can ask for "till that date" if there is only data for "till that date-1".

I should be done with this for tomorrow and I should learn for my test paper tomorrow, make a genetic algorhytm homework, and at least look at a ppt...

So here is my code, pleas ask if you need more information!

InternetRead2() ;Creating Date and Arfolyam arrays, using DateInterval read from above (_ArraysDisplayed and function is "tested")
;Calculate and write HUF*OSSZEG
Do
    Local $SZAMLATELJOlvaso = _Excel_RangeRead($ExcelObject, Default, $SZAMLATELJoszlop & $CellaOlvasoSzamlalo)
    Local $Time = StringTrimRight($SZAMLATELJOlvaso, 6)
    $DateArrayTimeIndex = _ArraySearch($DateArray, $Time, 0, 0, 0, 0, 1)
    ;MsgBox(64, "Értesítés", $DateArrayTimeIndex)

        If @error = 6 Then
            $DateArrayTimeIndex = _ArraySearch($DateArray, $Time-1, 0, 0, 0, 0, 1)
            ;MsgBox(64, "Értesítés", $DateArrayTimeIndex)
        EndIf
        If @error = 6 Then
            $DateArrayTimeIndex = _ArraySearch($DateArray, $Time-2, 0, 0, 0, 0, 1)
            ;MsgBox(64, "Értesítés", $DateArrayTimeIndex)
        EndIf
        If @error = 6 Then
            $DateArrayTimeIndex = _ArraySearch($DateArray, $Time-3, 0, 0, 0, 0, 1)
            ;MsgBox(64, "Értesítés", $DateArrayTimeIndex)
        EndIf
        If @error = 6 Then
            $DateArrayTimeIndex = _ArraySearch($DateArray, $Time-4, 0, 0, 0, 0, 1)
            ;MsgBox(64, "Értesítés", $DateArrayTimeIndex)
        EndIf

    $DateArrayTimeIndex = $DateArrayTimeIndex+1 ;Egyébként arraysearch -1et ad vissza, 0nál. Passz
    Sleep(200)

    If Not $SZAMLATELJOlvaso = "" Then
    _Excel_RangeWrite($ExcelObject, $ExcelObject.Activesheet, "=" & $OSSZEGoszlop & $CellaOlvasoSzamlalo & "*" & String($ArfolyamArray[$DateArrayTimeIndex]) & "", String($sHUFBeszurOszlop) & $CellaOlvasoSzamlalo)
        If @error Then
            _Excel_RangeWrite($ExcelObject, $ExcelObject.Activesheet, "=" & $OSSZEGoszlop & $CellaOlvasoSzamlalo & "*" & String($ArfolyamArray[$oXML_Node.Length]) & "", String($sHUFBeszurOszlop) & $CellaOlvasoSzamlalo)
            MsgBox(64, "Értesítés", "Valamilyen okból, nem találok az adott dátumhoz árfolyamot, a keresési idők közül a legutolsó árfolyammal töltöttem ki!" & @CRLF & Number($oXML_Node_Datum))
        EndIf
    EndIf


    $CellaOlvasoSzamlalo = $CellaOlvasoSzamlalo + 1

    Until $SZAMLATELJOlvaso = ""

also my internetread2 function :

Func InternetRead2() ;ezt használja az excelmove
;-----------------------------------
;Original Source: https://www.autoitscript.com/forum/topic/184884-solved-basic-com-help-working-on-string-returned-from-api-mi-doing-this-okey/
;by Genius257

;TO DO: Some error handling maybe: what if there is no $MinTime/$MaxTime?
;Read the returns in to array for future use --> Right now, 2 arrays one with the dates and one with the exchangerates
;-----------------------------------
    ;Globals decleared to avoid warnings
    Global $MinTime ;20160601000000
    Global $MaxTime ;20160610000000
    Global $MinTimeFormated = StringTrimRight($MinTime, 6)
    Global $MaxTimeFormated = StringTrimRight($MaxTime, 6)


    $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    If $MaxTimeFormated = "" and $MinTimeFormated = "" Then
        MsgBox(64, "Értesítés", "Nincsenek beolvasott dátumok a memóriában, dummy dátumokat adok meg!" & @CRLF & "20160901, 20160910")
        $MinTimeFormated = "20160901"
        $MaxTimeFormated = "20160905"
    EndIf
    $oHTTP.Open("GET", "http://api.napiarfolyam.hu/?bank=mnb&valuta=eur&datum="&$MinTimeFormated&"&datumend="&$MaxTimeFormated&"", False)
    $oHTTP.Send()
    $sXML = $oHTTP.responseText
    Global $oXML = ObjCreate("Microsoft.XMLDOM")
    $oXML.loadXML( $sXML )
    $oXML_Nodes = $oXML.SelectNodes("./arfolyamok/deviza/item")

    Global $ArfolyamArray[$oXML_Nodes.Length]
    ;MsgBox(64, "Title", "" & $oXML_Nodes.Length & "")
    ;_ArrayDisplay($ArfolyamArray)
    Global $DateArray[$oXML_Nodes.Length]

For $i=0 To $oXML_Nodes.Length-1

    Global $oXML_Node = $oXML_Nodes.Item($i)
    $oXML_Node_Bank = $oXML_Node.SelectNodes("./bank")
    $oXML_Node_Bank = $oXML_Node_Bank.Length>0?$oXML_Node_Bank.Item(0).text:""
    Global $oXML_Node_Datum = $oXML_Node.SelectNodes("./datum")
    $oXML_Node_Datum = $oXML_Node_Datum.Length>0?$oXML_Node_Datum.Item(0).text:""
    $oXML_Node_Penznem = $oXML_Node.SelectNodes("./penznem")
    $oXML_Node_Penznem = $oXML_Node_Penznem.Length>0?$oXML_Node_Penznem.Item(0).text:""
    $oXML_Node_Kozeps = $oXML_Node.SelectNodes("./kozep")
    $oXML_Node_Kozep01 = $oXML_Node_Kozeps.Length>0?$oXML_Node_Kozeps.Item(0).text:""
    $oXML_Node_Kozep02 = $oXML_Node_Kozeps.Length>1?$oXML_Node_Kozeps.Item(1).text:""

    $ArfolyamArray[$i] = $oXML_Node_Kozep01
    $DateArray[$i] = StringTrimRight(StringReplace($oXML_Node_Datum, "-", ""), 8)

    ConsoleWrite( "Match [" & StringFormat("%02i", $i+1) & "]:"&@CRLF& _
        @TAB&"Bank: "&@TAB&$oXML_Node_Bank&@CRLF& _
        @TAB&"Datum: "&@TAB&$oXML_Node_Datum&@CRLF& _
        @TAB&"Penznem: "&@TAB&$oXML_Node_Penznem&@CRLF& _
        @TAB&"Kozep01: "&@TAB&$oXML_Node_Kozep01&@CRLF& _
        @TAB&"Kozep02: "&@TAB&$oXML_Node_Kozep02&@CRLF _
    )
Next
;_ArrayDisplay($ArfolyamArray)
;_ArrayDisplay($DateArray)
EndFunc

My problem is that I have dates in my excel till 2016.10.30, but I only have dates till 28.

(see the api, according to aoutit console, I have the same data: http://api.napiarfolyam.hu/?bank=mnb&valuta=eur&datum=20161001&datumend=20161030 )

My idea/goal (with the If @error then... after @SZAMLATELJolvaso part) is that if this happens, I want it to automaticly jump to the last date of the array (I will be able to write something more elaborate, but for now thats my goal, to save the sinking ship :D )

Thank yu for your help and insight!

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • jjohn
      By jjohn
      Hi all,
      I have the following code, basically, it populates an array with unique characters found in a string, but it turns out only one character is in the array, i don't want to think it is due to a bug of autoit at this point, so i need your help on why, please
      Dim $sx[0] Dim $cn $ss = "Xx" ConsoleWrite(StringLeft($ss, 1) & " " & StringRight($ss, 1) & @CRLF) ConsoleWrite(StringLeft($ss, 1) = StringRight($ss, 1) & @CRLF) ;here show X <> x ConsoleWrite(@CRLF) ;the following is to populate array $sx with different unique characters in $ss For $b = 1 To StringLen($ss) $flg = 0 For $a = 0 To UBound($sx) - 1 ;if asc(StringMid($ss,$b,1)) = asc($sx[$a]) Then ;if this line is used instead of the next, everything is fine If StringMid($ss, $b, 1) = $sx[$a] Then If StringMid($ss, $b, 1) = "x" Then ConsoleWrite("<<->>" & StringMid($ss, $b, 1) & " " & $sx[$a] & @CRLF) $flg = 1 ExitLoop EndIf Next If $flg = 0 Then $cn = $cn + 1 ReDim $sx[$cn] $sx[$cn - 1] = StringMid($ss, $b, 1) EndIf Next ConsoleWrite(UBound($sx) & @CRLF);list the ubound of $sx ;list what is in array $sx For $a = 0 To UBound($sx) - 1 ConsoleWrite($sx[$a] & @CRLF) Next same code in a file is attached as uniqueChar2Array.au3
    • singbass
      By singbass
      I have an issue with disk space on a server so I wrote a simple little script to check specific directories and save the sizes to an Excel spreadsheet.  For this script, I am still using version 3.3.8.1.  Everything works fine, I just have a question.
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.8.1 #ce ---------------------------------------------------------------------------- #include<date.au3> #include<excel.au3> #include<array.au3> $file = FileOpen(@ScriptDir & "\CMScriptDirList.txt", 0) If @error Then Exit ;--folder view still OK $oexcel = _ExcelBookOpen(@ScriptDir & "\CMScriptDirSizesCopy.xlsx") If @error Then Exit ;--folder view now parent folder $excelArray = _ExcelReadSheetToArray($oexcel) $lastrow = $excelArray[0][0] $lastcol = $excelArray[0][1] _ExcelWriteCell($oexcel, _NowCalc(), 1, $lastcol + 1) While 1 $line = FileReadLine($file) If @error Then ExitLoop $size = DirGetSize(StringStripWS($line, 3)) / 1024 / 1024 $iIndex = _ArraySearch($excelArray, $line, 0, 0, 0, 0, 1, 1) If @error Then ContinueLoop _ExcelWriteCell($oexcel, $size, $iIndex, $lastcol + 1) WEnd FileClose($file) _ExcelWriteFormula($oexcel, "=SUM(R2C" & $lastcol + 1 & ":R38C" & $lastcol + 1 & ")", 39, $lastcol + 1) _ExcelWriteFormula($oexcel, "=R39C" & $lastcol + 1 & "/1024", 40, $lastcol + 1) $oexcel.ActiveSheet.columns($lastcol).copy ;used to copy the format of the original last column of the spreadsheet $oexcel.ActiveSheet.columns($lastcol + 1).PasteSpecial(-4122, Default, Default, Default) ;this just pastes the format of the original last column to the new last column $oexcel.ActiveSheet.Range("A1").Select ;select cell A1 just to unselect the entire column from previous command $oexcel.columns.AutoFit ;auto sizes the column width _ExcelBookClose($oexcel, 1) ;save file when closing The script is compiled and sitting is a sub-directory on the server in question. The text file and the spreadsheet that are used are both in this same folder as well.  When I navigate to the folder and run the script by double-clicking on the executable, the process runs but the folder view where I ran the script will go back up one level so when the script completes, I am in the parent folder from where I started.  I have added message boxes throughout the script and have determined that the folder view goes back up one level at some point after the @error check for the file open and before the @error check for the ExcelBookOpen (where the comments are).
      I just wanted to know if someone can tell me why and if there is a way to prevent it. (Note: still using v3.3.8.1 on this machine but slowly converting scripts to v3.3.14.2).
       
       
    • KimberlyJillPereira
      By KimberlyJillPereira
      I am a newbie in AutoIt. May I know what is the code used to expand the width and height of excel cells because I want to insert pictures in the cell.I tried AutoFit but that doesnt work as I cant specify for the width and height. Only for column width I could expand by using .ColumnWidth but for the row I am not able to expand the row? How to do?? What is the code used ??Please help me and thank you.
    • KimberlyJillPereira
      By KimberlyJillPereira
      I am a newbie in AutoIt. May I know what is the code used to expand the width and height of excel cells because I want to insert pictures in the cell.I tried AutoFit but that doesnt work as I cant specify for the width and height. Only for column width I could expand by using .ColumnWidth but for the row I am not able to expand the row? How to do?? What is the code used ??Please help me and thank you.
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening
      I was looking around the forum if I could find a function that allows to convert a string into a 2 dimensional array...
      The first column of the array is always the same, but the rows could change...
      I have a pattern like:

      Column A|Column B
      Static Text 1|Data 1
      Static Text 2|Data 2
      Static Text 3|Data 3
      Static Text 4|Data 4

      Where, Static Text (1...4) will be always the same, and I don't want to change them... But, Data 1...4 are dynamic fields... So, I could have the pattern above AND I coould have the pattern I'm going to show you right below

      Column A|Column B
      Static Text 1|Data 1
      Static Text 2|Data 2
      Static Text 3|Data 3
      Static Text 4|Data 4
      Static Text 1|Data 5
      Static Text 2|Data 6
      Static Text 3|Data 7
      Static Text 4|Data 8

      How can I do in this case?

      Thanks for everyone's help