javascriptcsvsahi

Randomly selecting a row for a specific column from CSV


I have a function called enterInformation(); with parameters that I want pulled in from a CSV file. The way I have my CSV file is it has about 12 columns and the first row is the TITLE/HEADER for what each column is. The way I have my script right now is it is always pulling the first line in the CSV file and I do not want that because it is the title/headers and I also want it to pick a random row when inputting text fields.

How would I go about having my function enter field information by randomly picking a value from the data set I created (also ignoring the first line if that's possible)?

Here is my snippet of where I am calling the function:

for (var $i=0; $i<$data.length; $i++) {
    var $row = $data[$i];
    try {
        enterInformation($row[0], $row[1], $row[2], $row[3]);
    } catch (e) {
        _logException(e);
    }
    break;
}

Here is my global function:

 function enterInformation($entityType, $entityName, $address1, $city) {
     _click(_link("Select Entity"));
     _click(_listItem($entityType));
     _setValue(_textbox("entityName"), $entityName);
     _setValue(_textbox("address1"), $address1);
     _setValue(_textbox("city"), $city);
     _click(_submit("Continue"));
};

Solution

  • How about picking the row you want to use for values?

    // -1 excludes the header from the count, +1 skips the header
    var rowForValues = Math.floor(Math.random() * ($data.length - 1)) + 1;
    var $row = $data[rowForValues];
    enterInformation($row[0], $row[1], $row[2], $row[3]);
    

    Or bake it into your loop, if you like.