0

I have a script. I have exported to console.log the elevations by including in for loop. Now I need to export this console.log to a file (could be text/csv/excel).

for (var i = 0; i < elevations.length; i++) {
data.addRow(['', elevations[i].elevation]);
console.log(elevations[i].elevation);}

Could you please help? Many thanks

amplatfus
  • 17
  • 5
  • Possible duplicate of [How to export JavaScript array info to csv (on client side)?](https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side) – jmargolisvt Mar 09 '18 at 15:03

2 Answers2

0

In google chrome console you can try the function copy():

copy(elevations)

This function copy elevations value in your clipboard. Hope it's help.

Tarik Merabet
  • 70
  • 1
  • 1
  • 9
0

You want to export it to a text file from Javascript? Because you can export a console log to a text file from the browser.

But if you want to do it in Javascript, this should work:

let blob = new Blob(["test"]);
let url = URL.createObjectURL(blob);
let file = document.createElement(`a`);
file.download = `file.txt`;
file.href = url;
document.body.appendChild(file);
file.click();
file.remove();
URL.revokeObjectURL(url);

Replace "test" with what you want to be in the text file. You can concatenate all elevations in a variable and replace "test" with variable.

So, for example, you could do:

var text = "";
for (var i = 0; i < elevations.length; i++) {
    data.addRow(['', elevations[i].elevation]);
    console.log(elevations[i].elevation);
    text += elevations[i].elevation + "\n";
}

And then use text in let blob = new Blob([text]);.

rafaelgomesxyz
  • 1,265
  • 6
  • 13
  • Thank you. Could you please modify it in order to use it in my example with console.log? All the best. – amplatfus Mar 09 '18 at 15:19
  • @amplatfus You can just concatenate the elevations with a newline (assuming you want each one to be in a separate line). Added an example. – rafaelgomesxyz Mar 09 '18 at 15:24
  • Thanks @revilheart. I did but when adding let data = new Blob([text]); I got: SyntaxError: redeclaration of var data. After delete I do not have this error. Question 1: please do you know why? Question 2: in what location will be the file saved? All the best! – amplatfus Mar 09 '18 at 15:54
  • @amplatfus Ah, it's because you're already using a variable named `data`, just change the name of the variable to something else. The file will be saved to your computer (you should get a window asking you where, just as if you were downloading a regular file). Edited the answer to use `blob` instead of `data`. – rafaelgomesxyz Mar 09 '18 at 16:02