【发布时间】:2020-10-29 10:30:43
【问题描述】:
我正在使用 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;
}
}
我需要能够使用常规数据设置 this.data...或者至少从 this.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
我的输出总是“给我测试”看起来辅助函数无法访问 GLOBAL SCOPE 变量...
【问题讨论】:
-
如果你在辅助函数中
console.log(this),输出是什么?它可能指向不同的对象,因此请尝试以下操作:google.script.run.withSuccessHandler(helper.bind(this)).getData(); -
您需要等到数据检索完毕。
-
@Reyno - 没有帮助...
-
我的输出总是“给我测试”你是怎么得到输出的?
-
也许你的 Successhandler 永远不会因为 Apps 脚本函数 getData() 失败而被调用?实现一个FailureHandler 来验证。另外,如果您认为这可能是问题的原因,请提供 getData() 的代码以进行故障排除
标签: javascript google-apps-script google-workspace