Google Apps脚本成功处理程序在功能完全执行之前运行 [英] Google Apps Script Success Handler Running before function is fully executed

查看:153
本文介绍了Google Apps脚本成功处理程序在功能完全执行之前运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google Apps Script中通过html服务显示对话框。在对话框中,我调用了一个谷歌脚本函数( openFormSidebar ),该函数打开一个带有成功处理程序的侧栏以关闭对话框。



HTML中的对话框

 <按钮
onclick =google。 script.run.withSuccessHandler(google.script.host.close())openFormSidebar()>。
创建表单< / button>

问题在于打开边栏之前对话框正在关闭(成功处理程序正在运行)。

gs code

  function openFormSidebar (){
var html = HtmlService.createHtmlOutputFromFile('FormSidebar')
.setTitle('Form Creation')
.setWidth(300)
.setSandboxMode(HtmlService.SandboxMode。 IFRAME);
SpreadsheetApp.getUi()//或DocumentApp或FormApp。
.showSidebar(html);

$ / code>

即使我制作了 openFormSidebar 函数一个简单的记录器语句(即 Logger.log('test'))它不会执行(没有记录)。

我错过了什么?为什么发生这种情况?

解决方案

google.script.run.withSuccessHandler(google.script.host。 close())。openFormSidebar() 调用 google.script.host.close()然后传递它的返回值值转换为 google.script.run.withSuccessHandler ,正如 foo(bar()); 调用的方式然后将其返回值传递给 foo


$ b $ b

您需要传入函数引用:

  google.script.run.withSuccessHandler( function(){google.script.host.close()})。openFormSidebar()`

或可能

  google.script.run.withSuccessHandler(google.script.host.close.bind(google.script.host)) .openFormSidebar()`

函数#bind 返回一个函数,该函数在调用时使用给定的 this 值调用原始函数。因此, google.script.host.close.bind(google.script.host)会创建一个函数,该函数在调用时会调用 google.script。 host.close 设为 google.script.host


I have a dialog I'm displaying via the html service in Google Apps Script. From the dialog, I call a google script function (openFormSidebar) that opens a sidebar with a success handler to close the dialog.

HTML in Dialog

<button
onclick="google.script.run.withSuccessHandler(google.script.host.close()).openFormSidebar()">
Create Form</button>

The problem is that the dialog is closing (the success handler is running) before the sidebar is opened.

gs code

function openFormSidebar () {
  var html = HtmlService.createHtmlOutputFromFile('FormSidebar')
         .setTitle('Form Creation')
         .setWidth(300)
         .setSandboxMode(HtmlService.SandboxMode.IFRAME);
     SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
     .showSidebar(html);
}

Even if I make the openFormSidebar function a simple logger statement (i.e. Logger.log('test')) it doesn't execute (nothing is logged).

What am I missing? Why is this occuring?

解决方案

google.script.run.withSuccessHandler(google.script.host.close()).openFormSidebar() calls google.script.host.close() and then passes its return value into google.script.run.withSuccessHandler, exactly the way foo(bar()); calls bar and then passes its return value in to foo.

You'll want to pass in a function reference instead:

google.script.run.withSuccessHandler(function() { google.script.host.close() }).openFormSidebar()`

or possibly

google.script.run.withSuccessHandler(google.script.host.close.bind(google.script.host)).openFormSidebar()`

Function#bind returns a function that, when called, calls the original with a given this value. So google.script.host.close.bind(google.script.host) creates a function that, when called, will call google.script.host.close with this set to google.script.host.

这篇关于Google Apps脚本成功处理程序在功能完全执行之前运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆