将ASCII解析为从js导出到Excel的纯文本

时间:2017-06-23 19:14:02

标签: javascript excel

导出电子表格时遇到一些麻烦。该工具是创建跟踪链接,一切正常,除非链接包含ascii字符。例如,someurl.com& trackingparameter%3d。

当发生这种情况时,它全部在应用程序中运行,但是当导出到电子表格时,它将显示为someurl.com& trackingparameter =。

想知道如何在导出时将这些ascii字符解析为纯文本。提前感谢您的任何帮助。

最佳,

埃里克



           <script>
    $(document).ready(function() {
        $("#btnExport").click(function(e) {
            e.preventDefault();

            var data_type = 'data:application/vnd.ms-excel';
            var table_div = document.getElementById('wrapper');
            var table_html = table_div.outerHTML.replace(/ /g, '%20');
           
            var a = document.createElement('a');
            a.href = data_type + ', ' + table_html;
            a.download = 'exported_URLS_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls';
            a.click();
        });
    });

</script>

 <script type="text/javascript">
    function createTable() {
        var deletebutton = '<button class="delete uk-button uk-button-danger" id="delete">delete</button>';
        var tbody = '';
        var descripdata = '';
        var simpleURLdata = '';
        var description = document.getElementById('description').value;
        var simpleURL = document.getElementById('simpleURL').value;
        var baseURL = document.getElementById('baseURL').value;
        var s1 = document.getElementById('s1').value;
        var s2 = document.getElementById('s2').value;
        var s3 = document.getElementById('s3').value;
        var s4 = document.getElementById('s4').value;
        var ts = document.getElementById('ts').value;
        var hdr = document.getElementById('hdr').value;
        var hdrid = document.getElementById('hdrid').value;
        var dmhdr = document.getElementById('dmhdr').value;
        var utm_source = document.getElementById('utm_source').value;
        var utm_medium = document.getElementById('utm_medium').value;
        var utm_campaign = document.getElementById('utm_campaign').value;
        var utm_category = document.getElementById('utm_category').value;

        tbody += '<td>';
        tbody += baseURL;
        if (s1 != '') {
            tbody += '&s1=';
            tbody += s1;
        }
        if (s2 != '') {
            tbody += '&s2=';
            tbody += s2;
        }
        if (s3 != '') {
            tbody += '&s3=';
            tbody += s3;
        }
        if (s4 != '') {
            tbody += '&s4=';
            tbody += s4;
        }
        if (ts != '') {
            tbody += '&ts=';
            tbody += ts;
        }
        if (hdr != '') {
            tbody += '&hdr='
            tbody += hdr;
        }
        if (dmhdr != '') {
            tbody += '&dmhdr=';
            tbody += dmhdr;
        }
        if (hdrid != '') {
            tbody += '&hdrid=';
            tbody += hdrid;
        }
        if (utm_source != '') {
            tbody += '&utm_source=';
            tbody += utm_source;
        }
        if (utm_medium != '') {
            tbody += '&utm_medium=';
            tbody += utm_medium;
        }
        if (utm_campaign != '') {
            tbody += '&utm_campaign=';
            tbody += utm_campaign;
        }
        if (utm_category != '') {
            tbody += '&utm_category=';
            tbody += utm_category;
        }
        tbody += '</td>';

        descripdata += '<td>';
        if (description != '') {
            descripdata += description;
        }
        descripdata += '</td>';

        simpleURLdata += '<td>';

        if (simpleURL != '') {
            simpleURLdata += simpleURL;
        }
        simpleURLdata += '</td>';

        var table = document.getElementById('urls');
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
        cell1.innerHTML = tbody;

        var cell2 = row.insertCell(1);
        cell2.innerHTML = descripdata;

        var cell3 = row.insertCell(2);
        cell3.innerHTML = simpleURLdata;

        var cell4 = row.insertCell(3);
        cell4.innerHTML = deletebutton;
    }

</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

如果您想将escape(), encodeURI() or encodeURIComponent()保留为%3d,则可以使用javascript原生函数%3d

有关这些问题的进一步帮助可以在When are you supposed to use escape instead of encodeURI / encodeURIComponent?

找到