Import .CSV data into Javascript and run execute i

2019-05-22 09:41发布

问题:

I have a couple of iMacro-files, that is executed with a single javascript file.

Very basic, looks like this.

iimPlay("GoogleMacro.iim");
iimPlay("IBMMacro.iim");
iimPlay("IMDBMacro.iim");
iimPlay("AltavistaMacro.iim");
iimPlay("GametrailersMacro.iim");
iimPlay("MortalCombatMacro.iim");
iimPlay("WikipediaMacro.iim");

It is called playme.js, and works really good.

Though, I don't want to run every macro each time I launch the .js file.

I have a separate CSV-file, urldata.csv

URLINFO,URLINFO2,DATA1,DATA2
http://google.com,GOOGLE,"hello","thank you for searching" 
http://ibm.com,IBM,null,null
http://imdb.com,IMDB,null,null
http://altavista.com,ALTAVISTA,"rip","rest in peace, my friend",
http://gametrailers.com,GAMETRAILERS,null,null
http://mortalkombat.wikia.com,MORTALKOMBAT,null,null
http://wikipedia.org,WIKIPEDIA,null,null

The way I want it to work, in this case (the data above in urldata.csv), the .js file would only execute GoogleMacro.iim and AltavistaMacro.iim

The rule I am looking for: If COL3 has the value null, do not iimPlay, and check the next file in line.

The .JS code should work (I am very aware of this is just gibberish) like this:

#Import urldata.csv

Loop whole CSV {
ROW2, If COL3 = null --> go to ROW3
else iimPlay("GoogleMacro.iim");
ROW3, If COL3 = null --> go to ROW4
else iimPlay("IBMMacro.iim");
Etc..
}

I need to figure out:

  • How to import/read the data from urldata.csv to my playme.js. Apparently, jQuery is not allowed in the free version of iMacros and therefore I cannot use this.
  • How to make a function that only use iiPlay if the value on row X is something else than null.

Please help! :)

回答1:

Solved!

Changed the .csv to the following:

"http://google.com",GoogleMacro,"hello","thank you for searching" 
"http://ibm.com",IBMMacro,"",""
"http://imdb.com",IMDBMacro,"",""
"http://altavista.com,ALTAVISTAMacro","rip","rest in peace, my friend",
"http://gametrailers.com",GAMETRAILERSMacro,"",""
"http://mortalkombat.wikia.com",MORTALKOMBATMacro,"",""
"http://wikipedia.org",WIKIPEDIAMacro,"",""

And got some really good help with the .js

var load;
load =  "CODE:";
load +=  "SET !DATASOURCE urldata.csv" + "\n";
load +=  "SET !DATASOURCE_COLUMNS 4" + "\n";
load +=  "SET !DATASOURCE_LINE {{i}}" + "\n";
load +=  "SET !extract {{!col2}}" + "\n";
load +=  "ADD !extract {{!col3}}" + "\n";

var siteName = "";
var siteContent = "";

//Change 4 to the number of websites
for(i=1;i<4;i++) {

  iimSet("i",i);
  // Load data
  iimPlay(load);
  siteName = iimGetLastExtract(1);

  // Check if the website has content
  siteContent = iimGetLastExtract(2);

  if(siteName != "Website" && siteContent != "") {
     iimPlay(siteName + '.iim');
  } else {
  }

 }


回答2:

Make a GET request to get the contents of your CSV file (jQuery might help with that), then use a CSV parser (maybe https://stackoverflow.com/a/14991797/711902) to parse the file, then loop through each row, and call the function if it matches your expectation.

jQuery.get('urldata.csv', function(response) {
    var data = parseCSV(response);
    for (var i = 1; i < data.length; i++) {
        var col3 = data[2], url = data[0];
        if (col3 != 'null') {
            var site = getSiteFromUrl(url);
            iimPlay(site + 'Macro.iim');
        }
    }
});

function getSiteFromUrl(url) {
    var site = url.match(/\/\/(.*)\./)[1];
    return site[0].toUpperCase() + site.substr(1);
}