问题
I have a question.
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.
In the below gs file, I defined function "hoge" using normal function expression.(the "this!" row)
If I execute this situation, output is randomly 1-4 numbers displayed on browser
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;};
(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
Q. Why, "1" work well, but "2" is error.
Thank you.
// 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>
回答1:
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.
Experiment:
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.
Sample script:
function myFunction() {
for (var i in this) {
if (i == "hoge") {
Logger.log("%s, %s", i, typeof this[i])
}
}
}
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
.
About var hoge = function (x){return x;};
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.
When this function of hoge
is run from other function, the script works.
About var hoge = function (x){return x;};
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.
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.
Result:
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.
来源:https://stackoverflow.com/questions/59386046/different-output-in-3-anonymous-function-in-gas