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

1

2 Answers 2

1

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]);.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Could you please modify it in order to use it in my example with console.log? All the best.
@amplatfus You can just concatenate the elevations with a newline (assuming you want each one to be in a separate line). Added an example.
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 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.
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.

2 Comments

Thank you. i tried but I dot below error:ReferenceError: copy is not defined [Learn More]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.