Showing posts with label DataTable Methods. Show all posts
Showing posts with label DataTable Methods. Show all posts

Friday, 24 May 2013

DataTable Methods in QTP

DataTable Methods

One can apply number of methods on run time DataTable.  All methods performed on this object apply to the run-time DataTable object only. Changes to the run-time DataTable object are reflected in the run results, but the design-time Data Table is not affected.

Associated Methods -


1- AddSheet Method
Adds the specified sheet to the run-time Data Table and returns the sheet so that you can directly set properties of the new sheet in the same statement.

Syntax- DataTable.AddSheet(SheetName)
Example- DataTable.AddSheet ("MySheet")

2- DeleteSheet Method
Deletes the specified sheet from the run-time Data Table.

Syntax- DataTable.DeleteSheet SheetID or SheetName
Example- DataTable.DeleteSheet "MySheet"

3- Export Method
Saves a copy of the run-time Data Table in the specified location.

Syntax- DataTable.Export(FileName)
Example- DataTable.Export ("C:\flights.xls")

4- ExportSheet Method

Exports a specified sheet of the run-time Data Table to the specified file.

  • If the specified file does not exist, a new file is created and the specified sheet is saved.
  • If the current file exists, but the file does not contain a sheet with the specified sheet name, the sheet is inserted as the last sheet of the file.
  • If the current file exists and the file contains the specified sheet, the exported sheet overwrites the existing sheet.

Syntax- DataTable.ExportSheet(FileName, DTSheet)
Example- DataTable.ExportSheet "C:\name.xls" ,1     'Saving first sheet of the run-time Data Table to the name.xls file

5- Import Method
Imports the specified Microsoft Excel file to the run-time Data Table.

Syntax- DataTable.Import(FileName) 
Example- DataTable.Import ("C:\flights.xls") 

6- ImportSheet Method
Imports a sheet of a specified file to a specified sheet in the run-time Data Table. The data in the imported sheet replaces the data in the destination sheet. 

Syntax- DataTable.ImportSheet(FileName, SheetSource, SheetDest)
Example- DataTable.ImportSheet "C:\name.xls" ,1 ,"name"   'Importing the first sheet of the name.xls table to the name sheet in the test's run-time Data Table.

7- GetCurrentRow Method
Returns the current (active) row in the first sheet in the run-time Data Table (global sheet).

Syntax- DataTable.GetCurrentRow
Example- row = DataTable.GetCurrentRow   'Retrieving the row currently used in run-time DataTable.


8- GetRowCount Method
Returns the total number of rows in the longest column in the first sheet in the run-time Data Table (global sheet). 

Syntax- DataTable.GetRowCount
Example- cnt=DataTable.GetRowCount
              rowcount = DataTable.GetSheet("MySheet").GetRowCount


9- GetSheet Method
Returns the specified sheet from the run-time Data Table. 

Syntax-  DataTable.GetSheet(SheetID)
Example- MyParam=DataTable.GetSheet ("MySheet").AddParameter("Time", "8:00")     'using GetSheet method to return the "MySheet" sheet of the run-time Data Table and add a parameter to it.

10- GetSheetCount Method
Returns the total number of sheets in the run-time Data Table. 

Syntax- DataTable.GetSheetCount
Example- sheetcount = DataTable.GetSheetCount


11- SetCurrentRow Method
Sets the specified row as the current (active) row in the run-time Data Table. 

Syntax- DataTable.SetCurrentRow(RowNumber)
Example- DataTable.SetCurrentRow (2)  'using SetCurrentRow method to change the active row to the second row in the global run-time Data Table.


12- SetNextRow Method
Sets the row after the current (active) row as the new current row in the run-time Data Table. 

Syntax- DataTable.SetNextRow
Example- DataTable.SetNextRow

13- SetPrevRow Method
Sets the row above the current (active) row as the new current (active) row in the run-time Data Table. 

Syntax- DataTable.SetPrevRow
Example- DataTable.SetPrevRow

Associated Properties-

1- GlobalSheet Property
Returns the first sheet in the run-time Data Table (global sheet).

Syntax- DataTable.GlobalSheet
Example- DataTable.GlobalSheet.AddParameter "Time", "5:45"

2- LocalSheet Property
Returns the current (active) local sheet of the run-time Data Table.

Syntax- DataTable.LocalSheet
Example- DataTable.LocalSheet.AddParameter("Time", "5:45")

3- RawValue Property
Retrieves the raw value of the cell in the specified parameter and the current row of the run-time Data Table.

Syntax- DataTable.RawValue ParameterID [, SheetID]    'SheetId is optional if not used, global sheet of run time data table is used.
Example- Val=DataTable.RawValue ("Date", "Sheet1")  'Using Sheet1
                Val=DataTable.RawValue ("Date")  'Using Global Sheet

4- Value Property
DataTable default property. Retrieves or sets the value of the cell in the specified parameter and the current row of the run-time Data Table.

Syntax-
To find the value:

DataTable.Value(ParameterID [, SheetID])   'SheetId is optional if not used, global sheet of run time data table is used.

or

DataTable(ParameterID [, SheetID])   'SheetId is optional if not used, global sheet of run time data table is used.

To set the value:

DataTable.Value(ParameterID [, SheetID])=NewValue   'SheetId is optional if not used, global sheet of run time data table is used.

or

DataTable(ParameterID [, SheetID]) =NewValue    'SheetId is optional if not used, global sheet of run time data table is used.

Example- DataTable.Value ("Destination", "Sheet1")="ABC"    'Using Sheet1
                DataTable.Value ("Destination")="ABC"        'Using Global Sheet
               Val=DataTable.Value("Name","dtGlobalSheet")                      'Using Global Sheet
               Val=DataTable.Value("Name",1)           'Using Sheet1
               Val=DataTable.Value("Name")             'Using Global Sheet