Overwrite and Reuse a CSV

Here is one way to create an array, save it as a CSV file and then reuse the CSV without having to delete the CSV manually:

{
  "Name": "2025",
  "CreationDate": "2025-3",
  "Commands": [
    {
      "Command": "store",
      "Target": "true",
      "Value": "!statusOK",
      "Description": ""
    },
    {
      "Command": "open",
      "Target": "https://www.Favorite Website.com",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "click",
      "Target": "xpath=//input[@id='txtDate']",
      "Value": "",
      "Description": ""
    },
     {
      "Command": "XClick",
      "Target": "icon.png",
      "Value": "",
      "Description": "get roster"
    },
   {
      "Command": "storeText",
      "Target": "xpath=//*[@id=\"GridView1\"]/tbody/tr[2]/td[5]",
      "Value": "a",
      "Description": " "
    },
    {
      "Command": "echo",
      "Target": "variable is ${a}",
      "Value": "purple",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var sites = {\"All is Well \":\"IFTAWHA\", \"Arlington  \":\"IFTACI\", \"Arlington   (REG)\":\"REGACIG\", \"Training Program\":\"IFTAHS\", \"Ben Barber Innovation \":\"IFTBBIA\", \"Global   Network Inc\":\"REGCGHE\", \" High School\":\"IFTCHS\", \"Integrity\":\"REGIHE\",  \"James  West\":\"IFTJLWA\", \" Program\":\"KCAL\", \"Legacy care \":\"REGLHCC\", \"OGT RTS\":\"GOTRC\", \"   care  Trainings\":\"TSDHC\", \"Springpack1\":\"EGSHCC\", \"Springpack2\":\"EGSHC2\", \"Stone \":\"TSTAC\", \"Stone \":\"EGSTAC\", \"Teamwork  \":\"GTCI\"}; return sites[${a}]",
      "Value": "siteID",
      "Description": "From Sites to Site Codes. Performed on variable a. Change variable 'sites' into 'SiteID' via variable 'a'"
    },
    {
      "Command": "echo",
      "Target": "The Site ID is ${SiteID}",
      "Value": "green",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "true",
      "Value": "!errorIgnore",
      "Description": "Enable error handling"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return [\"\"];",
      "Value": "DataArray",
      "Description": "Initialize empty array with blank first position"
    },
    {
      "Command": "times",
      "Target": "16",
      "Value": "",
      "Description": "Loop 16 times to collect all data"
    },
    {
      "Command": "storeText",
      "Target": "xpath=//*[@id=\"GridView1\"]/tbody/tr[${!times}+1]/td",
      "Value": "D1",
      "Description": "obtain list"
    },
    {
      "Command": "gotoIf_v2",
      "Target": "${!statusOK} == false",
      "Value": "EndSearch",
      "Description": "Check if we reached the end of data"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var newArr = ${DataArray}; newArr[${!times}] = ${D1}; return newArr",
      "Value": "DataArray",
      "Description": "Add new element to array at current position"
    },
    {
      "Command": "echo",
      "Target": "Result ${!times}: ${D1}",
      "Value": "teal",
      "Description": "Display current result"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": "End the times loop"
    },
    {
      "Command": "label",
      "Target": "EndSearch",
      "Value": "",
      "Description": "Label for loop exit"
    },
    {
      "Command": "echo",
      "Target": "${DataArray}",
      "Value": "purple",
      "Description": "Display final array"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var arr = ${DataArray}; return [arr];",
      "Value": "DataArray",
      "Description": "Convert to 2D array format"
    },
    {
      "Command": "csvSaveArray",
      "Target": "DataArray",
      "Value": "list.csv",
      "Description": "Save 2D array to CSV file"
    },
    {
      "Command": "csvReadArray",
      "Target": "list.csv",
      "Value": "csv",
      "Description": ""
    },
   {
      "Command": "store",
      "Target": "true",
      "Value": "!errorIgnore",
      "Description": ""
    },
    {
      "Command": "csvReadArray",
      "Target": "list.csv",
      "Value": "csv",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${csv}",
      "Value": "green",
      "Description": ""
    },
    {
      "Command": "csvReadArray",
      "Target": "list.csv",
      "Value": "csv",
      "Description": "csvReadArray | csvfile.csv | arrayname"
    },
{
      "Command": "executeScript",
      "Target": "function uniq(arr) {\n  // Flatten the array\n  var flattenedArr = [ ].concat.apply([ ], arr);\n\n  // Create an empty object to store unique values\n  var uniqueObj = {};\n\n  // Iterate over each element in the flattened array\n  for (var i = 0; i < flattenedArr.length; i++) {\n    var value = flattenedArr[i];\n\n    // Check if the value is already in the uniqueObj\n    if (!uniqueObj[value]) {\n      uniqueObj[value] = true;\n    }\n  }\n\n  // Extract the keys of uniqueObj into a new array\n  var uniqueArr = Object.keys(uniqueObj);\n\n  return uniqueArr;\n}\n\nreturn uniq(${csv})",
      "Value": "aa",
      "Description": "https://forum.ui.vision/t/remove-array-duplicate-using-javascript/12721/6"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${aa}.length - 1;",
      "Value": "elements",
      "Description": "https://forum.ui.vision/t/how-to-get-the-number-of-items-of-a-javascript-array/9755"
    },
    {
      "Command": "echo",
      "Target": "here is the number ${elements}",
      "Value": "green",
      "Description": "https://forum.ui.vision/t/how-to-get-the-number-of-items-of-a-javascript-array/9755"
    },
    {
      "Command": "selectWindow",
      "Target": "TAB=OPEN",
      "Value": "https://fs6.formsite.com/Prometric/form84/index.html",
      "Description": "open new window and go to url"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var arr = []; return arr",
      "Value": "array1",
      "Description": "creates a blank 1 x 1 array"
    },
    {
      "Command": "csvSaveArray",
      "Target": "array1",
      "Value": "list.csv",
      "Description": ""
    }
  ]
}

(note the “squares” are actually braces “[” “]” )

1 Like

Using csvSaveArray to overwrite existing CSV files a good idea. It solves the “Delete CSV” problem.