Siddharth
Siddharth

Reputation: 9574

Google Sheets variable does not work, hardcode string does

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

Answers (1)

Tanaike
Tanaike

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.

From:

EANCode = "'" + EANCode + "'"

To:

EANCode = EANCode.toString();

Note:

  • 8901719255137 is less than 9007199254740991 which is Number.MAX_SAFE_INTEGER. So I thought that you can use EANCode.toString().
  • From your question, I cannot confirm the method for calling getProductDetailsbyEAN(EANCode). So if above modification was not the direct solution, can you provide the script for calling getProductDetailsbyEAN(EANCode)?

Reference:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 2

Related Questions