javascript 使用javascript从excel电子表格中读取数据的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16172572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:34:10  来源:igfitidea点击:

easiest way to read data from excel spreadsheet with javascript?

javascriptexcel

提问by Wesley Smith

I have a list of airport codes, names, and locations in an Excel Spreadsheet like the below:

我在 Excel 电子表格中有一个机场代码、名称和位置列表,如下所示:

+-------+----------------------------------------+-------------------+
|  Code |               Airport Name             |      Location     |
+-------+----------------------------------------+-------------------+
|  AUA  |   Queen Beatrix International Airport  |  Oranjestad, Aruba|
+-------+----------------------------------------+-------------------+

My Javascript is passed a 3 character string that should be an airline code. When that happens I need to find the code on the spreadsheet and return the Airport Name and Location.

我的 Javascript 被传递了一个 3 个字符的字符串,它应该是一个航空公司代码。发生这种情况时,我需要在电子表格上找到代码并返回机场名称和位置。

Im thinking something like:

我在想:

var code = "AUA";

console.log(getAirportInfo(code));

function getAirportInfo(code) {

// get information from spreadsheet
//format info (no help needed there)

return airportInfo;
}

Where the log would write out:

日志会写出的地方:

Oranjestad, Aruba (AUA): Queen Beatrix International Airport

Oranjestad, Aruba (AUA): Queen Beatrix International Airport

What is the easiest method to get the data I need from the spreadsheet?

从电子表格中获取所需数据的最简单方法是什么?

Extra Info:

额外信息:

  1. The spreadsheet has over 17,000 entries
  2. The function alluded to above may be called up to 8 times in row
  3. I don't have to use an Excel Spreadsheet thats just what I have now
  4. I will never need to edit the spreadsheet with my code
  1. 电子表格有超过 17,000 个条目
  2. 上面提到的函数最多可以连续调用 8 次
  3. 我不必使用我现在拥有的 Excel 电子表格
  4. 我永远不需要用我的代码编辑电子表格

I did search around the web but everything I could find was much more complicated than what Im trying to do so it made it hard to understand what Im looking for.

我确实在网上搜索过,但我能找到的一切都比我试图做的要复杂得多,所以很难理解我在寻找什么。

Thank you for any help pointing me in the right direction.

感谢您为我指明正确方向的任何帮助。

采纳答案by Wesley Smith

I ended up using a tool at shancarter.com/data_converter to convert my flie to a JSON file and linked that to my page. Now I just loop through that JSON object to get what I need. This seemed like the simplest way for my particular needs.

我最终使用 shancarter.com/data_converter 上的工具将我的苍蝇转换为 JSON 文件并将其链接到我的页面。现在我只是遍历那个 JSON 对象来获取我需要的东西。这似乎是满足我特定需求的最简单方法。

回答by Mesh

I've used a plain text file(csv, or tsv both of which can be exported directly from Excel)

我使用了纯文本文件(csv 或 tsv,两者都可以直接从 Excel 导出)

Loaded that into a string var via xmlhttprequest. Usually the browsers cache will stop having to download the file on each page load.

通过 xmlhttprequest 将其加载到字符串 var 中。通常浏览器缓存将不再需要在每个页面加载时下载文件。

Then have a Regex parse out the values as needed.

然后让正则表达式根据需要解析出值。

All without using any third party....I can dig the code out if you wish.

全部不使用任何第三方......如果你愿意,我可以挖出代码。

Example: you will need to have the data.txt file in the same web folder as this page, or update the paths...

示例:您需要将 data.txt 文件放在与此页面相同的 Web 文件夹中,或者更新路径...

 <html>
      <head>
        <script>

          var fileName = "data.txt";
          var data = "";

          req = new XMLHttpRequest();
          req.open("GET", fileName, false);

          req.addEventListener("readystatechange", function (e) {
            data = req.responseText ;
          });

          req.send();

          function getInfoByCode(c){
            if( data == "" ){
              return 'DataNotReady' ;
            } else {
              var rx = new RegExp( "^(" + c + ")\s+\|\s+(.+)\s+\|\s+\s+(.+)\|", 'm' ) ;

              var values = data.match(rx,'m');
              return { airport:values[2] , city:values[3] };
            }
          }

          function clickButton(){
            var e = document.getElementById("code");
            var ret = getInfoByCode(e.value);

            var res = document.getElementById("res");

            res.innerText = "Airport:" + ret.airport + " in " + ret.city;

          }

        </script>
       </head>
       <body>
        <input id="code" value="AUA">
        <button onclick="clickButton();">Find</button>
        <div id="res">
        </div>

       </body>
    </html>