简体   繁体   English

使用 JavaScript 将 CSV 文件中的数据作为数组加载到脚本中

[英]Using JavaScript to load data from CSV file into the script as array

I am working on an Adobe Illustrator JavaScript and need to load data from a CSV file on my computer as an array into the script so I can work with them later (everything Is happening on my computer, and nothing happens online/web browser.) I need every line of the text in the CSV to be separated in the array, and then I need to separate the words in the line into an array so that I have an array of arrays in the end.我正在使用 Adob​​e Illustrator JavaScript 并需要从我的计算机上的 CSV 文件中加载数据作为数组到脚本中,以便我以后可以使用它们(一切都在我的计算机上发生,在线/网络浏览器没有任何反应。)我需要将 CSV 中的每一行文本分隔到数组中,然后我需要将行中的单词分隔成一个数组,这样最后我就有了一个 arrays 的数组。 Each line has three variables which get fed into a function that has to happen for each line.每条线都有三个变量,它们被输入到 function 中,每条线都必须发生。

The error I am getting from the code below says:我从下面的代码中得到的错误说:

'Error 25: Expected:;. '错误 25:预期:;。 -> let reader = new FileReader();' -> 让阅读器 = 新的 FileReader();'

  var csv = Folder ("Path to my csv file");
  function processData(csvFile) {
      let reader = new FileReader();
      reader.readAsText(csvFile);
      reader.onload = function(event) {
        var allText = reader.result;
      };
      const allTextLinesArr = allText.toString().split(/\r\n|\n/);
      var alen = allTextLinesArr.length;
      const allTextLinesArrArr = [];

      for (var i=1; i<=alen; i++) {
        allTextLinesArrArr[i-1] = allTextLinesArr[i-1].split(",");
        }

      for (var i=1; i<=alen; i++) {
        doStuff(allTextLinesArrArr[i-1][0],allTextLinesArrArr[i-1][1],allTextLinesArrArr[i-1][2]);
        }

  }

Error 25 isn't a standard Javascript error that you'd ever see in a web browser.错误 25 不是您在 Web 浏览器中看到的标准 Javascript 错误。

Are you using something like Adobe ExtendScript perhaps?您可能在使用 Adob​​e ExtendScript 之类的东西吗? If so, perhaps update your question with exactly where this code is being used.如果是这样,也许可以使用此代码的确切使用位置来更新您的问题。

The answer however, is probably that the program that you're using has an old version of Javascript that doesn't support FileReader (which is a fairly new bit of Javascript code).然而,答案可能是您使用的程序有一个不支持 FileReader 的旧版本的 Javascript(这是一个相当新的 Javascript 代码)。

It's also worth noting that you wouldn't usually be able to access the user's file from Javascript (without the user selecting it manually).还值得注意的是,您通常无法从 Javascript 访问用户的文件(除非用户手动选择它)。 However, it's possible that the program you're using to run JS does support this.但是,您用来运行 JS 的程序可能确实支持这一点。

Here is the classic native Extendscript way to read CSV data from a file:这是从文件中读取 CSV 数据的经典原生 Extendscript 方法:

var csv_file = File('test.csv');
csv_file.open('r');
csv_file.encoding = 'utf-8';
var data = csv_file.read().split('/\r\n|\n/'); // split by lines
csv_file.close();
for (var row in data) data[row].split(','); // split all lines by comas

alert(data); // here is your 2d array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM