如何使用javascript更新csv文件

时间:2016-05-04 04:20:06

标签: javascript

我正在使用Google Map API和CSV文件在地图上绘制数据。现在我想使用JavaScript更新CSV文件,以便绘制的点可以在地图中显示为移动对象。我使用了一个active-X对象。它仅在IE中更新CSV文件。这是JavaScript代码:

var test = [
    "2015-03-03 18:12:12.297,1,19.1618,73.002,0.0",
    "2015-03-03 18:12:12.297,2,19.158141,73.000202,0.0",
    "2015-03-03 18:12:12.297,10,19.158141,73.000201,0.0"
];

function file() {
    for(var i=0; i < test.length; i++) {
        setTimeout(function() {
            time(i);
        }, 10000);
    }
}
function time(i) {
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var fileLoc = "D:\\test.csv";
    var file = fso.OpenTextFile(fileLoc, 2, true, 0);

    file.writeline(test[i]); 
    file.Close();
    alert('File created successfully at location: ' + fileLoc);
}

我需要每10秒更新一次数据。我正在接近以CSV的形式删除现有的latlong并添加新的latlong,以便将该对象看作是移动的。

1 个答案:

答案 0 :(得分:1)

我认为根据标准,在客户端存储文件并不安全,因此不允许[1]

但是根据您的用例:

  

CSV文件以在地图上绘制数据。...我需要每10秒更新数据 ...以删除CSV中现有的latlong, 放新拉特龙

您的代码中包含这个js变量

var test = [
"2015-03-03 18:12:12.297,1,19.1618,73.002,0.0",
"2015-03-03 18:12:12.297,2,19.158141,73.000202,0.0",
"2015-03-03 18:12:12.297,10,19.158141,73.000201,0.0"];

因此,您可以尝试JS本地存储[2],也许是这样

//set in local storage
  localStorage.setItem('test','"2015-03-03 18:12:12.297,1,19.1618,73.002,0.0","2015-03-03 18:12:12.297,2,19.158141,73.000202,0.0","2015-03-03 18:12:12.297,10,19.158141,73.000201,0.0"');
//retrieve from local storage                        
    var test2= localStorage.getItem('test');

    console.log(test2);

[1] How can I create a file for storage on the client side with JavaScript?

[2] https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage