在Google工作表中插入重音字符?

时间:2014-02-11 23:45:41

标签: javascript google-sheets gdata-api google-spreadsheet-api

我使用以下方法将数据插入Google工作表:

var row = new RowWork(null, null, {
    column1: col1,
    column2: col2,
});

this.GData.postEntry(row);

toAtom: function() {
return '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">'+
            '<gsx:col1><![CDATA['+this.col1+']]></gsx:col1>'+
            '<gsx:col2><![CDATA['+this.col2+']]></gsx:col2>'+
        '</entry>';
}

postEntry: function(row)
{
    var req = new XMLHttpRequest();
    var atom = row.toAtom();

    req.open('POST', row.editurl, false);
    req.setRequestHeader('Authorization', 'GoogleLogin auth='+ClientLogin.token);
    req.setRequestHeader('GData-Version', '3.0');
    req.setRequestHeader('Content-Type', 'application/atom+xml; charset=utf-8');
    req.setRequestHeader('Content-Length', atom.length);
    req.setRequestHeader('Connection', 'close');
    req.sendAsBinary(atom);

    if(! req.responseText.match(/^<\?xml.+/)) {
        throw new Error(req.responseText);
    }

    var parser = new DOMParser();
    var DOM = parser.parseFromString(req.responseText, "text/xml");
    row.editurl = DOM.getElementsByTagName('link')[1].getAttribute('href');
    row.etag = DOM.getElementsByTagName('entry')[0].getAttribute('gd:etag');

    return row;
},

但我插入的数据有时会带有重音字符,所以一旦插入就说:mécho我在工作表中得到m�cho。在插入之前使用alert(col2)时,我会得到正确的mécho

似乎是什么问题?

1 个答案:

答案 0 :(得分:1)

使用replace函数将文字转换为其等效的XML实体:

var atom = row.toAtom().replace(/é/g, "&#233;");

<强>参考