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

Javascript google.script.run.withSuccessHandler(函数)不返回数据,javascript,google-apps-script,google-workspace,Javascript,Google Apps Script,Google Workspace,我正在使用G-suite并使用“google.script.run”功能。我不能从从服务器上获取或设置数据到客户端的函数中获取或设置数据…我可以为#myId将数据设置为DOM并进行检查,但我想在后台执行此操作…知道这为什么不起作用吗 function Class(){ this.data = false; this.getData = function(){ google.script.run.withSuccessHandler(helper).getData();

我正在使用G-suite并使用“google.script.run”功能。我不能从从服务器上获取或设置数据到客户端的函数中获取或设置数据…我可以为#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;
  }
}
我需要能够将此.data设置为常规数据…或至少从此.getData()返回它们

更新

我有

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
我的输出总是“给我测试”,看起来助手函数无法访问全局范围变量…

问题:
google.script.run
的时差或异步性质:在执行
submitData
时,
this.data
false
,因为
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
解决方案: 调用
this.submitData()
后调用
helper

片段: 参考资料:

如果您
console.log(this)
在helper函数中,输出是什么?它可能指向另一个对象,因此请尝试以下操作:
google.script.run.withSuccessHandler(helper.bind(this)).getData()您需要等到数据被检索出来。@Reyno-nope没有帮助…我的输出总是“给我测试”您是如何获得输出的?@TheMaster-Updated我理解您的意思,但事实并非如此……也许是我的错,我让函数同时运行这两个函数,但实际上我在至少一分钟前运行函数getData来读取它们,事实上我可以在屏幕上看到它们被保存在DOM中……用户使用这些数据在客户端站点上进行操作也许一分钟后,我运行提交功能…@Orbit您能编辑您的问题以提供一个完整的答案吗?
  function helper(data){
    $('#myId').html('data'); // => works...
    this.data = data; 
    this.submitData();// => works
  }