Bug - Csv read loop - reads the first line of csv even though it is not within the specified loop range

Issue: When reading a CSV file, the loop command to start from row n (where n > 1) always reads the first row of the CSV file during the initial iteration.

Example: When I loop from rows 5 to 10, it always performs the action for row 1 first.

I have attached the code for the macro that reads the CSV file, and the macro that needs to loop:

{
  "Name": "read CSV",
  "CreationDate": "2024-3-24",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed",
      "Description": ""
    },
    {
      "Command": "comment",
      "Target": "The file ReadCSVTestData.csv is pre-installed with UI.Vision RPA.",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "csvRead",
      "Target": "create new ad account innodi agency.csv",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "comment",
      "Target": "Call subroutine for the actual form filling",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "run",
      "Target": "SUB/Sub_CREATE NEW AD ACC",
      "Value": "",
      "Description": ""
    }
  ]
}

{
  "Name": "Sub_CREATE NEW AD ACC",
  "CreationDate": "2024-3-24",
  "Commands": [
    {
      "Command": "store",
      "Target": "5",
      "Value": "!timeout_wait",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "${!COL1}",
      "Value": "adaccountname",
      "Description": ""
    },
    {
      "Command": "open",
      "Target": "https://google.com/",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "type",
      "Target": "id=APjFqb",
      "Value": "${!COL1}",
      "Targets": [
        "id=APjFqb",
        "name=q",
        "xpath=//*[@id=\"APjFqb\"]",
        "xpath=//textarea[@id='APjFqb']",
        "xpath=//textarea",
        "css=#APjFqb"
      ],
      "Description": ""
    },
    {
      "Command": "pause",
      "Target": "5000",
      "Value": "",
      "Description": ""
    }
  ]
}

I recommend to not use the LOOP button! Instead use e. g. a WHILE loop like in DemoCsvReadWithWhile.

To start reading at CSV file line 5 add this:

store | 5 | !csvReadLineNumber

I often need to loop through different ranges of numbers in my code. For example, sometimes I need to loop from 2 to 100, other times from 325 to 424, and the range changes frequently. To handle this, I currently use button controls to set the start and end values of the loop each time.

However, this approach is not very convenient or scalable.

Could you please suggest a better way to dynamically set the loop ranges in my code? I’m looking for a more efficient solution that doesn’t require manually changing the loop boundaries every time.

How do you (the human) decide which range to loop?

This might help: How to mark a row in a CSV line as processed?

I created a macro to fill in the values of consecutive product codes.**

Example:** SA0001, SA0002, SA0003 …

Each time the macro is executed, it will fill in from a position that I specify (due to the nature of my work, the starting or ending serial number can be any number).**

I created a CSV file containing product codes from SA0001 to SA10000 corresponding to the row number in the CSV file.**

Each time I need to fill in the product name, I just need to loop from the desired position to the end position. For example, when I loop from 100 to 225, the product names filled in will be SA0100 to SA0225.**

The loop is very simple to execute using the !TIMES command.

To skip loops and not start from loop 1 you can use the IF command and GOTOIF or GOTOLABEL.

Info about TIMES

https://ui.vision/rpa/docs/selenium-ide/times

Hello, here is a sample loop that you can actually always use.
I have taken it from a working script, you have to adapt the CSV.
In addition, everything from the CSV is packed into an array and at the end every line is deleted from the array and therefore also from the CSV file.
This way, if a loop breaks off, you don’t always have to delete everything manually from the CSV.

  {
      "Command": "csvRead",
      "Target": "1.mydata.csv",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "csvReadArray",
      "Target": "1.mydata.csv",
      "Value": "meinArray",
      "Description": ""
    },
   {
      "Command": "store",
      "Target": "${!CsvReadMaxRow}",
      "Value": "maxRows",
      "Description": "Anzahl der Zeilen speichern"
    },
    {
      "Command": "echo",
      "Target": "Anzahl Zeilen der CSV: ${maxRows}",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return parseInt(${!csvReadLineNumber}) + 1",
      "Value": "!csvReadLineNumber",
      "Description": "Increase here the numer in Target in which row the loop should startet. If the loop should start at Row 2 = +1 | If loop start in Row 3 = +2 and so on"
    },
    {
      "Command": "comment",
      "Target": "------------------ LOOP STARTS ---------------",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "label",
      "Target": "startLoop",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "csvRead",
      "Target": "1.mydata.csv",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "csvReadArray",
      "Target": "1.mydata.csv",
      "Value": "meinArray",
      "Description": ""
    },
   {
      "Command": "while",
      "Target": "${!csvReadLineNumber} <= ${maxRows}",
      "Value": "",
      "Description": "loop to the last and empty row CSV"
    },
  {
      "Command": "executeScript_Sandbox",
      "Target": "return parseInt(${!csvReadLineNumber})",
      "Value": "!csvReadLineNumber",
      "Description": "Increace Value + 1 for the next row"
    },
    {
      "Command": "comment",
      "Target": "########### WRITE ONLY HERE YOUR OWN COMMANS",
      "Value": "",
      "Description": ""
    },
{
      "Command": "executeScript_Sandbox",
      "Target": "var meinArray = ${meinArray}; meinArray.splice(1, 1); return meinArray;",
      "Value": "meinArray",
      "Description": ""
    },
    {
      "Command": "csvSaveArray",
      "Target": "meinArray",
      "Value": "1.mydata.csv",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "Row finished",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "gotoLabel",
      "Target": "startLoop",
      "Value": "",
      "Description": "jump to next row in csv"
    },
    {
      "Command": "comment",
      "Target": "----- END WHILE LOOP -----",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }

1 Like