简体   繁体   English

google.script.run.withSuccessHandler(function) 不返回数据

[英]google.script.run.withSuccessHandler(function) not returning data

I'm working with G-suite and using "google.script.run" function.我正在使用 G-suite 并使用“google.script.run”功能。 I can't fetch or set data out of the function that get's me data to client from server...I can set data to DOM for #myId and check that but I'd like to do it on background...any idea why is this not working?我无法从从服务器获取数据到客户端的函数中获取或设置数据......我可以将数据设置为 #myId 的 DOM 并检查,但我想在后台进行......任何想法为什么这不起作用?

function Class(){
  this.data = false;

  this.getData = function(){
    google.script.run.withSuccessHandler(helper).getData();
      function helper(data){
        $('#myId').html('data'); // => works...
        this.data = data; // => does not work...
        return data; // => does not work...
      }
  }

  this.submitData = function(){
    if(this.data === false){
      alert('no data');
    }
    else{
      ...code...
    }
  }

  this.run = function(){
    this.getData();
    this.submitData(); // => always get false;
  }
}

I need to be able to set this.data with with regular data...or atleast return them from this.getData()我需要能够使用常规数据设置 this.data ......或者至少从 this.getData() 返回它们

UPDATE更新

I've got我有

this.getData = function(){
  var testOuput = 'give me test';
    google.script.run.withSuccessHandler(helper).getData();
      function helper(data){
        testOuput = 'give me something else';
      }
  return testOutput;   
}

this.run {
  alert(this.getData());
}

this.run() // => runs on button click

My output is always "give me test" It looks like helper function is not able to access GLOBAL SCOPE variables...我的输出总是“给我测试”看起来辅助函数无法访问全局范围变量......

Issue:问题:

Time difference or Async nature of google.script.run : At the time, submitData executes, this.data is false , because getData() hasn't finished executing.时差或google.script.run Async 性质:当时, submitData执行, this.datafalse ,因为getData()还没有完成执行。

                                                 ➚ Server executes `getData()` => Calls `helper(datafromServer)` after execution 
                                               ➚  
`this.run` => `this.getData()` => Server called with `google.script.run` and  returns void immediately 
                                                (Client will not wait for server/async) 
                                               ➘ `this.submitData()` called => `helper()` hasn't finished executing. `this.data` is still false

Solution:解决方案:

Call this.submitData() after helper is called.呼叫this.submitData()后, helper被调用。

Snippet:片段:

  function helper(data){
    $('#myId').html('data'); // => works...
    this.data = data; 
    this.submitData();// => works
  }

References:参考:

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

相关问题 Google Apps脚本google.script.run.withSuccessHandler失败 - Google Apps Script google.script.run.withSuccessHandler failing google.script.run.withSuccessHandler 不返回值 - google.script.run.withSuccessHandler does not return value google.script.run.withSuccessHandler 可以返回区域吗? - Can google.script.run.withSuccessHandler return area? 有什么方法可以简化 google.script.run.withSuccessHandler 中来自 code.gs 的各种函数调用吗? Google Apps 脚本 - Is there any way to simplify various function calls from code.gs in a google.script.run.withSuccessHandler? Google Apps Script google.script.run.withSuccessHandler(onSuccess)。 onSuccess在构造函数中不起作用 - google.script.run.withSuccessHandler(onSuccess). onSuccess doesn't works inside constructor 如何结合谷歌应用程序脚本processForm,withSuccessHandler和withUserObject - how to combine google app script processForm, withSuccessHandler and withUserObject getSheetValues不返回数据Google脚本 - getSheetValues not returning data Google Script Google Sheet 上的脚本以运行 Function - Script on Google Sheet to Run Function google.script.run 不返回字符串 - google.script.run not returning string Google App脚本-函数返回未定义 - Google App Script - Function Returning Undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM