簡體   English   中英

使用javascript從excel電子表格中讀取數據的最簡單方法?

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

我在Excel電子表格中列出了機場代碼,名稱和位置,如下所示:

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

我的Javascript傳遞了一個3字符的字符串,應該是航空公司代碼。 當發生這種情況時,我需要在電子表格中找到代碼並返回機場名稱和位置。

我想的是:

var code = "AUA";

console.log(getAirportInfo(code));

function getAirportInfo(code) {

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

return airportInfo;
}

日志會寫出來的地方:

Oranjestad, Aruba (AUA): Queen Beatrix International Airport

從電子表格中獲取所需數據的最簡單方法是什么?

額外信息:

  1. 電子表格包含超過17,000個條目
  2. 上面提到的功能最多可以連續調用8次
  3. 我不必使用我現在擁有的Excel電子表格
  4. 我永遠不需要使用我的代碼編輯電子表格

我確實在網上搜索,但我發現的一切都比我想要做的要復雜得多,因此很難理解我在尋找什么。

感謝您幫助我指明正確的方向。

我使用了純文本文件(csv或tsv,兩者都可以直接從Excel導出)

通過xmlhttprequest將其加載到字符串var中。 通常,瀏覽器緩存將不再需要在每個頁面加載時下載文件。

然后讓Regex根據需要解析這些值。

所有沒有使用任何第三方....如果你願意,我可以挖出代碼。

示例:您需要將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>

我最終使用shancarter.com/data_converter上的工具將我的flie轉換為JSON文件並將其鏈接到我的頁面。 現在我只是遍歷那個JSON對象來獲得我需要的東西。 這似乎是滿足我特殊需求的最簡單方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM