简体   繁体   English

在QuickTest Pro中使用Excel电子表格中的数据

[英]Using data from an Excel spreadsheet in QuickTest Pro

I have been trying to figure out how to do this for a little while.. Here is my situation: 我一直在试图弄清楚如何做到这一点。这是我的情况:

I have an Excel spreadsheet (lets call this "workbook.xls" and I have two sheets that I need to use for my script. One sheet has a column of data "columnA" and I am trying to put cell A2 (A1 is a header) into my QTP script to use. 我有一个Excel电子表格(让我们将其称为“ workbook.xls”,我需要将两张纸用于脚本。其中一张纸具有一列数​​据“ columnA”,我正尝试将单元格A2放入其中(A1是标头)放入我的QTP脚本中使用。

I want to be able to use this specific value and then process some data and then use the next cell, A3 to process another procedure/process, and so on.. Is this possible? 我希望能够使用此特定值,然后处理一些数据,然后使用下一个单元格A3处理另一个过程/过程,依此类推。是否可能?

In other languages, I know that I could use a for loop to iterate through the data, but I am getting stuck on trying to process the data and then going back through the loop to get the data. 在其他语言中,我知道我可以使用for循环来遍历数据,但是我一直在尝试处理数据,然后返回循环以获取数据。

For example: 例如:

Excel may have A2 as "1" and A3 as "2". Excel可能将A2设为“ 1”,将A3设为“ 2”。 I want to be able to put the value of A1 into an input field on the browser.. Then I want to be able to do some actions (clicking, writing to another file, etc) with this value.. Then I want to move onto the value A3 and put the value of A3 into that same input field as before and then do some other actions (that may not be what I did with A2). 我希望能够将A1的值放入浏览器的输入字段中。然后,我希望能够对该值进行一些操作(单击,写入另一个文件等)。然后,我想移动放在值A3上,然后将A3的值放入与以前相同的输入字段中,然后执行其他一些操作(这可能与我对A2所做的不一样)。 I am wondering how I could achieve this scenario. 我想知道如何实现此方案。

Thanks in advance. 提前致谢。

Yes, it's possible. 是的,有可能。 Assuming I understood your question correctly you could do something like this: 假设我正确理解了您的问题,则可以执行以下操作:

Set xl = CreateObject("Excel.Application")
xl.Visible = True  'set to False for production

Set wb = xl.Workbooks.Open("C:\path\to\workbook.xls")
Set ws = wb.Sheets(1)

firstRow = ws.UsedRange.Rows(1).Row
lastRow  = firstRow - 1 + ws.UsedRange.Rows.Count

For i = firstRow+1 To lastRow
  val = ws.Cells(i, 1).Value   'get value from cell
  'do stuff with val
Next

wb.Close
xl.Quit

A test's DataTable is an Excel spreadsheet. 测试的数据表是Excel电子表格。 You could import your data into your test's DataTable in QTP - in the default sheet(s) or add your own - and then access them in code: 您可以将数据导入QTP中测试的DataTable中(在默认工作表中),也可以添加自己的数据,然后通过代码访问它们:

Set sheet = DataTable.GetSheet "Sheet Name"
numRows = sheet.GetRowCount

For index = 0 To numRows
    value = sheet.GetParameter(index)
    'Do something
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM