![](/img/trans.png)
[英]Google Apps Script google.script.run.withSuccessHandler failing
[英]google.script.run.withSuccessHandler(onSuccess). onSuccess doesn't works inside constructor
我在Google Apps Script Web App中使用客户端/服务器通信时遇到了一些问题。
我的code.gs包含:
function loadPetName() {
Logger.log('Client ask a Pet Name');
// use Google apps UserProperties as default storage
var storage = PropertiesService.getUserProperties();
return JSON.parse(storage.getProperty('name');
}
Pet.html是:
<script>
// CLASS Pet
function Pet() {
// CONSTRUCTOR
console.log('Creating new Pet');
var petName;
// Get pet name from outer storage
console.log('Loading pet name from storage...');
google.script.run.withSuccessHandler(onSuccessLoad).loadPetName();
var onSuccessLoad = function(name) {
console.log('run onSuccessLoad');
this.petName = name;
alert('Pet Name is ' + name)
};
// METHODS ...
}
</script>
init.html是:
<script>
function init() {
console.log('Initialization');
var cat = new Pet();
}
</script>
index.html包含:
<body onload=init()>
...
在浏览器中打开index.html之后,我在控制台中看到:
Initialization
Creating new Pet
Loading Pet Name from storage...
但是没有“运行onSuccessLoad”。
在Google脚本控制台中,我们可以看到:“客户端询问宠物名称”。 一切正常,但是onSuccessLoad函数未运行。
如果在Pet.html中,我们替换
google.script.run.withSuccessHandler(onSuccessLoad).loadPetName();
至
google.script.run.withSuccessHandler(console.log('SuccessHandler fired')).loadPetName();
我们将在控制台中看到:“ SuccessHandler被解雇”。
我的问题在哪里?
谢谢!
我发现了一个错误...天哪!
该代码不起作用:
google.script.run.withSuccessHandler(onSuccessLoad).loadPetName();
var onSuccessLoad = function(name) {
console.log('run onSuccessLoad');
this.petName = name;
};
但是这段代码可以正常工作:
var onSuccessLoad = function(name) {
console.log('run onSuccessLoad');
this.petName = name;
};
google.script.run.withSuccessHandler(onSuccessLoad).loadPetName();
但为什么?! (很抱歉这个愚蠢的问题)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.