1
我想搜索電子表格中的數據並將結果顯示到HTML網頁。使用Google Apps搜索電子表格腳本不返回任何數據
我的問題:沒有結果顯示在網頁上。
我的策略:
- 該數據庫保存在谷歌雲端硬盤中的電子表格。
- 網頁訪問者在文本框中輸入一個字符串並點擊搜索按鈕。
- 在
Column A
中搜索字符串並返回同一行的數據。 - 將表格的行結果數據放入網頁(同一頁)中。
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>
這是什麼在控制檯中說? –