Csvsave, dont want to write to the next line, inside a while loop

hi

i have a while loop and it do writing some data with !csvline and csvsave commands to a csv file
Now, with each repetition of the loop, the text is written inside the csv file, and with the next loop run, the text is written inside the next line. How can it be prevented? That is, only one line should be rewritten.

You must save in csv at the end of last loop this is the solution

In this case, if the macro is run several times. Lines 2.3.4 … are filled…
Is there no other solution?

The classic csvSave command is only for the standard case, which is to create a new line for each entry. But with csvSaveArray you have full control over the CSV file. This macro does what you want:

  • First we create an array
  • Then we start a times loop:
    – Then we set a certain cell to the value in line 7.
    – Then save the array to CSV - same line for every loop!

image

See also How to mark a row in a CSV line as processed? - #3 by TechSupport which uses csvSaveArray, too.

    {
      "Name": "Save2SameRow",
      "CreationDate": "2021-3-22",
      "Commands": [
        {
          "Command": "comment",
          "Target": "Read CSV file to array",
          "Value": ""
        },
        {
          "Command": "store",
          "Target": "fast",
          "Value": "!replayspeed"
        },
        {
          "Command": "comment",
          "Target": "Create an array. Later we save the content to a CSV file",
          "Value": ""
        },
        {
          "Command": "executeScript_Sandbox",
          "Target": "var arr = []; for(var row = 0; row < 5; row++){arr[row] = []; for(var col = 0; col < 3; col++){arr[row][col] = \"\";}}; return arr",
          "Value": "array1"
        },
        {
          "Command": "times",
          "Target": "3",
          "Value": ""
        },
        {
          "Command": "comment",
          "Target": "Always write in 2nd line (index=1)",
          "Value": ""
        },
        {
          "Command": "executeScript_Sandbox",
          "Target": "var newArr = ${array1}; newArr[1][0] = \"t=\"+${!times}; return newArr",
          "Value": "array1"
        },
        {
          "Command": "csvSaveArray",
          "Target": "array1",
          "Value": "test1.csv"
        },
        {
          "Command": "end",
          "Target": "",
          "Value": ""
        }
      ]
    }
1 Like