fayrehouse
12 years agoFrequent Contributor
Working with CSV files - but NOT DDT
Hi all. Has anyone got any experience / suggestions for the following.... I have a script that runs fine - producing a CSV file as part of it's processing. Now, I need to perform various...
- 12 years agoYou should be able to use the DDT object without using the CSV as a Driver of the test. In my experience it's a little clunky, but doable. You just have to create a new DDT driver and name it something different than the test driver DDT. Then you can iterate through it and find your information.
The challenge in using the DDT object is that it thinks it HAS TO be a driver, so inherent in that attribute it defaults to read row by row. This means it's predictable, but can be tedius when looking up the last row of the CSV. In other words you can't just ask for the value of Column_D, Row_P you have to go iterate through the file and find it.
What I do is something like this (I use Excel and VBScript for this, so change it to a CSV driver and your language of choice and you should be good):
'varible for the excel or csv file.
validationFileDriver = [LOCATION TO FILE]
'this next line sets the driver file object [TabName] is the name of the Excel sheet... this is for Excel 2010
Set valFileDriver = DDT.ExcelDriver(validationFileDriver, [TabName], True)
'This line names the driver, I just put Validation driver in there, you can call it what you want.
valFileDriver.Name = [VALIDATION DRIVER]
Then you'll have to iterate through the rows and find the row you need. Since TC doesn't have a clue which column is which and I want my code to be robust I've written a little function called FindColumn where 'header' is the header text you're looking for and 'dataSheetDriver' is the [VALIDATION DRIVER] from above.
Function FindColumn(header, dataSheetDriver)
colCount = DDT.DriverByName(dataSheetDriver).ColumnCount
For i = 1 to colCount
currentColumn = DDT.DriverByName(dataSheetDriver).ColumnName(i-1)
If currentColumn = header Then
FindColumn = i-1
'Log.Message header & " column value = " & FindColumn
Exit For
End If
Next
End Function
And then I use that to find a value in a specific column ( and row.
If DDT.DriverByName([VALIDATION DRIVER]).Value(FindColumn(field_a, [VALIDATION DRIVER])) = "value_b" Then
'Do whatever you want with the rest of this row
'say you have a field_x column as well you could ...
field_x_value = DDT.DriverByName([VALIDATION DRIVER]).Value(FindColumn(field_x, [VALIDATION DRIVER]))
Else
Log.Message "Didn't find '" & field_a & "'."
End If
You'll probably have to wrap that If-Then statement in a For-Next loop that iterates through the whole sheet, but I hope this gets you headed toward a solution.
JC