App脚本加载项事件处理程序无法正常工作

时间:2014-09-14 03:38:05

标签: google-apps-script add-on

我正在为Google电子表格创建附加组件。单击搜索按钮时似乎没有任何事情发生。如何调试这个?

我正在使用Google Apps脚本来创建插件。我正在使用QuickStart的示例,并进行了一些更改以反映我的要求。

这是我的代码

<div class='sidebar branding-below'>
  <form>
         <div class="block col-contain">
                 <div class="block form-group">
                   <div>
                     <label for="Query-txt"><b>Query Term</b></label><br/>
                     <input type="text" name="querytxt" id="Query-txt" value="" class="width-100" />
                   </div>
                   <br/>
                   <div>
                     <label for="Channel-id-dd"><b>Channel</b></label><br/>
                     <select id="Channel-id-dd" class="width-100">
                         <option value="UCpZG4Vl2tqg5cIfGMocI2Ag">CFC India</option>
                         <option value="UCPPkrC9R5ED2R2JRTaQgETw">NCCF Church</option>
                         <option value="UCL8wQnv6qB7rZtEYfY3vPtw">River of Life Christian Fellowship</option>
                     </select>
                   </div>
                   <br/>
                   <div>
                     <label for="Region-Code-dd"><b>Country Code</b></label><br/>
                     <select id="Region-Code-dd" class="width-100">
                         <option value="AU">AU - Australia</option>
                         <option value="GB">GB - United Kingdom</option>
                         <option value="US">US - USA</option>
                         <option value="UAE">UAE - United Arab Emerates</option>
                     </select>
                   </div>
                   <br/>
                   <div>
                     <label for="Safety-Type"><b>Safety</b></label><br/>
                     <select id="Safety-Type" class="width-100">
                         <option value="moderate">moderate</option>
                         <option value="strict">strict</option>
                         <option value="none">none</option>
                     </select>
                   </div>
                   <br/>
                   <div class="block" id="button-bar">
                     <button class="blue" id="run-Query">Search</button>
                   </div>
                 </div>
         </div>
  </form>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">

 $(document).ready(function (){
    $('#run-Query').click(function () {
    Logger.log("Starting");
    var query = $('#Query-txt').val();
    var channelid = $('#Channel-id-dd').val();
    var countryCode = $('#Region-Code-dd').val();
    var safety = $('#Safety-Type').val();
    google.script.run
        .withSuccessHandler(
          alert("success");
        )
        .withFailureHandler(
          alert("failure");
        )
        .withUserObject(this)
        .search_all(query, safety, channelid, countryCode);
    Logger.log("Complete");
  });
 });

</script>

1 个答案:

答案 0 :(得分:1)

我发现您的代码存在两个问题。首先,Logger.log()是一个Apps Script服务器端方法。它旨在从服务器端代码调用,例如在Code.gs文件中。您在上面写的是在最终用户的浏览器上执行的客户端JavaScript。要在此处进行登录,您可以执行与常规javascript编程相同的操作,并使用console.log()方法。这会将日志记录语句写入javascript控制台(可通过Chrome中的Ctrl + Shift + J访问)。

其次,withSuccessHandler和withFailureHandler将成功和失败场景中调用的方法作为参数,并且没有参数。您定义的方法将自动获取参数。请注意它们在快速入门中的使用方式:

...
.withSuccessHandler(
    function(returnSuccess, element) {
        element.disabled = false;
})
...

要像成功一样警告成功,你需要使用以下内容:

...
.withSuccessHandler(
    function(returnVal, element) {
        alert("Call was successful with return value: " + returnVal);
    }
)
...

使用这两种方法,您应该拥有调试代码所需的工具。

相关问题