Import .CSV data into Javascript and run execute if string contains data

只谈情不闲聊 提交于 2019-12-06 11:58:22
Liu Kang

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 {
  }

 }
Trevor Dixon

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!