'*******************sub Class_Terminate End***********
'the function only for providing a demo....
Public Function GetSheetData(sFileName, SheetNumber, strRangeBegin, strRangeEnd)
With Environment("Excel_Object")
.Visible = False
Set bjWorkbook = .Workbooks.Open(sFileName) '"C:/temp/Branches.xls"
Set bjWorkSheet = objWorkbook.WorkSheets(SheetNumber)
Reporter.ReportEvent micDone, "Current range:"&strCurrentQueryRangeBegin, _
objWorkSheet.Range(strRangeBegin&":"&strRangeEnd).value
Set bjWorkbook = Nothing
Set bjWorkSheet = Nothing
End with
End Function
End Class
Set oExcelInstace1 = New OurExcel
oExcelInstace1.GetSheetData "C:/temp/Branches.xls", 1, "A200", "A200"
oExcelInstace1.GetSheetData "C:/temp/Branches.xls", 1, "A201", "A201"
Set oExcelInstace2 = New OurExcel
oExcelInstace2.GetSheetData "C:/temp/Branches.xls", 1, "A202", "A202"
oExcelInstace2.GetSheetData "C:/temp/Branches.xls", 1, "A203", "A203"
'with class's Class_Terminate method, you can call it using the following style....
'Notice:This behave will call destructor of your class.............
'If has reference exist, then you will find one excel process in your machine.
Set oExcelInstace1 = Nothing
Set oExcelInstace2 = Nothing
里面有两个Environment变量,一个Excel_Object,一个Reference_counter。我想尽力把这东西做的象COM,当然有很大很大差距。当初就是为了找个全局的地方放数据而已。
3)存储在QTP的GlobalTable中。
涉及2个函数。一个是写入到GlobalTalbe,另一个是从其读入。
Function setValue(strColName, strColValue)
On Error Resume Next
DataTable.Value(strColName, dtGlobalSheet) = strColValue
If Err.Number <> 0 Then DataTable.GlobalSheet.AddParameter strColName, strColValue
setValue = strColValue
End Function
Function getValue(strColName)
Dim outputTemp
On Error Resume Next
outputTemp = DataTable.Value(strColName, dtGlobalSheet)
If Err.Number <> 0 Then
getValueFromGS = ""
Exit Function
Else
getValue = outputTemp
End If
End Function
一般这个方法的实现,可能还要遍历列名来判断是否存在,这样如果变量很多肯定效率低。QTP对于DataTable取数的实现肯定也进行了遍历或Find这样的方法,所以任务尽量让QTP来解决比较好,比如随机数干脆就直接用RandomNumber对象而取代 vbscript的Randomize加Rnd。
而On Error在这里很好用,不能把它只想用于容错恢复,这里算巧用吧,好像还是容错。
文章来源于领测软件测试网 https://www.ltesting.net/