Google App Script无法解释javascript

时间:2015-03-02 02:25:11

标签: google-apps-script

我正在尝试根据另一个列表框中的选择填充列表框。它可以在Apache服务器上加载的简单html / js页面上正常工作,但是当我尝试将其放入Google App Script中的html服务时,只会出现第一个列表框:第二个框中没有任何内容。我觉得我在GAS中缺少一些基本概念。

在code.gs中我有:

function hardCode() {
  var html = HtmlService.createHtmlOutputFromFile('hardcode.html')
      .setWidth(600).setHeight(425);
  SpreadsheetApp.getUi().showModalDialog(html, 'Why doesn't this work');
}

然后在html方面我有:

<form name="classic">
    <select name="countries" size="4" onChange="updatecities(this.selectedIndex)" style="width: 150px">
        <option selected>Select A City</option>
        <option value="usa">USA</option>
        <option value="canada">Canada</option>
        <option value="uk">United Kingdom</option>
    </select>

     <select name="cities" size="4" style="width: 150px" onClick="alert(this.options[this.options.selectedIndex].value)">
     </select>
     </form>

     <script type="text/javascript">

       var countrieslist=document.classic.countries
       var citieslist=document.classic.cities
       var cities=new Array()
       cities[0]=""
       cities[1]=["New York|newyorkvalue", "Los Angeles|loangelesvalue", "Chicago|chicagovalue", "Houston|houstonvalue", "Austin|austinvalue"]
       cities[2]=["Vancouver|vancouvervalue", "Tonronto|torontovalue", "Montreal|montrealvalue", "Calgary|calgaryvalue"]
       cities[3]=["London|londonvalue", "Glasgow|glasgowsvalue", "Manchester|manchestervalue", "Edinburgh|edinburghvalue",
 "Birmingham|birminghamvalue"]

       function updatecities(selectedcitygroup){
         citieslist.options.length=0
         if (selectedcitygroup>0){
           for (i=0; i<cities[selectedcitygroup].length; i++)
             citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0],
             cities[selectedcitygroup][i].split("|")[1])
           }
         }
     </script>

1 个答案:

答案 0 :(得分:1)

HtmlService的标准模式是NATIVEEMULATED(后者由旧浏览器触发)。每个都使用Caja进行安全性预解析,这可能会破坏某些功能。

如果您将沙箱更改为IFRAME,则应允许您的代码正常运行。

function hardCode() {
  var html = HtmlService.createHtmlOutputFromFile('hardcode.html')
    .setWidth(600).setHeight(425)
    .setSandboxMode(HtmlService.SandboxMode.IFRAME); // ADD THIS LINE
  SpreadsheetApp.getUi().showModalDialog(html, 'Why doesn't this work');
}

买者 请记住IFRAME沙盒模式虽然它带来了更多功能,但它支持的浏览器范围却更低。

随着时间的推移,对该模式的支持很可能会得到扩展。