0
votes

I am trying to call a function from a web app I have created using google.script.run the function simply helps me find the index of a value in a list. When I call the function normally it works fine, when I call it from the web app however, it always returns 'undefined'. Even when I pass it the same values? I first thought it was because the arrays were global so I try to define them in the function and still no luck. Can anyone see anything I can be missing? I tried using with the failureHandler, but the script isn't failing per se

I have even tried using the onSuccessHandler after finding a similar thread on here saying that's how you return a value from a server-called function..but still no luck

below is the function i am trying to call

function findStockCode(stockName){

  var stockListi  = ss.getRangeByName("stockName").getValues().filter(String);
  var stockCodesi = ss.getRangeByName("stockCodes").getValues().filter(String);

  for(i = 0; i < stockListi.length; i++){
    if(stockListi[i].toString() === stockName.toString()){
      return stockCodesi[i].toString();
    }
  }
  return "not found"
}

and here is how I am calling it:

 $("#productSelection1").change(function(){

          alert(google.script.run.withSuccessHandler(onSuccessCode).findStockCode("SMK SALMON TRIM W/PEPPER"));

     $("#productCode1").html("code");
   });

eventually I want the productCode1 value to change to a corresponding product code when the product name is picked in productSelection1

1
Your return from findStockCode, will be sent to a function called onSuccessCode, once the execution of the findStockCode function is done on the server side.show us your onSuccessCode. Google.script.run.function() calls your function on the script and moves to the next line (asynchronous call) their is no waiting or return from it.Jack Brown
To add to what Jack said, Your alert should be inside a function named onSuccessCode. function onSuccessCode(code){alert(code)}TheMaster

1 Answers

0
votes

I think this will work for you:

$("#productSelection1").change(function(){
    google.script.run
    .withSuccessHandler(function(str){
      alert(str);
    })
    .findStockCode("SMK SALMON TRIM W/PEPPER"));
    $("#productCode1").html("code");
});