Reputation: 4650
I have two functions created with Google Script and I need to pass the variable from the first function to the second one.
Code:
function fetch() {
var endpoint = 'login';
var url = 'https://example.com/api/'+endpoint;
var payload = {
'username' : "user",
'password' : "pass",
}
var options = {
'method' : 'post',
'payload': JSON.stringify(payload),
};
var urlResponse = UrlFetchApp.fetch(url, options);
}
The following function is called when I press a button:
function logResponse(){
Logger.log(urlResponse); //This must return the data from the urlResponse variable
}
Upvotes: 0
Views: 115
Reputation: 3337
please tell me if this is your scenario:
function fetch()
is called when you run the script (opening a document or on a doGet) and the second one logResponse()
is called from an action on the interface (spreadsheet button or google.script.run....).
In this scenario the script is 'run' twice and everything is forgoten beetween the two runs. what you could do is: A/ pass the value you got from the first function to the user interface and back to the second function B/ store the value you got in the first function with something like:
function fetch() {
var endpoint = 'login';
var url = 'https://example.com/api/'+endpoint;
var payload = {
'username' : "user",
'password' : "pass",
}
var options = {
'method' : 'post',
'payload': JSON.stringify(payload),
};
var urlResponse = UrlFetchApp.fetch(url, options);
PropertiesService.getUserProperties().setProperty("urlResp", urlResponse.getContentText());
}
function logResponse(){
var urlResponse = PropertiesService.getUserProperties().getProperty("urlResp");
Logger.log(urlResponse); //This must return the data from the urlResponse variable
}
propertiesService can only store text (and you can use something else than userproperty like scriptproperty, or documentproperty
Upvotes: 0
Reputation: 309
I think you might want to accomplish the following:
function fetch() {
var endpoint = 'login';
var url = 'https://example.com/api/'+endpoint;
var payload = {
'username' : "user",
'password' : "pass",
}
var options = {
'method' : 'post',
'payload': JSON.stringify(payload),
};
var urlResponse = UrlFetchApp.fetch(url, options);
return urlResponse;
}
function logResponse(){
Logger.log(fetch());
}
So you can use "return" keyword to return the value from function on its call.
Then you don't need to declare variable outside of the function (for example in global scope).
You just need to call and pass it as a parameter to your logResponse()
function.
So, pay attention to these lines:
return urlResponse;
Logger.log(fetch())
P.S. Of course declaring a variable in outer scope which is accessible for both function also works. But my solution above seems a better option.
Also some suggestions:
fetch()
to fetchUrl
to be more explicit.urlResponse
in the first function is redundant and you can do just:return UrlFetchApp.fetch(url, options);
Update:
Pavel, look at the Option 1 which Jeremy has suggested in the answer. In short words, as a suggestion you can wrap your piece of code (depending on your environment) into Immediately-invoked function expression:
Take a look the following example (pay attention to the comments) ->
(function () {
// all the inside of this function is a shared scope for two function below
'use strict';
// this is a declared variable which is accessible for 2nd function
// the value assigned will be the result of calling the 1st function
var urlResponse = fetch();
function fetch() {
var endpoint = 'login';
var url = 'https://example.com/api/'+endpoint;
var payload = {
'username' : "user",
'password' : "pass"
};
var options = {
'method' : 'post',
'payload': JSON.stringify(payload)
};
// return the value
return UrlFetchApp.fetch(url, options);
}
function logResponse() {
// use the shared variable (the value of it is a result of calling fetch() function
Logger.log(urlResponse);
}
// then if you want to log your response
// call the function
logResponse();
})();
Upvotes: 1
Reputation: 23873
function fetch() {
var endpoint = 'login';
var url = 'https://example.com/api/'+endpoint;
var payload = {
'username' : "user",
'password' : "pass",
}
var options = {
'method' : 'post',
'payload': JSON.stringify(payload),
};
// This variable ONLY exists inside of the fetch function.
// once this function exits, it is gone.
var urlResponse = UrlFetchApp.fetch(url, options);
}
function logResponse(){
// Nope, this is an entirely SEPARATE variable with the same name.
Logger.log(urlResponse); //This must return the data from the urlResponse variable
}
Solutions: Either a) Pass in the value to logResponse() or move the var UrlResponse
to an outer scope available to both function. Keep the assignment, however, where it is.
Option 1
var urlResponse;
function fetch() {
var endpoint = 'login';
var url = 'https://example.com/api/'+endpoint;
var payload = {
'username' : "user",
'password' : "pass",
}
var options = {
'method' : 'post',
'payload': JSON.stringify(payload),
};
// This variable ONLY exists inside of the fetch function.
urlResponse = UrlFetchApp.fetch(url, options);
}
function logResponse(){
// Nope, this is an entirely SEPARATE variable with the same name.
Logger.log(urlResponse); //This must return the data from the urlResponse variable
}
Option 2
function fetch() {
var endpoint = 'login';
var url = 'https://example.com/api/'+endpoint;
var payload = {
'username' : "user",
'password' : "pass",
}
var options = {
'method' : 'post',
'payload': JSON.stringify(payload),
};
// This variable ONLY exists inside of the fetch function.
var urlResponse = UrlFetchApp.fetch(url, options);
logResponse(urlResponse);
}
function logResponse(urlResponse){
Logger.log(urlResponse); //This must return the data from the urlResponse variable
}
Upvotes: 0
Reputation: 5998
A little bit explanation:
When you pass your variable, declared above to the function (like in example) a closure will be created. And function will get access to this variable.
var a = 11;
function setVar() {
a = '777';
}
function logVar(){
Logger.log(a);
}
Upvotes: 0
Reputation: 3962
Your two functions (setVar()
and logVar()
) have different scopes, meaning they can't access variables / entities within one another. What you want to do is create a variable outside of these two, so that each function can access the same variable as such:
var a = 0;
function setVar () {
a = 10;
}
function logVar () {
Logger.log (a);
}
Upvotes: 1