Micro Focus QTP (UFT) Forums
GetRowWithCellText does not work if the header has a colspan - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: GetRowWithCellText does not work if the header has a colspan (/Thread-GetRowWithCellText-does-not-work-if-the-header-has-a-colspan)



GetRowWithCellText does not work if the header has a colspan - ConstantChange - 05-02-2008

Hi all!

Actually I think I just post this to get rid of/share some more negative feelings about certain functions.

The method GetRowWithCellText (WebTable object) does not work if the header of the table does not match the "actual" columns of the table. By that I mean that the <th.. has a colspan of two, and each of the following lines contains 2 <td>.

When using GetRowWithCellText on such a table, it will only be able to find things in the first line (effectively the TH).

Again I will have to write my own methods, but if somebody else would like to add a solution or rant a bit along side me - please do so!

Cheers!
CC


RE: GetRowWithCellText does not work if the header has a colspan - Anshoo Arora - 05-09-2008

Do you want to capture a value from a given row? or a cell?


RE: GetRowWithCellText does not work if the header has a colspan - ConstantChange - 05-09-2008

Hi!

In advance: This is solved, I created my own function and that actually works..

But @Anshoo: No, this function is supposed to return a row number for a given text, so if there is a table and somehwhere in line 3 the word "test" occurs, normaly the this call GetRowWithCellText("text") should return 3. But that does not always work. Thanks anyway though! Smile

In case somebody wants to use my function, here it is. Its not very elegant, but I came to the conclusion that VBScript (or lets say the DotNet Object Model) does not want us to be elegant...

Code:
Function getTableRowWithText(table, textToFind, exact, uncountHeader)
    rows=table.rowCount()
    getTableRowWithText=-1
    if(table is nothing or textToFind="" or rows<1) then
        Exit function
    end if
    For i=1 to rows
        For j=1 to table.columnCount(i)
            val=table.getCellData(i, j)
            if(exact) then
                if(val=textToFind) then
                    getTableRowWithText=i
                    if(uncountHeader) then
                        getTableRowWithText=getTableRowWithText-1
                    end if
                    Exit function
                end if
            else
                if(instr(val, textToFind)>0) then
                    getTableRowWithText=i
                    if(uncountHeader) then
                        getTableRowWithText=getTableRowWithText-1
                    end if
                    Exit function
                end if
            end if
        Next
    Next
End Function



RE: GetRowWithCellText does not work if the header has a colspan - ConstantChange - 05-09-2008

hm, sorry, somehow the tabs got lost....


RE: GetRowWithCellText does not work if the header has a colspan - Ankur - 05-09-2008

nice of you ConstantChange for posting your solution to help others... if everyone shows the same spirit, this can indeed become a better place for QTP beginners and experts alike.

btw, you can use the "Preview Post" button (below) to preview your posts for any formatting errors etc.


RE: GetRowWithCellText does not work if the header has a colspan - Anshoo Arora - 05-10-2008

ConstantChange, you can also try this (a little simple and configurable):

Code:
Public Function getTargetRow(sText)

    Set oTable = Browser("micclass:=Browser").Page("micclass:=Page").WebTable("")
    rowTotal = oTable.GetROProperty("rows")
    colTotal = oTable.GetROProperty("cols")
    For rowStart = 1 to rowTotal
        For colStart = 1 to colTotal
            sData = ColInfoTable.GetCellData(rowStart,colStart)
            If sData = sText Then
                getTargetRow = rowStart
                Exit Function
            End If
        Next
    Next

End Function



RE: GetRowWithCellText does not work if the header has a colspan - Mahmoud.Karam - 05-08-2012

Thanks "ConstantChange" for posting this thread and for your proposed solution.

And thanks "Anshoo Arora" for your proposed solution, I tried it but it suffered also from the same problem that the "colTotal" always reads the first row columns. and the line "sData = ColInfoTable.GetCellData(rowStart,colStart)" needed small correction to be "sData = oTable.GetCellData(rowStart,colStart)". I've made small modifications to your function correcting the errors and adding 2 new parameters; the table as an input object and the table columns to make it more generic.

Code:
Public function getTargetRow(oTable, sText, colTotal)
    rowTotal = oTable.GetROProperty("rows")
    For rowStart = 1 to rowTotal
        For colStart = 1 to colTotal
            sData = oTable.GetCellData(rowStart,colStart)
            If sData = sText Then
                getTargetRow = rowStart
                Exit Function
            End If
        Next
    Next
End Function

To call this function:
Code:
Set myTableObject = Browser("myBrowser").Page(myPage").WebTable("myTable")
msgbox getTargetRow(myTableObject, "myText", 2)