使用JavaScript生成CSV工作,但我得到一个空的尾列 [英] Generating a CSV with JavaScript works but I am getting an empty trailing column

查看:144
本文介绍了使用JavaScript生成CSV工作,但我得到一个空的尾列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些脚本,将分页(每个ajax请求20张照片)从instagram json feeds转换为csv,以便轻松地将照片网址存储在我们的数据库中。我们的CMS可以自动将CSV文件转换为SQl文件,通过替换表或附加到它。问题是它只有在所有的列都是相同的。

I've written some scripts to convert the pagination (20 photos per ajax request) from instagram json feeds to csv for easily storing the photo urls in our database. Our CMS is automatically able to convert CSV files into SQl files either by replacing the table or by appending to it. The problem is it will only work if ALL of the columns are the same.

它接近完全工作,但我不能导入我生成的csvs,因为他们不断得到空列,它应该换行到一个新行,因为最终CSV输出包含逗号+换行符,当它应该只返回换行符(即没有一个结尾逗号)。

It's close to totally working but I can't import my generated csvs because they keep getting an empty column where it should be line breaking to a new row because the final CSV output contains a comma + line break when it should only be returning the line break (i.e. without a trailing comma).

编码为UTF-8,换行符使用\\\
添加。我试过控制台日志记录的过程中的每一步,似乎有

Encoding is UTF-8 and line breaks are being added using "\n". I've tried console logging just about every step of the process and it seems that there that

这是一个图片我生成的CSV之一: http://screencast.com/t/dZfqN08A

Here's a picture of one of the CSVs I am generating: http://screencast.com/t/dZfqN08A

以下是所有相关代码:

首先我使用ajax和jsonp回调来加载基于主题标签的照片。

First I'm using ajax with a jsonp callback to load instagram photos based on a hashtag.

照片以如下方式加载:

function loadNext(nextUrl)  {

    $.ajax({
        url: url,
        cache: false,
        type: 'POST',
        dataType: "jsonp",
        success: function(object) {
            console.log('loadmore');
            if (object) {
                console.log(object);
                $('.loadmore').fadeOut(500); 

                // chargement photos gallerie 
                $.each( object.data, function(home, photo) {
                    photo = '<div class="photo photo-load">' + 
                        '<img class="pull-me" src="' + photo.images.low_resolution.url + '" height="380px" width="380px" alt="photo">' +
                        '<div class="dot"></div>' + 
                        '<div class="share" >' +
                             '<!-- AddThis Button BEGIN -->' +
                                '<div class="addthis_toolbox addthis_default_style addthis_16x16_style">' +
                                    '<a class="addthis_button_twitter"></a>' +
                                    '<a class="addthis_button_facebook"></a>' +
                                '</div>' +
                             '<!-- AddThis Button END -->' +
                        '</div>' +
                        '<div class="text-photo">' + 
                          '<div class="svg line w-line"></div>' + 
                          '<h4 class="left">'+ photo.user.username + '</h4>' + 
                          '<h4 class="right share-photo">PARTAGE</h4>' + 
                        '</div>' +
                        '<div class="vote w-path-hover">' +
                          '<div class="fb-like" data-href="http://dev.kngfu.com/maurice/" data-layout="box_count" data-action="like" data-show-faces="false" data-share="false"></div>' +
                          '<div class="insta-like">' +
                            '<div class="count-box">' +
                              '<p>'+ photo.likes.count + '</p>' +
                            '</div>' +
                            '<a class="insta-button" title="Pour appuyer votre proposition préférée, rendez-vous sur Instagram." href="http://instagram.com/" ><i class="fa fa-instagram"></i>J aime</a>' +
                        '</div> ' + 
                        '<div class="w-path"></div>' +
                            '<div class="base-cross"></div>' +
                            '<h4 class="vote-button">VOTE</h4>' +
                        '</div>' +
                        '</div>';
                        $(photo).appendTo( $( ".gallery" ) );

                    });

                    url = object.pagination.next_url;
                    console.log(url);

                } else {
                    console.log("error");
                }
        } // end success func.
    });
}

然后在单独的ajax调用中,我可以将同一个json feed转换为csv文件使用此函数(此函数也调用一些其他函数,因此依赖函数包含在ajax调用下面):

Then in a separate ajax call I can convert the same json feed to a csv file using this function (this function also calls a couple other functions so the dependent functions are included below the ajax call):

function convertJSON (nextUrl) {
    $.ajax({
        url: url,
        cache: false,
        type: 'POST',
        dataType: "jsonp",
        success: function(object) {
            if (object) {
                console.log(object);

                var fromJSON = new Array();

                i = 0;
                $.each( object.data, function(home, photo) {
                    i++;

                    var photopath = photo.images.low_resolution.url;

                    var postID = photo.id;
                    var userID = photo.user.id;
                    var user = photo.user.username;

                    // watch out for those wild fullnames in instagram json
                    var fullname = photo.user.full_name;
                    fullname = fullname.replace(/[^a-z0-9]+|\s+/gmi, " ");
                    //console.log(fullname);

                    var likes = photo.likes.count;
                    var winner = 0;
                    var winnerplace = " ";
                    var campaign = "maurice1";
                    var timestamp = photo.created_time;

                    // easydate field formatting
                    var date = new Date();
                    date.setSeconds( timestamp );

                    var photodeleted = 0;

                    // add new rows to csv
                    var linebreak = "\n";

                    var arrayFromJSON = new Array(  linebreak+photopath, 
                                                postID, 
                                                userID, 
                                                user, 
                                                fullname, 
                                                likes, 
                                                winner, 
                                                winnerplace, 
                                                campaign, 
                                                timestamp, 
                                                date, 
                                                photodeleted );

                    fromJSON[i] = arrayFromJSON.join();
                });

                //url = object.pagination.next_url;
                //console.log(url);

                //console.log( fromJSON );

                makeCSV( fromJSON );

                } else {
                    console.log("error");
                }
        } // end success func.
    });
}

// json to csv converter
function makeCSV (JSONData) {

    //console.log("makeCSV() function was started");

    var data = encodeURIComponent(JSONData);

    var currentTime = new Date().getTime();
    var date = getDate( currentTime );

    //console.log(JSONData);

    var fileName = date;

    var uri = "data:text/csv;charset=utf-8," // sets mime/data type
            + "photopath," // now 12 strings which are the CSV's column titles
            + "postid,"
            + "userid," 
            + "username," 
            + "fullname," 
            + "likes," 
            + "winner," 
            + "winnerplace," 
            + "campaign," 
            + "creationdate,"  
            + "easydate," 
            + "photodeleted"
            + data;  // finally append our URI encoded data

    console.log(uri);   

    // generate a temp <a /> tag that will auto start our download when the function is called
    var link = document.createElement("a");
    link.id = new Date().getTime();
    link.href = uri;

    // link visibility hidden
    link.style = "visibility:hidden";
    link.download = fileName + ".csv";

    // append anchor tag and click
    $("div#hidden").append(link);
    link.click();
    //document.body.removeChild(link);
}

// this function just makes human readable dates for CSV filename and id of our link tag
function getDate() {
    var date = new Date();
    //zero-pad a single zero if needed
    var zp = function (val){
        return (val <= 9 ? '0' + val : '' + val);
    }

    //zero-pad up to two zeroes if needed
    var zp2 = function(val){
        return val <= 99? (val <=9? '00' + val : '0' + val) : ('' + val ) ;
    }

    var d = date.getDate();
    var m = date.getMonth() + 1;
    var y = date.getFullYear();
    var h = date.getHours();
    var min = date.getMinutes();
    var s = date.getSeconds();
    var ms = date.getMilliseconds();
    return '' + y + '-' + zp(m) + '-' + zp(d) + ' ' + zp(h) + 'h' + zp(min) + 'm' + zp(s) + 's';
}

从我所做的所有控制台记录,我得到没有结尾逗号,直到最后一步,json数组数据获得URI编码。

From all the console logging I've done, I can definitely assure you that I'm getting no trailing comma until the final step where the json array data gets URI encoded.

由于这个额外的列也包含在标题行中,我想知道它是否与这一行有关。

var uri = "data:text/csv;charset=utf-8," // sets mime/data type

我也试过ISO-8859-1编码,但我得到相同的结果。

I've also tried ISO-8859-1 encoding but I get the same result.

有没有人知道为什么会发生这种情况?任何帮助将不胜感激

推荐答案

您传递一个数组到 encodeURIComponent

Your passing an array of lines to encodeURIComponent. That will stringify the array, joining it with a comma - which you don't want.

你应该做的是

                var arrayFromJSON = new Array( photopath, 
// remove linebreak here                      ^
                                            postID, 
                                            userID, 
                                            user, 
                                            fullname, 
                                            likes, 
                                            winner, 
                                            winnerplace, 
                                            campaign, 
                                            timestamp, 
                                            date, 
                                            photodeleted );

                fromJSON[i] = arrayFromJSON.join(",");
// make comma explicit:                          ^^^
            …
            makeCSV( fromJSON );
…

    var data = encodeURIComponent(JSONData.join("\n"));
// join the lines by a linebreak:               ^^^^
    …
    var uri = "data:text/csv;charset=utf-8," // sets mime/data type
        + "photopath," // now 12 strings which are the CSV's column titles
        + "postid,"
        + "userid," 
        + "username," 
        + "fullname," 
        + "likes," 
        + "winner," 
        + "winnerplace," 
        + "campaign," 
        + "creationdate,"  
        + "easydate," 
        + "photodeleted"
        + "\n"
//      ^^^^^^ add linebreak
        + data;  // finally append our URI encoded data

这篇关于使用JavaScript生成CSV工作,但我得到一个空的尾列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆