使用javascript從excel電子表格中讀取數據的最簡單方法? - easiest way to read data from excel spreadsheet with javascript? -开发者知识库

使用javascript從excel電子表格中讀取數據的最簡單方法? - easiest way to read data from excel spreadsheet with javascript? -开发者知识库,第1张

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): Beatrix女王國際機場

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. 這個電子表格有超過17000個條目
  3. The function alluded to above may be called up to 8 times in row
  4. 上面提到的函數可以連續調用8次。
  5. I don't have to use an Excel Spreadsheet thats just what I have now
  6. 我不需要使用Excel電子表格,這正是我現在所擁有的。
  7. I will never need to edit the spreadsheet with my code
  8. 我永遠都不需要用我的代碼編輯電子表格

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.

謝謝你給我指出正確的方向。

2 个解决方案

#1


4  

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.

然后讓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...

示例:您需要有數據。txt文件與此網頁相同,或更新路徑…

 <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>

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复