问题描述:

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! :)

网友答案:

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";

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

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

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

    iimSet("i",i);
    // Load website
    iimPlay(load);
    siteName = iimGetLastExtract(0);

    iimSet("i",i);
    // Check if the website has content
    iimPlay(loadContent);
    siteContent = iimGetLastExtract(0);

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

}
网友答案:

Make a GET request to get the contents of your CSV file (jQuery might help with that), then use a CSV parser (maybe http://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);
}
相关阅读:
Top