0

I am reading csv file passing it to Webpivot table but it shows error. and also if i am trying with other csv file it show me SyntaxError: missing ) after argument list.

ReferenceError: Month is not defined var csvData = Month,Expenses,Income

Here is my csv file

Month,Expenses,Income

January,339,446

February,52,571

$(document).ready(require([ "wpt/WebPivotTable", "dojo/domReady!"],function(WebPivotTable) {
    <%
    String orname = "c:/d.csv";
        File file = new File(orname);

        FileReader fin = new FileReader(file); //Read file one by one
        BufferedReader bi = new BufferedReader(fin);

        int res;

        StringBuilder sb = new StringBuilder();
        while ((res = bi.read()) != -1) {
            sb.append((char) res);
            //csv = csv + ((char) res); //Converted int to char and stored in csv
        }
        String csv = sb.toString();
        %>
        var WebPivotTable = new WebPivotTable({
                                        customOptions : {
                                            uiFlags : {

                                                menuBtn : 0,

                                                dataSourceBtn : 0

                                            },
                                        }
                                    }, "wpt-container");
                                    var csvData =<%=csv%>;
                                    WebPivotTable.setCsvRawData(csvData);

                                }));

This is my other Csv file

Name,Party,Province,Age,Gender "Mourani, Maria",BlocQuebecois,Quebec,43,Female "Sellah, Djaouida",NDP,Quebec,30,Female "St-Denis, Lise",NDP,Quebec,72,Female "Fry, Hedy",Liberal,BritishColumbia,71,Female "Turmel, Nycole",NDP,Quebec,70,Female "Sgro, Judy",Liberal,Ontario,68,Female "Raynault, Francine",NDP,Quebec,67,Female "Davidson, Patricia",Conservative,Ontario,66,Female

Mayur
  • 1,123
  • 3
  • 22
  • 38
  • You have an error message saying *"Month is not defined"*, so why the heck would you post a piece of code that has no such variable or function name ? – adeneo Jul 12 '14 at 11:17
  • the month coming from csv file – Mayur Jul 12 '14 at 11:18
  • So do you wan't a string? just do `var csvData = "<%=csv%>";` – adeneo Jul 12 '14 at 11:23
  • its not working. SyntaxError: unterminated string literal – Mayur Jul 12 '14 at 11:30
  • Then try single quotes `var csvData = '<%=csv%>'`; – adeneo Jul 12 '14 at 11:31
  • its remain same with the same error – Mayur Jul 12 '14 at 11:32
  • Then I have no idea, you have to quote the outputted string, but if you're getting errors, you probably have to escape the string or something, and I have no idea how you'd do that in Java, but there's something here -> http://stackoverflow.com/questions/18471500/how-can-i-add-escape-characters-to-a-java-string – adeneo Jul 12 '14 at 11:49

1 Answers1

1

You need to escape the text in csvData as proper javascript string.

Note that you're reading the csv file and printing to the page 'as is'. What adeneo suggested would input something like:

var csvData = "Month,Expenses,Income
January,339,446
February,52,571";

which is not valid javascript.

I'd suggest something like:

StringBuilder sb = new StringBuilder();
String line;
boolean first = true;
while ((line = bi.readLine()) != null) {
    if (!first) {
        sb.append("\n\t+ "); // to append the lines in javascript
    }
    sb.append("'"); // string quote in js
    sb.append(line.trim()); // trim to remove the original trailing newline
    sb.append("\\n'"); // output newline in js and string quote

    if (first) {
        first = false; // only once to keep track of the + symbols
    }
}
sb.append(";\n"); // terminate the string in javascript

// ... further down
var csvData =<%=sb.toString()%>;

This should output something like:

var csvData = 'Month,Expenses,Income'
    + 'January,339,446'
    + 'February,52,571';

This is obviously very prone for errors since you could have any kind of characters in your csv file that could break you javascript string, but for this example it should work. Also if you have a very large CSV your page will take a while to load until the whole CSV is rendered – behind the scenes – in your code.

schwarzmx
  • 50
  • 6
  • What's not working? Any error message? We can't help you if you don't tell us what's wrong. – schwarzmx Jul 15 '14 at 02:35
  • unterminated string literal – Mayur Jul 15 '14 at 06:11
  • I have edited my code and also added another csv file to it. and i am getting the error of SyntaxError: missing ) after argument list. – Mayur Jul 15 '14 at 06:23
  • 1
    You could see the source code for your generated page (on the browser) and see if there's a quote, semi-colon, or something else missing. Maybe you could also update your question with the generated code as well. – schwarzmx Jul 15 '14 at 18:29