Is it possible to transfer a URLFetchApp.fetch request from the Google Apps Script side to the JavaScript side?

My current task involves parsing an XML document tree upon clicking a button. The XML file is obtained using a lookup function that requires two values ("id" and "shipping") to be inserted into the appropriate URL. Then, the data retrieved is parsed using JSON.parse and added to the "address" text field. Now, I am faced with the challenge of dynamically populating another input line with the results of this parse. Unfortunately, the URLFetch function is only accessible on the code.gs side. Is there a way to transfer the array from the gs side to my JavaScript side, or should I resort to using an XMLHttpRequest function?

I have experimented with performing the URL fetch on the JavaScript side and even tried implementing a new function called "httpGet".

URLFetch Function:

document.getElementById('lookup').addEventListener('click',personLookup);
 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;
 } 

Despite realizing that URLFetch is restricted to the gs side and encountering issues with the httpGet function not meeting my requirements, I am also less familiar with this approach compared to the fetch function.

Answer №1

To implement the feature, first create the function on the GS side and then transmit the variables to it using google.script.run.googleScriptFunction(id,shipping) where id and shipping represent the values to be sent to the google script function.

For more guidance, refer to this detailed guide

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mysterious and never-ending loop that seems to loop endlessly and eludes my

My prototype includes a method for adding callbacks: /* * Add a callback function that is invoked on every element submitted and must return a data object. * May be used as well for transmitting static data. * * The callback function is supposed to e ...

Do developers typically define all flux action types within a constants object as a common programming practice?

This question arises from an informative article on flux. The common approach involves defining all action types within a constants object and consistently referencing this object throughout the application. Why is it considered a common practice? What ...

Encountering an issue with Next.js React SSR using styled-jsx: unable to access the 'state' property as

I've come across a problem that I can't seem to figure out. Despite my attempts to search for a solution here, I haven't been able to help myself. I'm new to javascript and react, so please bear with me. Issue: I'm using React (1 ...

Importing dynamic NodeJS modules from one module

ModuleA src index.js modules utility.js ModuleB src framework activities activity.js ModuleA serves as the main "runnable" module in this setup, while ModuleB acts as a framework li ...

Reversing the order of input-group-addon and input in bootstrap on mobile devices

I attempted to adjust the layout of a bootstrap input-group-addon on mobile devices by using two input elements and manipulating their display and visibility properties. From a design standpoint, I achieved the desired result as the input is now positione ...

Learn how Angular 2 allows you to easily add multiple classes using the [class.className] binding

One way to add a single class is by using this syntax: [class.loading-state]="loading" But what if you want to add multiple classes? For example, if loading is true, you want to add the classes "loading-state" and "my-class". Is there a way to achieve t ...

How can I incorporate a child component into a separate component within Angular version 14?

Currently working with Angular 14 and facing a challenge with including a child component from another module into a standalone component. The structure of the standalone component is as follows: <div> <child-component></child-component& ...

Checking if an array exists after parsing JSON using jQuery

I am working on a solution to automatically submit form fields after triggering the onchange event. After making some changes, the JSON response I receive appears in this format: { "data": [ { "name":"p_data1,KEY=1", "value":"2", "error":"no error" } ] } ...

Cannot adjust expiration date of express-session in browser

In my current project, I am utilizing express-session. Let's say a session has been created between the web browser and the Node.js server with a default expiration time of one hour. At this point, there is a cookie named connect.sid stored in the use ...

Exploring and identifying matching pairs within a JavaScript object

Currently, I have a JavaScript object that looks like this: records: [ { id: 1, name: michael, guid: 12345 }, { id: 2, name: jason, guid: 12345 }, { id: 3, name: fox, guid: 54321 }, { id: 4, ...

Error: Attempts to access the 'avatar' property of a null value result in a TypeError

I've been attempting to showcase both an avatar and the user name, but I keep encountering this error. Despite trying to declare a user variable to troubleshoot, it's not resolving the issue. Error: Cannot read property 'avatar' of n ...

Enhancing event listener using AngularJS and Jasmine: spying on callback functions

Can someone assist me with spyOnning a method connected to an event using scope.$on in a factory service, using Jasmine? The actual method is being called instead of the spy. Here is a plinkr I created showcasing the issue: http://plnkr.co/edit/2RPwrw?p=pr ...

Is it possible to execute an ajax function at regular intervals until a specific condition is fulfilled?

My application consists of two JSP pages. One is responsible for the UI, while the other processes the response. UICounter.jsp: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Page</titl ...

How can I transform JSON data into a dataframe in R, even when the lists are uneven and contain some missing values? Check out the example provided

I have a JSON file named json_data containing lists with different column lengths (16 & 15). I need to create a single data frame with the list data in JSON. How can I approach this problem? data2 [1] "{\"2020-01-01\": [{\"contactsPerPage ...

Establish a connection between the MySql database in WHM (phpmyadmin) and a node.js application

Our team has been working on setting up the database connection with Mysql using node.js in Cpanel. Although I didn't create the database myself, I have all the necessary information such as the host, user, password, name of the database, and port nu ...

Menu changes when hovering

I want to create an effect where hovering over the .hoverarea class will toggle the visibility of .sociallink1, .sociallink2, and so on, with a drover effect. However, my code isn't working as expected. Additionally, an extra margin is automatically ...

Trouble with Bootstrap Collapse feature not collapsing on its own

I recently added a Bootstrap collapse feature to my payment view in Laravel. The Bootstrap section collapses when clicked, but I would like it to be collapsed by default. I understand that I need to include: aria-expanded="false" However, it still doesn& ...

Quarkus API routes for downloading and uploading JSON documents

I need to develop endpoints for downloading and uploading group configurations in the form of a JSON file. The configuration is currently stored in the database as a JSON object, so when a user wants to download the configuration, it needs to be converted ...

Despite element being a grandchild, the function this.wrapperRef.current.contains(element) will return false

The issue arises when using the EditModal component with an onClickOutside event. This component includes a child element, a Material-UI Select, where clicking on a MenuItem triggers the onClickOutside event, causing the modal to close without selecting th ...

Can you explain the contrast between uploading files with FileReader versus FormData?

When it comes to uploading files using Ajax (XHR2), there are two different approaches available. The first method involves reading the file content as an array buffer or a binary string and then streaming it using the XHR send method, like demonstrated he ...