简体   繁体   English

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

[英]easiest way to read data from excel spreadsheet with javascript?

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. 我的Javascript传递了一个3字符的字符串,应该是航空公司代码。 When that happens I need to find the code on the spreadsheet and return the Airport Name and Location. 当发生这种情况时,我需要在电子表格中找到代码并返回机场名称和位置。

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

What is the easiest method to get the data I need from the spreadsheet? 从电子表格中获取所需数据的最简单方法是什么?

Extra Info: 额外信息:

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

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. 感谢您帮助我指明正确的方向。

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. 通过xmlhttprequest将其加载到字符串var中。 Usually the browsers cache will stop having to download the file on each page load. 通常,浏览器缓存将不再需要在每个页面加载时下载文件。

Then have a Regex parse out the values as needed. 然后让Regex根据需要解析这些值。

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>

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. 我最终使用shancarter.com/data_converter上的工具将我的flie转换为JSON文件并将其链接到我的页面。 Now I just loop through that JSON object to get what I need. 现在我只是遍历那个JSON对象来获得我需要的东西。 This seemed like the simplest way for my particular needs. 这似乎是满足我特殊需求的最简单方式。

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

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