1

I searched a lot here in Stack Overflow and in Google but unfortunately no answer.

I wrote a code in JS, it give me such a "expectable and correct" result:

W,52.xxxxx,10.1
W,52.xxxxx,10.2
W,52.xxxxx,10.3
W,52.yyyyy,10.1
W,52.yyyyy,10.2
W,52.yyyyy,10.3

The problem is that there is a too large list of such rows, I can't copy it fully in Android, so I would to save this list as csv file instead to display it on the webpage via the command

"document.write"

I modified my code with the help of other peoples' codes but it doesnt work. I become the word "undefined" instead of the list of data that would be inside the csv file :(

Code is here:

<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>

    </body>
    <script>
        function convertTo()
{

    var lon,lat;

    for(lat = 52 + (1/3600) ; lat <= 52 + (3/3600) ; lat=lat+(1/3600))

    {

       for(lon = 9 + (1/3600) ; lon<= 9 + (4/3600) ; lon=lon+(1/3600))
           {

      document.write("W,"+lat+","+lon+"<br>")

           }
    }

};

var CSV = convertTo();

window.URL = window.webkitURL || window.URL;

var contentType = 'text/csv';

var csvFile = new Blob([CSV], {type: contentType});

var a = document.createElement('a');

a.download = 'my.csv';

a.href = window.URL.createObjectURL(csvFile);

a.textContent = 'Download as CSV';

a.dataset.downloadurl = [contentType,

a.download, a.href].join(':');

document.body.appendChild(a);


    </script>
</html>

Any help please!

Hint: I am beginner with JS

Edit: Here I got the code the export into csv file the I added and modified with my code:

Lee Kowalkowski's answer

3
  • First problem is you write on document try to get in CSV variable, you should use variable instead of document.write... and use return Commented Oct 23, 2017 at 16:54
  • @Ferhat so I should delete document.write("W,"+lat+","+lon+"<br>") then write return("W,"+lat+","+lon+"<br>") ?!! Commented Oct 23, 2017 at 17:01
  • var lon, lat, str str+=...... End of function return str Commented Oct 23, 2017 at 17:04

1 Answer 1

2

Hi Sorry you can use below code also working code in jsfiddle

Script

function convertTo()
{
var lon,lat;
var str ='';
for(lat = 52 + (1/3600) ; lat <= 52 + (3/3600) ; lat=lat+(1/3600))
{
   for(lon = 9 + (1/3600) ; lon<= 9 + (4/3600) ; lon=lon+(1/3600))
       {
  str+="W;"+lat+";"+lon+"\n";
       }
}
return str;
};
var CSV = convertTo();
window.URL = window.webkitURL || window.URL;
var contentType = 'text/csv';
var csvFile = new Blob([CSV], {type: contentType});
var a = document.createElement('a');
a.download = 'my.csv';
a.href = window.URL.createObjectURL(csvFile);
a.textContent = 'Download as CSV';
a.dataset.downloadurl = [contentType,
a.download, a.href].join(':');
document.body.appendChild(a);
Sign up to request clarification or add additional context in comments.

7 Comments

thank you very much, could you help me more?!! I modified my code so that I can direct input my values in html, but code doesn't functionate!!! Why?!! Global variables must be read from function!! Please there is my code: jsfiddle.net/4473vjbw
This "confirm" button is optional, I put it just to check the inputed values
Yes but i am not available now you can ask your question when i am available then can help you
Thank you, would it be better if I post my problem as new question ?!
No it is not necessery that asking as new question
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.