samdaily34
samdaily34

Reputation: 67

Is there a way to pass a URLFetchApp.fetch from the gs side to javascript side

I am trying to parse an XML document tree on a button click. The XML file is fetched in a lookup function that takes 2 values ("id" and "shipping") and appends it onto the appropriate URL, then a JSON.parse appends the relevant data into the appropriate text filed "address". I need to dynamically populate another input line with the results of this parse, but the URLFetch is only available on the code.gs side. Is there a way to pass the array from the gs side to my javascript side, or is my best bet an XMLHttpRequest function?

I have tried the url fetch in the js side, as well as a new function "httpGet"

URLFetch Function:

document.getElementById('lookup').addEventListener('click',patientLookup);
 function personLookup(){
 var patient = document.getElementById('id').value;
 var shippingStyle = document.getElementById('shipping').value;
 var address = document.getElementById('address');

 var url = "someurl?patientID=" + patient;
 var shippingAddress = UrlFetchApp.fetch(url,{'muteHttpExceptions':true});
 Console.log(shippingAddress)     
 var addressData = JSON.parse(shippingAddress.getContentText());
 var shipAdd = addressData.Address;

 var city = addressData.City;

 var state = addressData.State;

 var zip = addressData.Zip;

 address.value = shipAdd + "," + city + "," + state + "," + zip;



 };

HttpGet function:

  function httpGet(theUrl)
 {
     var xmlHttp = new XMLHttpRequest();
     xmlHttp.open( "GET", theUrl, false ); // false for synchronous 
     request
     xmlHttp.send( null );
     return xmlHttp.responseText;
 } 

Obviously the error that URLFetch is only supported on the gs side and then the httpGet function not working how I need it, I am also not as familiar with a function like this as I am with the fetch function.

Upvotes: 0

Views: 113

Answers (1)

jdavis
jdavis

Reputation: 448

You need to write the function on the GS side and then pass the variables to it via google.script.run.googleScriptFunction(id,shipping) where id and shipping are the values you want to pass to the google script function.

You may find this documentation helpful

Upvotes: 1

Related Questions