【问题标题】:Google App Script not interpreting javascript谷歌应用脚​​本不解释 javascript
【发布时间】:2015-03-02 02:25:10
【问题描述】:

我正在尝试根据来自另一个列表框的选择来填充列表框。它在 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>

【问题讨论】:

  • 尝试将.setSandboxMode(HtmlService.SandboxMode.IFRAME) 添加到 HtmlService。
  • 另一件事,当我进入 javascript 控制台时,我收到此错误:未捕获的类型错误:无法读取未定义的属性“国家”。
  • 成功了!抱歉,我错误地插入了您的代码 sn-p。根据 GAS 开发者网站的说法,这种沙盒模式与旧版浏览器的向后兼容性较差,但提供了更多的 HTML5 约定。再次感谢您的帮助!

标签: google-apps-script


【解决方案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 沙盒模式虽然带来了更多功能,但支持的浏览器范围更小。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多