blizzardlyk 发表于 2010-3-31 16:48:40

DataTable 常用的方法

'Methods of getting a Data Table value
val = datatable.Value("ParamName",dtGlobalSheet)
val = datatable.Value("ParamName","Global")

'by giving the sheet index starting from 1 for the global sheet
val = datatable.Value("ParamName",1)

'Sheet name or id is a optional parameter and is assumed to be as for global data sheet in case not provided
val = datatable.Value("ParamName")

'Value property is the default property of the DataTable object so DataTable("ParamName",dtGlobalSheet)
'is equivalent to datatable.Value("ParamName",dtGlobalSheet)
val = datatable("ParamName",dtGlobalSheet)
val = datatable("ParamName")

'Using the data table object model
val = datatable.GlobalSheet.DeleteParameter("ParamName").value
val = datatable.GlobalSheet.DeleteParameter("ParamName").valueByRow(1)

'============
'Various methods to get data table value
val = datatable.Value("ParamName",dtLocalSheet)
val = datatable.Value("ParamName","<LocalActionName>")
val = datatable("ParamName",dtLocalSheet)
val = datatable("ParamName","<LocalActionName>")

'The local sheet of the action which is executing this statement
val = datatable.LocalSheet.DeleteParameter("ParamName").value

'==============
'Function to check if DataTable exists
Function isSheetExists(sheetName)
   On error resume next
   isSheetExists = true
   err.clear
   Set objSheet = datatable.GetSheet(sheetName)
   If err.number <>0Then
isSheetExists= false
   End If
End Function

'==================
'This would be modified to 1.23456789E+010 due to autoformatting
datatable("ParamName") = "12345678901"
'This will not be auto formatted and will be treated as text
datatable("ParamName")= "'" &"12345678901"

'==========
'Check if a parameter exists in data table
Function isParameterExists(sheetName,paramName)
On error resume next
isParameterExists = true
err.clear
parameTotal = datatable.GetSheet(sheetName).GetParameter(paramName)
   If err.number <>0Then
isParameterExists= false
   End If   
End Function

'===========
'Variable declaration
Dim i,j
Dim rowCount,colCount
Dim cellText,objTable

'Get table object
Set objTable = Browser("").Page("").WebTable("")

'Get the row count of the webtable
rowCount = objTable.RowCount

'Get the column count of the webtable
rowCount = objTable.ColumnCount(1)

'Create a output sheet
Set outSheet = datatable.AddSheet("Output")

'Greate Parameters based on the 1st row of the web table
For i=2 to colCount
cellText = objTable.GetCellData(1,i)

'Note in case the CellText contains space in between Then QTP will automatically convert it to a "_" character
outSheet.AddParameter celltext,""
Next

'Skip first row as we assumed it to be a header rows
For i = 2 to rowCount
outSheet.SetCurrentRow i-1

'Re-calculate the column count as some rows have different column sizes
colCount = objTable.ColumnCount(i)

For j = 2 to colCount
cellText = objTable.GetCellData(i,j)
outSheet.GetParameter(j-1).value = cellText
Next
Next

'===================
'Get a value by row
val = datatable.GetSheet("SheetName").GetParameter("ParamName").ValueByRow(RowNumber)

'==================
'Declare variable
Dim i,iCount

'Get the global sheet object
Set oGlobal= datatable.GlobalSheet

'Get # of rows
iCount = oGlobal.GetRowCount

For i = 1 to iCount
'set the current row
oGolbal.SetCurrentRow i
'Execute the code to be repeated here
msgbox DataTable("UserName")
Next

'==================

TIB 发表于 2010-4-8 11:51:01

AdvancedQTP.com上有一个在线测试QTP的DataTable的掌握程度的网页:
http://www.advancedqtp.com//wp-content/uploads/Quiz/DT/QuizForce.html
页: [1]
查看完整版本: DataTable 常用的方法