google-apps-script - 将 Apps Script HTML 中的客户端`JavaScript fetch().then()` 转换为 google.script.run
问题描述
这个问题在以下 Stack Overflow 链接中得到解答:
此问题特定于google.script.run
API for Apps 脚本。我需要使用google.script.run.serverFncName()
调用在服务器上运行代码,然后在运行下一个函数之前等待返回。我尝试转换的代码示例使用 JavaScript fetch().then()
。
我尝试转换的代码示例:
<script>
window.some_Object_Name = {
innerNameOne : function() {
return fetch('The URL goes here for HTTP Request', {
method: 'post'
}).then(function(response_Back_From_Request) {
return res.json()response_Back_From_Request;
});
},
}
</script>
需要使用google.script.run.myServerAppsScriptFnc()
而不是fetch(url)
显然,类似以下的内容不起作用,但我出于说明目的展示它:
<script>
window.some_Object_Name = {
innerNameOne : function() {
google.script.run
.withSuccessHandler(function(response_Back_From_Request) {return response_Back_From_Request;})
.myServerAppsScriptFnc({method:'post'});
.then(function(response_Back_From_Request) {
return response_Back_From_Request;
});
},
}
</script>
我假设我需要使用Promise
<script>
window.makeCallToGoogleServer = function() {
google.script.run
.withSuccessHandler(function(rtrnFromServer) {return rtrnFromServer;})
.myServerAppsScriptFnc({method:'post'});
}
window.some_Object_Name = {
innerNameOne : function() {
let myPromise = new Promise(function(myOnSuccessFnc) {
makeCallToGoogleServer();
myOnSuccessFnc(response_Back_From_Request);
});
myPromise.then(
return fromFirstFunc;
);
}
}
</script>
解决方案
推荐阅读
- c# - 正则表达式积极前瞻没有按我的预期工作
- android - android:screenOrientation 在清单纵向/锁定中哪个是最好的以及它们之间的区别
- python - 为什么这个过滤后的噪声信号不像我预期的那样表现?
- java - 如何以编程方式下载 SSL 客户端证书
- angular - 无法使用插值从 get 方法返回值。不从 getTitle() 方法打印“this._title”值
- getstream-io - 它是否能够获取用户的所有活动?
- python - 使其列表可以通过该功能
- php - HTML/PHP/JQUERY - 多个“警报”名称
- flutter - 处理文本字段数据,将其保存在本地文本文件中并随后显示
- r - 如何使用 R 在 t.test 中仅运行比较子集?