![](/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.