Forum Discussion

frederic_gignac's avatar
frederic_gignac
New Contributor
13 years ago

Writing to Excel

Hello,



I'm kinda new to TestComplete and I got some newb questions.



Currently, I'm working on a script that read two excel files and writing to a third one. One of my colleagues suggested me to go with DB Tables for the two reading files and it's working nicely. However, I had issue to writing to the third one.



I think the best way I should do it is to put my data in a multiple dimension array and then, writing it in one shot. Though, I don't find a way to declare a multiple dimension array (without bounds) in JScript (not Javascript).



Is there a better way ? Using a Memory Table ?



Any help is welcome

Thanks!

Fred

3 Replies

  • Sorry I don't understand the problem regarding JScript dynamic array usage (when you know nothing neither about final array dimensions no about number of elements of final table (or set of tables).



    You may define any element in empty array and assign existing array as element for another array (getting two-dimensional array). You may define 3rd dimension etc.



    Yes, you don't need to know actual size of arrays when define them. The final array size can be read from .length property and actual value in every cell can be checked with ( myArr[ index ] != undefined ) comparison.



    Here the example of totally random array elements definitions.



    function arrDemo()

    {

      var arrExample = [];

      arrExample[ 14 ] = "string";

      arrExample[ 23 ] = "string2";

     

      for ( var index = 0; index < arrExample.length; index++ ) // array length = 24 (max index defined, counting from 0)

        Log.Message( arrExample[ index ] );

     

      Log.Message ( "----------------------------------------------------" );

         

      var arrExampleSecondDimension = []; // empty array

     

      arrExampleSecondDimension[ 25 ] = arrExample;

      arrExampleSecondDimension[ 37 ] = [ "Value1", "Value2", "Value3" ];

      arrExampleSecondDimension[ 25 ][ 18 ] = "New value"; // when one array "packed" into another it's possible to add/define new elements in it (and change it's length if necessary).

     

      Log.Message( arrExampleSecondDimension[ 25 ][ 14 ] );

      Log.Message( arrExampleSecondDimension[ 25 ][ 18 ] );  

      Log.Message( arrExampleSecondDimension[ 37 ][  2 ] );

    }







  • Honestly, I did not understand what array related features exist in Javascript and don't in JScript?



    As for me, I'm quite happy using both hashed arrays (properties of objects) and ordinary (indexed) arrays in conjunction.



    Following piece of code which creates two "workbooks" and assigns tables (two-dimensional arrays) into it. Workbooks addressed using hashed (named) array.






    function arrayDemo()


    {


      var myWorkbook = {};


      var valueCounter = 0;


     


      myWorkbook.Sheet1 = makeTable();


      myWorkbook.Sheet2 = makeTable();


      


      // output


      for ( var sheet in myWorkbook )


      {


        for ( var tableLine = 0; tableLine < myWorkbook[ sheet ].length; tableLine++ )


          for ( var cell = 0; cell < myWorkbook[ sheet ][ tableLine ].length; cell++ )


            Log.Message( "workbook: " + sheet + ", cell[" + tableLine + "][" + cell + "] = " + myWorkbook[ sheet ][ tableLine ][ cell ] );


      }        


      


      function makeTable() // build two-dimensional array (table)


      {


        var myTable = [];


        for ( var tableLine = 0; tableLine < 3; tableLine++ )


        {


          var myString = [];


          for ( var cell = 0; cell < 10; cell++ )


            myString.push( "Value " + ( valueCounter += 2 ) ); // value counter used just for value generation


          myTable.push( myString );


        }


        return myTable;


      }


    }


     

  • Hello,



    I'll try your example but that's not quite what I need.



    In fact, I "read" an old Cobol application and I need to put data that I found into an Excel file for later use.



    I don't know at the time I write in the array how much lines will be, nor the columns number. That why I thought to put it into a multidimensonal array temporary and then, at the end, writing it to Excel.



    Thanks!