Datatable object in UFT

Datatable object is used to store the test data required for testing the application. You can think of DataTable as a Excel Workbook. By default, excel file (default.xls) from the test directory is imported when the test run is started. Just like Excel workbook, a datatable can have one or more sheets in it. There is a special sheet in Datatable called as global sheet. Any action within a test can access data from global sheet. For each row in the global sheet, test is repeated for that row. But we can change this iteration setting from test settings dialog. Other than global sheet, we also have sheet per action and only specific action can access data from such sheet. We can also add extra sheet in a data table. The changes you make to the datatable are not saved parmanently. To save the changes in the datatable, you will have to export your datatable to excel file. We can perform various operations on Datatable and Datasheet as mentioned below.
  • Access data from global and local sheet
  • Write data to global and local sheet
  • Iterate through the rows and columns of the sheet
  • Import all or specific sheets from the excel workbook in Datatable
  • Export all or specific sheet from Datatable to excel file
Here is the example that shows how to use Datatable in UFT.
'to import all sheets from excel file, use below  line of code
datatable.Import "mydata.xlsx"

'To add new sheet in datatable, use below line of code
datatable.AddSheet "uftsheet"

'to import  single sheet from excel file, use below  line of code
'here we are importing the sheet data from mydata.xls into uftsheet in datatable.
datatable.ImportSheet "mydata.xlsx","data","uftsheet"

'You can add a parameter using below syntax. Second argument is the value of the parameter
'Here we have added parameter with name - result
datatable.GetSheet("uftsheet").AddParameter "result", "paramValue"

'to read value from cell, use parameter name and sheet name
msgbox datatable.Value ("result","uftsheet")

for i=1 to 2
msgbox datatable("result","uftsheet")
'to move to next row, use SetNextRow method

'Below code gets the total used rows in the sheet

'Below code set the pointer to first row in uftsheet

'To Export complete datatable, use below line of code

'export only particular sheet from the datatable to excel file, use below line of code
datatable.ExportSheet "specificsheetexport.xls","uftsheet"

'Check if parameter exists in sheet.
on error resume next

if err.number<> 0 then
        msgbox ("Parameter result does not exist in uftsheet")
         msgbox "Parameter result exists in uftsheet"
end if

Complex problems, Simple Solutions