JavaScript - Unicode UTF-8 en JS, problemas con Tildes

 
Vista:
sin imagen de perfil

Unicode UTF-8 en JS, problemas con Tildes

Publicado por leandro (1 intervención) el 06/09/2016 22:47:19
Estoy usando Selenium IDE para leer un archivo CSV con la extensión "CSVRead_Selenium-IDE.js" y cargar de forma automática algunos valores.

Pero note que no me lee de las celdas los valores que contengan tildes "í, ó, ú", no conozco nada de JS, pero creo que una opcion logica seria colocar un "IF" que transforme con el codigo UTF-8, quiero decir:

1
2
3
IF read.celda= í {
imprime="\u00ED"          <-- Codigo unicode
}

De esta manera cuando la extensión lea del archivo .CSV me cambie a código unicode ese carácter y me permita leerlo. Alguien podria darme ideas de como agregar un condicional asi en JS?

Dejo el condigo de la extensión que estoy usando. Y una imagen con el Error:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// global array
 
 
    array = new Array();
 
        Selenium.prototype.doStoreCellValue = function(variableName,cellPosition)
    {
 
        var xy = cellPosition.split(",");
        var x = xy[0] - 1;
        var y = xy[1] - 1;
        var value = array[x][y];
        storedVars[variableName] = value;
 
    }
 
    // ================================================================================
    Selenium.prototype.doReadCSV = function(xmlpath)
    {
        var filedata = null;
        loader = new FileReader();
        var xmlHttpReq = loader.getIncludeDocumentBySynchronRequest(xmlpath);
        LOG.info("Reading from: " + xmlpath);,
        filedata = xmlHttpReq.responseText; //CSV Doc data
        array = CSVToArray (filedata);
 
    }
    // ==================== File Reader ====================
 
 
    function FileReader() {}
 
    FileReader.prototype.getIncludeDocumentBySynchronRequest = function(includeUrl) {
 
        var requester = this.newXMLHttpRequest();
        if (!requester) {
            throw new Error("XMLHttp requester object not initialized");
        }
        requester.open("GET", includeUrl, false); // synchron mode ! (we don't want selenium to go ahead)
        try {
            requester.send(null);
        } catch(e) {
          throw new Error("Error while fetching includeUrl '" + includeUrl + "' details: " + e);
        }
        if ( requester.status != 200 && requester.status !== 0 ) {
            throw new Error("Error while fetching " + includeUrl + " server response has status = " + requester.status + ", " + requester.statusText );
        }
        return requester;
    };
 
    FileReader.prototype.newXMLHttpRequest = function() {
        var requester = 0;
        var exception = '';
        try {
            // for IE/ActiveX
            if(window.ActiveXObject) {
                try {
                    requester = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e) {
                    requester = new ActiveXObject("Microsoft.XMLHTTP");
                }
            }
            // Native XMLHttp
            else if(window.XMLHttpRequest) {
                requester = new XMLHttpRequest();
            }
        }
        catch(e) {
            throw new Error("Your browser has to support XMLHttpRequest in order to use include \n" + e);
        }
        return requester;
    };
 
//==============CSV to Array ===================
 
    function CSVToArray( strData, strDelimiter ){
        strDelimiter = (strDelimiter || ",");
        var objPattern = new RegExp(
            (
                "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
                "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
                "([^\"\\" + strDelimiter + "\\r\\n]*))"
            ),
            "gi"
            );
        var arrData = [[]];
        var arrMatches = null;
        while (arrMatches = objPattern.exec( strData )){
            var strMatchedDelimiter = arrMatches[ 1 ];
            if (
                strMatchedDelimiter.length &&
                strMatchedDelimiter !== strDelimiter
                ){
                arrData.push( [] );
            }

            var strMatchedValue;
            if (arrMatches[ 2 ]){

                strMatchedValue = arrMatches[ 2 ].replace(
                    new RegExp( "\"\"", "g" ),
                    "\""
                    );

            } else {
                strMatchedValue = arrMatches[ 3 ];
            }
            arrData[ arrData.length - 1 ].push( strMatchedValue );
        }
        return( arrData );
    }


errorREAD
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder