Forum Discussion

Uzumaki's avatar
Uzumaki
Occasional Contributor
13 years ago

From two-dimensional array to .csv file

Hi,



How do I export data from a two-dimensional array into a csv file using scripts(J-Script, C#-Script) in TestComplete?



Thank you for any help.



/Joakim

3 Replies

  • Uzumaki's avatar
    Uzumaki
    Occasional Contributor
    It would also be nice if someone could explain how I easliy modify an csv file using scripts in TestComplete.



    Thank you!



    /Joakim
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Try using aqFile methods.  



    First, use the OpenTextFile method to open the CSV file.  This method will return an aqTextFile object which allows you to use WriteLine to the text file.



    Now, as to the line you're going to write.  A CSV file consists of rows of values separated by specific characters.  This is a concept that TestComplete calls a string list.  So, you would use aqString methods to work with a string list to create a row for that table and then use WriteLine to write that row out.  You would use aqString.AddListItem to add items to the end of your string list from your array.  Keep adding items to that list until you reach the end of your row and then pass the result of that to your writeline method to write the whole row to your text file.



    So... you'd need a nested set of for-loops to loop through the two dimensions of your array.  The outer loop would by your "row" which would include your WriteLine call.  Before the writeline call, you'd have your inner loop which would go through your columns adding to your list item.



    so, in Pseudo code, it would look something like this:



    function blah(array)

    {

    var InnerIndex, OuterIndex;

    var MyString;

    var MyCSVFile = aqFile.OpenTextFile("C:\\Folder1\\MyCSVFile.CSV", faReadWrite, ctANSI, true)

    for (OuterIndex=0;OuterIndex<=RowHighBound;OuterIndex++)

    {

        MyString = "";

        for(InnerIndex=0;InnerIndex<=ColumnHighBound;InnerIndex++)

    {

            MyString = aqString.AddListItem(MyString, array[OuterIndex, InnerIndex], -1)        

    }

        MyCSVFile.WriteLine(MyString)

    }



    }




    Note that the above code has not been tested but hopefully the concept comes out clear.



    As for modifying an existing CSV file, you could use similar methodology in that you would use methods of the aqTextFile and aqString to read values, modify values, and write them back out.
  • Uzumaki's avatar
    Uzumaki
    Occasional Contributor
    Thank you very much Robert!



    This was exactly what I was looking for.



    Best regards



    /Joakim