我想使用对话框构建按钮以使用脚本匹配结果。例如,我将美国划分为东,中,西。我有一个名为US 50的文本文件名称和缩写。当我打字" NY"或波士顿",结果将显示" East"。我怎样才能在Google Spreadsheet.Thanks中实现这一目标。
答案 0 :(得分:1)
这是脚本文件:
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;
}
您必须运行侧边栏代码才能加载它,或者您可以在onOpen()中执行此操作。希望这会有所帮助。