我有一張表格,其中有大約1400行,用於跟蹤停車場的停車位。到目前爲止,在幫助下,我已經能夠打開邊欄,創建一個搜索框,並添加「搜索」和「關閉」按鈕。現在,一旦「搜索」按鈕被點擊,它看起來不會在搜索框中抓取空格號,也不會與腳本進行通信。在Google表格邊欄中創建自定義搜索
我的問題是如何得到信息的搜索框,通過B列查找匹配,抓住從該行中列BG的信息,並在下面的格式返回信息:
- B欄:colBInfo
- 柱C:colCInfo
- 柱d:colDInfo
- E欄:colEInfo
- 柱F:colFInfo
- G列:colGInfo
- 行號:rowNumInfo
我已經做了調試和基金該功能searchInfo是給錯誤:範圍的座標或尺寸無效。 (第28行,文件「側邊欄」),因爲rowWithSpaceNumber返回-1。我相信一旦SpaceNumber能夠從搜索框中獲取信息,它就會糾正這個錯誤。
Menu.gs
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Sidebar')
.addItem('Show Sidebar', 'showSidebar')
.addToUi();
}
Sidebar.gs
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Individual Space Lookup');
SpreadsheetApp.getUi()
.showSidebar(html);
SpreadsheetApp.getActiveSpreadsheet().toast('Sidebar opened', 'Status');
}
function searchInfo(theSpaceNumber) {
var ss = SpreadsheetApp.openById("1isuunf_SRThXe8C71ROKAC2JvjZ49w05MrLE2323Yqk");
Logger.log("Book name: "+ ss.getName());
var sheetWithData = ss.getSheetByName("Master Sheet");
Logger.log("Sheet name: "+ sheetWithData.getName());
var numOfRowsInSheet = sheetWithData.getLastRow();
var values = sheetWithData.getSheetValues(4, 2, numOfRowsInSheet, 1);
Logger.log("Values: "+ values);
//Convert two dimensional array to one dimensional array.
var theRowValues = [];
for (var i = 0; i < values.length; i++) {
theRowValues.push(values[i]);
};
var rowWithSpaceNumber = theRowValues.indexOf(theSpaceNumber);
Logger.log("rowWithSpaceNumber: " + theSpaceNumber);
//Get 6 columns of data: getSheetValues(startRow, startColumn, numRows, numColumns)
var colBInfo = sheetWithData.getSheetValues(rowWithSpaceNumber, 2, 1, 1);
var colCInfo = sheetWithData.getSheetValues(rowWithSpaceNumber, 3, 1, 1);
var colDInfo = sheetWithData.getSheetValues(rowWithSpaceNumber, 4, 1, 1);
var colEInfo = sheetWithData.getSheetValues(rowWithSpaceNumber, 5, 1, 1);
var colFInfo = sheetWithData.getSheetValues(rowWithSpaceNumber, 6, 1, 1);
var colGInfo = sheetWithData.getSheetValues(rowWithSpaceNumber, 7, 1, 1);
var rowNumInfo = sheetWithData.getRange(rowWithSpaceNumber, 2);
Logger.log("Row Number: " + rowWithSpaceNumber);
return app (colBInfo, colCInfo, colDInfo, colEInfo, colFInfo, colGInfo, rowNumInfo);
};
的index.html
<div>
Search Individual Space: <input type="search" id="idUserInput"/>
<BR>
<input type="button" value="Search"
onclick="getUserInput()" />
<input type="button" value="Close"
onclick="google.script.host.close()" />
<script>
function onSuccess(colBInfo, colCInfo, colDInfo, colEInfo, colFInfo, colGInfo, rowNumInfo) {
alert('Column B: ' + colBInfo
<br>'Column C: ' + colCInfo
<br>'Column D: ' + colDInfo
<br>'Column E: ' + colEInfo
<br>'Column F: ' + colFInfo
<br>'Column G: ' + colGInfo
<br>'Row Number: ' + rowNumInfo
);
}
function getUserInput() {
var theSpaceNumber = document.getElementById("idUserInput").value;
google.script.run.withSuccessHandler(onSuccess)
.searchInfo(theSpaceNumber);
};
</script>
</div>
'var values = sheetWithData.getSheetValues(4,2,numOfRowsInSheet,1);':將1更改爲最後一個列號。 – rpm 2015-02-06 21:35:18
@ user1989將1更改爲6將從工作表中獲取所有數據以搜索輸入的數據,而不是僅搜索1列。我認爲我遇到的最大問題是在搜索框中輸入的數據沒有正確地被「拔出」框並被調用。關於如何將輸入的信息傳遞給腳本其餘部分的任何建議? – 2015-02-06 21:53:56
將價值傳遞給GAS似乎沒有任何問題。這是正確的。 – rpm 2015-02-06 22:21:09