问题描述
我有一个问题.
在google.script运行的正式网页,他们说您可以使用google.script.run从客户端调用任何服务器端功能".
In the formal web page of google.script run, they saids that you can call "any server-side function" from client side using google.script.run.
在下面的gs文件中,我使用常规函数表达式定义了"hoge"函数.("this!"行)
In the below gs file, I defined function "hoge" using normal function expression.(the "this!" row)
如果我执行这种情况,则输出是在浏览器上随机显示的1-4个数字
If I execute this situation, output is randomly 1-4 numbers displayed on browser
顺便说一句,我试图更改函数"hoge"的定义样式.我使用匿名函数创建了3个模式.(所有这些都是使用"hoge(vv)"从客户端调用的)
By the way, I tried to change the define style of function "hoge". I created 3 pattern using anonymous function. (all are called from client side using "hoge(vv)")
-
var hoge = function hoge(x){return x;};
(双方都使用"hoge"关键字)→然后,它的工作方式与普通函数定义样式相同. -
var hoge = function(x){return x;};
(仅使用"hoge"关键字左移)→错误 -
var hogeNot =函数hoge(x){return x;};
(仅使用"hoge"关键字正确)→错误
var hoge = function hoge(x){return x;};
(both side using "hoge" keyword) → then this worked same as normal function definition style.var hoge = function (x){return x;};
(only left using "hoge" keyword) → errorvar hogeNot = function hoge(x){return x;};
(only right using "hoge" keyword) → error
问.为什么"1"效果很好,但"2"却是错误.
谢谢.
// gs file
var x;
function doGet() {
return HtmlService.createTemplateFromFile("hello").evaluate(); // テンプレートオブジェクトの取得
}
function hoge(x){ // this!
return x;
}
// html file
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p id="wi">hello</p>
<script>
function success(get){
document.getElementById("wi").insertAdjacentHTML("afterend","<p>" + get + "</p>");
}
for (var v=1; v <= 4; ++v){ // aaを4回呼ぶ
aa(v);
}
async function aa(vv){
await google.script.run.withSuccessHandler(success).hoge(vv);
}
</script>
</body>
</html>
推荐答案
Q.为什么"1"表示工作正常,但"2"表示是错误的.
Q. Why, "1" work well, but "2" is error.
对于这个问题,这个答案怎么样?请认为这只是几个可能的答案之一.
For this question, how about this answer? Please think of this as just one of several possible answers.
在Google Apps脚本中,似乎可以通过脚本编辑器识别该功能,并且可以在 this
上看到该功能,则可以直接运行该功能.为了检查该功能是否包含在 this
中,可以使用以下脚本.
At Google Apps Script, it seems that when the function can be recognized with the script editor and the function can be seen at this
, the function can be directly run. For checking whether the function is included in this
, the following script can be used.
function myFunction() {
for (var i in this) {
if (i == "hoge") {
Logger.log("%s, %s", i, typeof this[i])
}
}
}
关于 var hoge = function hoge(x){return x;};
在这种情况下,可以在脚本编辑器中看到 hoge
的功能,并且该功能可以由脚本编辑器直接运行.而且,上面的脚本返回 hoge,function
.
About var hoge = function hoge(x){return x;};
In this case, the function of hoge
can be seen at the script editor and this function can be directly run by the script editor. And also, above script returns hoge, function
.
在这种情况下,在脚本编辑器中看不到 hoge
的功能,而上面的脚本返回了 hoge,function
.而且 hoge
不能直接运行,因为这在脚本编辑器中看不到.
In this case, the function of hoge
cannot be seen at the script editor while above script returns hoge, function
. And hoge
cannot be directly run because this cannot be seen at the script editor.
从其他功能运行 hoge
的此功能时,脚本将起作用.
When this function of hoge
is run from other function, the script works.
在这种情况下,在脚本编辑器中无法看到 hogeNot
的功能.但是 hoge
的功能可以在脚本编辑器中看到.当 hoge
的功能由脚本编辑器运行时,会出现类似未找到该功能的错误.在上述脚本中, i =="hoge"
始终为 false
.但是,如果语句使用 i =="hogeNot"
,则会返回 hogeNot函数
.
In this case, the function of hogeNot
cannot be seen at the script editor. But the function of hoge
can be seen at the script editor. When the function of hoge
is run by the script editor, an error like the function is not found occurs. At the above script, i == "hoge"
is always false
. But when i == "hogeNot"
is used for the if statement, hogeNot, function
is returned.
从其他功能运行 hogeNot
的此功能时,脚本将起作用.但是,当 hoge
的此功能从其他功能运行时,会发生错误.
When this function of hogeNot
is run from other function, the script works. But when this function of hoge
is run from other function, an error occurs.
在上述情况下,为了使用 google.script.run
运行,需要能够直接在脚本编辑器上运行该功能.我认为这可能是Google方面的规范.
From above situations, it is considered that in order to run with google.script.run
, it is required to be able to directly run the function at the script editor. I think that this might be the specification of Google side.
如果我误解了你的问题,而这不是你想要的方向,我深表歉意.
If I misunderstood your question and this was not the direction you want, I apologize.
这篇关于GAS中3个匿名函数的输出不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!