簡體   English   中英

使用Google Apps腳本搜索電子表格不會返回任何數據

[英]Searching a spreadsheet using Google Apps Script returns no data

我想在電子表格中搜索數據並將結果顯示在HTML網頁上。

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

我的策略:

  1. 數據庫以電子表格形式保存在Google雲端硬盤中。
  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>

我認為這是一個錯字問題:你添加了一個字符串: document.getElementById("searchResult").innerHTML = "searchResult";

並且可能必須使用您的變量重新設置它:

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

暫無
暫無

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

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