'*******************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在這里很好用,不能把它只想用于容錯恢復,這里算巧用吧,好像還是容錯。
文章來源于領測軟件測試網 http://www.kjueaiud.com/