Forum Discussion

Naveenks's avatar
Naveenks
Contributor
6 years ago

exce reding with DDT

Hi,

 

I wrote a script in DDT to read rows and columns from excel sheet but it is displaying only two even after my sheet having more than 2 rows.:-

 

var RecNo;

 

// Posts data to the log (helper routine)

function ProcessData()

{

  var i;

 

 

 

  for(i = 0; i < DDT.CurrentDriver.ColumnCount; i++)

  Log.Message(DDT.CurrentDriver.ColumnName(i) + ": " + aqConvert.VarToStr(DDT.CurrentDriver.Value(i)));

  Log.PopLogFolder();

  RecNo = RecNo + 1;

}

 

// Creates the driver (main routine)

function TestDriver()

{

  var Driver;

 

  

  Driver = DDT.ExcelDriver("C:\\Users\\naveenks\\Documents\\ExcelRead.xlsx", "Test");

 

  // Iterates through records

  RecNo = 0;

  while (! Driver.EOF() )

  {

    ProcessData(); // Processes data

    Driver.Next(); // Goes to the next record

  }

 

  // Closes the driver

  DDT.CloseDriver(Driver.Name);

}

 

 

Regards,

Naveen

  • Thanks for the reply, Naveenks. As tristaanogre mentioned, it's worth adding your excel file here (or at least a sample to demonstrate the data structure) with the image of the log you get.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    It would be helpful to see the Excel file you're using.

     

    Also, your for loop in ProcessData doesn't have any braces around the code to be executed.  The way it looks now, it will only get the first column and that's all.

     

    Finally, your increment of the record number needs to be inside the while loop, not the for loop, so it actually tracks the number of records.

     

    I've attached my Excel file and here's the slightly modified code

     

    var RecNo;
    // Posts data to the log (helper routine)
    
    function ProcessData(){
        var i;
        for(i = 0; i < DDT.CurrentDriver.ColumnCount; i++){
            Log.Message(DDT.CurrentDriver.ColumnName(i) + ": " + aqConvert.VarToStr(DDT.CurrentDriver.Value(i)));
            Log.PopLogFolder();
        }
    }
    
    // Creates the driver (main routine)
    function TestDriver(){
        var Driver;
        Driver = DDT.ExcelDriver("C:\\Temp\\Test.xls", "Test", true);
        // Iterates through records
        RecNo = 0;
        while (!Driver.EOF()){
            ProcessData(); // Processes data
            Driver.Next(); // Goes to the next record
            RecNo = RecNo + 1;
        }
        // Closes the driver
        DDT.CloseDriver(Driver.Name);
    }

    Here's what the log looks like


    So, as you can see, on principle, your code works.  But it needed a bit of refinement to actually do what you wanted it to do.

    • Naveenks's avatar
      Naveenks
      Contributor

      Hi,

       

      I tried with same code but my log is not showing Column no in left pane,instead it is displaying data repeatedly of the row's in column no field.

       

      Regards,

      Naveen...

      • TanyaYatskovska's avatar
        TanyaYatskovska
        SmartBear Alumni (Retired)

        Thanks for the reply, Naveenks. As tristaanogre mentioned, it's worth adding your excel file here (or at least a sample to demonstrate the data structure) with the image of the log you get.