Forum Discussion

David91's avatar
David91
Frequent Contributor
4 years ago
Solved

Variable table findrow method

Please i need help with create variable table method findrow.. i found in TC community code:

 

def search_in_table(name):
table = Project.Variables.yourTableVar
table.Reset() counter = 1 #consider that first row contains column names while not table.isEOF(): counter += 1 if table.Value["Name"] == name: Log.Message(counter) table.Next()

I need to rewrite it in a jscript or javascript. I tried it but it made mistakes.. 😞

 

My rewrite code with bugs..

 

function search_in_table(name){
var table = KeywordTests.Cenotvorba.Variables.DOKL_SLEVA_1;

 

table.reset(); // error
var counter = 1;

while (!table.isEOF()) { // error
counter += 1
if (table.Value["Name"] == name) //error
{
Log.Message(counter)
table.Next()
}
}

}

  • Okay so I dont know what table.reset does I've never really used it. What I would do in your case is ( If you have your column number fixed which is not a great idea btw)

     

    function FindDavid(){
    table = Project.Variables.yourTableVar

    for(var i=0; i<table.RowCount; i++){

    var cell = table.cell(i,0) //whatever your column number is

    if(cell.contentText == "David"){
    return i;
    }

    }
    }

     

    Try this and let me know.

5 Replies

  • Okay so I dont know what table.reset does I've never really used it. What I would do in your case is ( If you have your column number fixed which is not a great idea btw)

     

    function FindDavid(){
    table = Project.Variables.yourTableVar

    for(var i=0; i<table.RowCount; i++){

    var cell = table.cell(i,0) //whatever your column number is

    if(cell.contentText == "David"){
    return i;
    }

    }
    }

     

    Try this and let me know.

    • BenoitB's avatar
      BenoitB
      Community Hero

      As using the word Table instead of Array i suspect is about DB table (so the reset and the isEOF).

      Do you want to search in DB table or in array ?

       

      On array, you have native Javascript functions that does all the job:

       

      function TestFind() {
        const jungle = [ 
                         { name: "frog", threat: 0 },
                         { name: "monkey", threat: 5 },
                         { name: "gorilla", threat: 8 },
                         { name: "lion", threat: 10 }
                       ];
      
        // break the object down in order to use .includes() or .indexOf()
        const names = jungle.map(el => el.name); // returns ['frog', 'monkey', 'gorilla', 'lion']
        Log.Message(names.includes("gorilla")); // returns true
        Log.Message(names.indexOf("lion")); // returns 3 - which corresponds correctly assuming no sorting was done
        
        // methods we can do on the array of objects
        Log.Message(jungle.find(el => el.threat == 5)); // returns object - {name: "monkey", threat: 5}
        Log.Message(jungle.filter(el => el.threat > 5)); // returns array - [{name: "gorilla", threat: 8}, {name: 'lion', threat: 10}]
      }
      

       

      It comes from this very good article here:

      https://alligator.io/js/array-search-methods/

       

       

       

      • sonya_m's avatar
        sonya_m
        SmartBear Alumni (Retired)

        Awesome job Benoit, Reshail! Thank you for the efforts!

         

        David91 Please mark the best answer as a solution or let us know if you need more help with this 🙂

  • Hi,

    Could you link where exactly you got that snippet from so I could read it before commenting on it. Also, if you could share what error you're getting at those particular lines. Seems like your table variable has an issue. 

    Thanks