2017-06-13 55 views
1

我想搜索電子表格中的數據並將結果顯示到HTML網頁。使用Google Apps搜索電子表格腳本不返回任何數據

我的問題:沒有結果顯示在網頁上。

我的策略:

  1. 該數據庫保存在谷歌雲端硬盤中的電子表格。
  2. 網頁訪問者在文本框中輸入一個字符串並點擊搜索按鈕。
  3. Column A中搜索字符串並返回同一行的數據。
  4. 將表格的行結果數據放入網頁(同一頁)中。
function doGet() { 
 return HtmlService.createTemplateFromFile('index').evaluate(); 
    var searchString = document.getElementById('searchString').value; 
} 

function onOpen() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var menuEntries = [ {name: "Search", functionName: "onSearch"} ]; 
    ss.addMenu("Commands", menuEntries);  
} 

function onSearch() { 
    var searchString = function doGet() { 
         document.getElementById('searchString').value; 
         } 
    var sheetActive = SpreadsheetApp.openById("1MQVrQ7fmkitP-KNvv9m17Yi0ykGWgF5UjrVh5yCP8RE"); 
    var sheet = sheetActive.getSheetByName("Sheet1"); 

    var column =1; //column Index 
    var columnValues = sheet.getRange(2, column, sheet.getLastRow()).getValues(); //1st is header row 
    var searchResult = columnValues.findIndex(searchString); //Row Index - 2 

    if(searchResult != -1) 
    { 
     //searchResult + 2 is row index. 
     SpreadsheetApp.getActiveSpreadsheet().setActiveRange(sheet.getRange(searchResult + 2, 1)); 
     document.getElementById("searchResult").innerHTML = searchResult; 

    } 
} 

Array.prototype.findIndex = function(search){ 
    if(search == "") return false; 
    for (var i=0; i<this.length; i++) 
    if (this[i] == search) return i; 

    return -1; 
} 
<!DOCTYPE html> 
<html> 
  <head> 
    <base target="_top"> 
  </head> 

<body> 
<input type="text" id="searchString" name="searchString" /> 

    <script> 
      google.script.run.doGet(); 
      google.script.run.onOpen(); 
    </script> 

<a href="javascript: onSearch();">Search</a> 
<div id="searchResult"></div> 
</body> 
</html> 
+0

這是什麼在控制檯中說? –

回答

0

我認爲它SA錯字問題: 你添加一個字符串:document.getElementById("searchResult").innerHTML = "searchResult";

,並可能與您的變量remplace它:

document.getElementById("searchResult").innerHTML = searchResult; 
+0

感謝您的回答,我在運行onSearch()函數時發現了新錯誤:雖然在函數doGet中定義了searchstring,但未定義它! – user3140792

相關問題