admin管理员组文章数量:1356597
How would I structure this to return multiple values (message and name), and be able to access them in the js.html
file?
--code.gs
function createArtistTable(name)
{
var message = "test";
//return message and name
}
--js.html
function openArtistTable(name)
{
google.script.run
.withSuccessHandler(openSuccess)
.withFailureHandler(openFailure)
.createArtistTable(name)
}
function openSuccess(//have 2 values here -- var1, var2)
{
console.log(var1);
console.log(var2);
}
EDIT:
I've fixed the problem. Thank you all for the help and information. This is what I changed:
How would I structure this to return multiple values (message and name), and be able to access them in the js.html
file?
--code.gs
function createArtistTable(name)
{
var message = "test";
//return message and name
}
--js.html
function openArtistTable(name)
{
google.script.run
.withSuccessHandler(openSuccess)
.withFailureHandler(openFailure)
.createArtistTable(name)
}
function openSuccess(//have 2 values here -- var1, var2)
{
console.log(var1);
console.log(var2);
}
EDIT:
I've fixed the problem. Thank you all for the help and information. This is what I changed: http://pastebin./Ci1e8ZWx
Share Improve this question edited Sep 20, 2016 at 4:51 user4411473 asked Sep 20, 2016 at 2:59 user4411473user4411473 2091 gold badge4 silver badges10 bronze badges 6- 2 Either you can return it as an array or as an object. – Karpak Commented Sep 20, 2016 at 3:01
-
1
return an object like
{message: message, name: name}
– CoderLim Commented Sep 20, 2016 at 3:03 - @CoderGLM How would I access it in the openSuccess function? – user4411473 Commented Sep 20, 2016 at 3:07
-
call
createArtistTable
inopenSuccess
– CoderLim Commented Sep 20, 2016 at 3:09 - @CoderGLM Something like var x = createArtistTable().message ? – user4411473 Commented Sep 20, 2016 at 3:10
1 Answer
Reset to default 5A function can only return one value.
So the way to do this is to wrap them together inside an array or object.
function return2Vals()
{
var var1;
var var2;
//Code that does stuff with var1 and var2
///
///
//Create an array with the values and return it.
var results = [var1, var2];
return results;
}
Using the result:
var vals = return2Vals();
console.log("One of the return values is:", vals[0]);
console.log("The other return value is:", vals[1]);
Alternatively you could use an object and basically do whatever you want by using an object:
function returnSomeValsAsObj()
{
var var1;
var var2;
//Code that does stuff with var1 and var2
///
///
//Create an object with the values and return it.
var results = {primary_result: var1, secondary_result: var2, acpanying_message: "some message"};
return results;
}
Using:
var results = returnSomeValsAsObj();
console.log(results.primary_result);
console.log(results.secondary_result);
console.log(results.acpanying_message);
本文标签:
版权声明:本文标题:javascript - Return multiple values and access them? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743959622a2568780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论