将带有 for 循环的函数的结果保存为 JavaScript 中的 csv 文件
Saving the result of a function with for-loop as csv file in JavaScript
我在 Stack Overflow 和 Google 中搜索了很多,但不幸的是没有答案。
我用 JS 写了一段代码,它给了我这样一个 "expectable and correct" 结果:
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
问题是这样的行列表太多了,我无法在Android中完全复制它,所以我想把这个列表保存为csv文件而不是在网页上显示通过命令
"document.write"
我在其他人的代码的帮助下修改了我的代码,但它不起作用。
我变成了 "undefined" 这个词,而不是 csv 文件中的数据列表:(
代码在这里:
<!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>
请帮忙!
提示:我是 JS 初学者
编辑:
在这里,我得到了导出到 csv 文件的代码,我用我的代码添加和修改了代码:
Lee Kowalkowski's answer
您好抱歉,您可以使用下面的代码也可以在 jsfiddle
中使用
脚本
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);
我在 Stack Overflow 和 Google 中搜索了很多,但不幸的是没有答案。
我用 JS 写了一段代码,它给了我这样一个 "expectable and correct" 结果:
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
问题是这样的行列表太多了,我无法在Android中完全复制它,所以我想把这个列表保存为csv文件而不是在网页上显示通过命令
"document.write"
我在其他人的代码的帮助下修改了我的代码,但它不起作用。 我变成了 "undefined" 这个词,而不是 csv 文件中的数据列表:(
代码在这里:
<!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>
请帮忙!
提示:我是 JS 初学者
编辑: 在这里,我得到了导出到 csv 文件的代码,我用我的代码添加和修改了代码:
Lee Kowalkowski's answer
您好抱歉,您可以使用下面的代码也可以在 jsfiddle
中使用脚本
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);