-4
我想用對話框構建按鈕來匹配使用腳本的結果。例如,我將美國分爲東,中,西。我有一個名爲US 50州名和縮寫的文本文件。當我鍵入「NY」或波士頓」,結果會顯示‘東’我怎樣才能在谷歌Spreadsheet.Thanks實現這一使用谷歌腳本搜索和匹配數組
我想用對話框構建按鈕來匹配使用腳本的結果。例如,我將美國分爲東,中,西。我有一個名爲US 50州名和縮寫的文本文件。當我鍵入「NY」或波士頓」,結果會顯示‘東’我怎樣才能在谷歌Spreadsheet.Thanks實現這一使用谷歌腳本搜索和匹配數組
這是腳本文件:
function findSection(state)
{
var state =(typeof(state) != 'undefined')?state: '';
var found = "No Data Provided";
if(state)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sht = ss.getSheetByName('StateSearch');
var rng = sht.getDataRange();
var rngA = rng.getValues();
found ='Not Found';
for(var i=1;i<rngA.length;i++)
{
if((state==rngA[i][0]) || (state == rngA[i][1]))
{
found = rngA[i][2];
break;
}
}
}
return found;
}
function searchSideBar()
{
var ui = HtmlService.createHtmlOutputFromFile('sectionSearchDialog').setTitle('State Search');
SpreadsheetApp.getUi().showSidebar(ui);
}
這是HTML文件側邊欄:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
State:<input type="text" id="txt1" size="30" value=""/><br />
Section:<input type="text" id="txt2" size="30" value=""/><br />
Search:<input type="button" value="Search" onClick="sectionSearch();" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
console.log('here');
function sectionSearch()
{
var v = $('#txt1').val();
google.script.run
.withSuccessHandler(updateSection)
.findSection(v);
}
function updateSection(v)
{
$('#txt2').val(v);
}
</script>
</body>
</html>
這就是電子表格「StateSearch很像: 你可以在這裏把狀態數據和第三列,如果你願意,你可以添加你的節名。添加城市是讀者的特殊練習。祝你好運。
一個簡單的方法來添加一些城市沒有廣告位置可能是這樣:
同一側邊欄代碼: 這個腳本文件:
function findSection(state)
{
var state =(typeof(state) != 'undefined')?state: '';
var found = "No Data Provided";
if(state)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sht = ss.getSheetByName('StateSearch');
var rng = sht.getDataRange();
var rngA = rng.getValues();
found ='Not Found';
for(var i=1;i<rngA.length;i++)
{
if((state==rngA[i][0]) || (state == rngA[i][1]) || (isACity(state,rngA[i][3])>-1))
{
found = rngA[i][2];
break;
}
}
}
return found;
}
function searchSideBar()
{
var ui = HtmlService.createHtmlOutputFromFile('sectionSearchDialog').setTitle('State Search');
SpreadsheetApp.getUi().showSidebar(ui);
}
function isACity(city,cities)
{
var city = (typeof(city) != 'undefined')?city:'';
var cities = (typeof(cities) != 'undefined')?cities:'';
var r = -1;
if(city && cities)
{
var citiesA = String(cities).split(',');
r = citiesA.indexOf(city);
}
return r;
}
您必須運行側欄co de加載它,或者你可以在onOpen()中做到這一點。希望這可以幫助。
你好,我輸入了這段代碼,但沒有地方可以搜索。我可以在哪裏搜索?換句話說,我必須添加按鈕來顯示調用這個腳本嗎? – HelloWorld