jsParse

Js Excel onPaste paser

by rastrano

HTML

<textarea id="src"></textarea><br>
<textarea id="dest"></textarea>

CSS

#src,#dest{
    width:100%;
    height: 300px;
}

JavaScript

jQuery("#src").bind("change",parseit);
jQuery("#src").bind("paste",parseit);

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);
}

function parseit(e){
 var t=e.currentTarget;
var txt=t.value;
     var dtxt="";
var arr1=txt.split("\n");
    var regione="";
    var categoria,categoriastr="";
    var count=0;
    var name,addr,tel,cat2="";
    
    scienzaCats=["Scienza e Tecnologia","Storia Naturale"];
     storiaCats=["Storia","Archeologia","Antropologia","Arte e Archeologia"];
    arteCats=["Arte"];
    jQuery.each(arr1,function(i,v){
        count++;
       console.debug(count);
        var arr2=v.split("\t");
        try{ 
                 //  console.debug(arr2[1]);
            if (arr2[0]!=undefined && arr2[0]!=null && arr2[0]!="" && arr2[1]!=null && arr2[1]!="" && arr2[1]!=undefined ){
                count=1;
               // continue;
                regione=arr2[0];
                categoria=arr2[1];
                categoria=categoria.trim();
                categoriastr=categoria;
                if (inArray(categoria,scienzaCats)){
                  categoria="39";
                }else{
                     if (inArray(categoria,storiaCats)){
                  categoria="38";
                }else{
                     if (inArray(categoria,arteCats)){
                  categoria="40";
                     }else{
                     categoria="37";
                     }
                }
                }
                
                return true; //continue;
        }
                }catch(e){}
           if (count==1){
                cat2=v.trim();
               categoria=cat2;
               categoriastr=cat2;
                if (inArray(categoria,scienzaCats)){
                  categoria="39";
         ...