简体   繁体   English

Google Apps脚本:访问.evaluate()内部发送的访问变量<script> tag in HTML Page

[英]Google Apps Script: Access variable send by .evaluate() inside <script> tag in HTML Page

I have two columns in Sheet, A is College name, and B is City Name. 我在工作表中有两列,A是大学名,B是城市名。 I want to create a form, where there will be two drop downs. 我想创建一个表单,其中将有两个下拉菜单。 First is for city, second is for college. 首先是城市,其次是大学。 Then if you select city in first drop down, the second drop down should show college from that city only. 然后,如果您在第一个下拉列表中选择城市,则第二个下拉列表应仅显示该城市的大学。

The Approach I took was, 我采取的方法是
Read data from sheet and create array for cities. 从工作表中读取数据并创建城市数组。 Pass two variables, array of city and 2D array of college and city to HTML via evaluate 通过评估将两个变量(城市数组和学院和城市的二维数组)传递给HTML

 function showSidebarAddNew() { var ss=SpreadsheetApp.getActiveSpreadsheet(); var sheet=ss.getSheetByName('option'); var CollegeList=sheet.getDataRange().getValues(); var cities=[]; for(var d=0; d<CollegeList.length; d++) { if(cities.indexOf(CollegeList[d][1])==-1){cities.push(CollegeList[d][1])} } var ui = SpreadsheetApp.getUi(); var template = HtmlService.createTemplateFromFile('form'); template.cities=cities; template.CollegeList=CollegeList; ui.showSidebar(template.evaluate().setTitle('New Form')); } 

Till here it is all OK. 直到这里一切都OK。 Next in HTML Form, create two drop downs. 接下来在HTML表单中,创建两个下拉菜单。 and from City Dropdown, call onChange function that will change the drop down options of college list 然后从“城市下拉菜单”中调用onChange函数,该函数将更改大学列表的下拉选项

  <form id="form1"> <label for="city">City </label> <select name="city" size="1" id="city" onchange="UpdateRange(this.value);"> <option hidden selected></option> <?for(var c=0; c<cities.length; c++){?> <option value="<?=cities[c]?>"><?=cities[c]?></option> <?}?> </select> <label for="city">Colleges </label> <select name="city" size="1" id="college"> <option hidden selected></option> </select> <br> <input type="submit" class="btn btn-primary" value="Submit"> </form> 

Now in onChange function, I need to access variable called "CollegeList", so that I can filter the list of college from this city and create the options, but I am not able to access the variable "CollegeList" inside <Script> tag. 现在,在onChange函数中,我需要访问名为“ CollegeList”的变量,以便可以从该城市过滤大学列表并创建选项,但是我无法访问<Script>标记内的变量“ CollegeList”。

 <script type="text/javascript"> function UpdateRange(v) { //clear all options from list document.getElementById('college').options.length = 0; //add default (blank) option var optn = document.createElement("OPTION"); optn.text = ''; optn.value = ''; document.getElementById('college').options.add(optn) var thisCity=CollegeList.filter(function (dr){return dr[1]==v} for (var i=0; i<thisCity.length; i++) { var optn = document.createElement("OPTION"); optn.text = thisCity[i][0]; optn.value = thisCity[i][0]; document.getElementById('college').options.add(optn) } } </script> 

When I try to create random options without using the variable "CollegeList", the option gets created, means the way options are being created is working. 当我尝试不使用变量“ CollegeList”创建随机选项时,将创建该选项,这意味着正在创建选项。 Only the variable is not working. 只有变量不起作用。 Please help. 请帮忙。

I think you forgot to access the CollegeList variable using the scriptlet syntax <?=CollegeList?> . 我认为您忘记了使用scriptlet语法<?=CollegeList?>访问CollegeList变量。 I generally create client-side javascript variables with the same name as the server-side variables at the top of the html as follows: 我通常使用与html顶部的服务器端变量相同的名称来创建客户端javascript变量,如下所示:

<script>
  var CollegeList = <?=CollegeList?>
  var cities = <?=cities?>
</script>

You can then use cities and CollegeList in the rest of your javascript. 然后,您可以在其余的javascript中使用citiesCollegeList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM