Reputation: 9574
I am trying to call a http rest url using Google Sheets custom function.
This works
function getProductDetailsbyEAN(EANCode) {
EANCode = '8901719255137'
if (EANCode == undefined) {
Logger.log("EANCode undefined") ;
return "undefined EANCode"
}
//EANCode = "'" + EANCode + "'"
Logger.log(EANCode)
var formData = {
'gtins': [EANCode],
};
Logger.log(formData.toString())
var options = {
'method' : 'post',
'headers' : { 'Authorization':'Bearer mysecuritycode', 'content-type': 'application/json' },
'payload' : JSON.stringify(formData)
};
var URL = "http://myurl"
var response = UrlFetchApp.fetch(URL, options);
response = response.toString() ;
if (response.indexOf("returned code 404") != -1) {
Logger.log("ResponseError");
Logger.log(response) ;
return "ResponseError" ;
}
var jsonResponse = JSON.parse(response) ;
var jsonResponseItems = jsonResponse['items']
var jsonResponseZerothItem = jsonResponseItems[0] ;
Logger.log(jsonResponseZerothItem) ;
return jsonResponseZerothItem ;
SpreadsheetApp.flush();
}
But if I try to get this working via the parameter it does not, returns 404
This does not work
function getProductDetailsbyEAN(EANCode) {
//EANCode = '8901719255137'
if (EANCode == undefined) {
Logger.log("EANCode undefined") ;
return "undefined EANCode"
}
EANCode = "'" + EANCode + "'"
Logger.log(EANCode)
var formData = {
'gtins': [EANCode],
};
Logger.log(formData.toString())
var options = {
'method' : 'post',
'headers' : { 'Authorization':'Bearer myscuritycode', 'content-type': 'application/json' },
'payload' : JSON.stringify(formData)
};
var URL = "http://myurl"
var response = UrlFetchApp.fetch(URL, options);
response = response.toString() ;
if (response.indexOf("returned code 404") != -1) {
Logger.log("ResponseError");
Logger.log(response) ;
return "ResponseError" ;
}
var jsonResponse = JSON.parse(response) ;
var jsonResponseItems = jsonResponse['items']
var jsonResponseZerothItem = jsonResponseItems[0] ;
Logger.log(jsonResponseZerothItem) ;
return jsonResponseZerothItem ;
SpreadsheetApp.flush();
}
I am missing something really basic on parameter passing. Please advice.
Upvotes: 0
Views: 37
Reputation: 201378
I think that in your case, it seems that EANCode
is required to be used as the string. So how about this modification?
Please modify the below script in your question as follows.
EANCode = "'" + EANCode + "'"
EANCode = EANCode.toString();
8901719255137
is less than 9007199254740991
which is Number.MAX_SAFE_INTEGER. So I thought that you can use EANCode.toString()
.getProductDetailsbyEAN(EANCode)
. So if above modification was not the direct solution, can you provide the script for calling getProductDetailsbyEAN(EANCode)
?If I misunderstood your question and this was not the result you want, I apologize.
Upvotes: 2