Skip to content Skip to sidebar Skip to footer

Is It Possible To Send Js Variables To Google Sheets? (not Form Data)

I have some variables that get created depending on what a user selects on my web app. I want to send those variables to a google sheet - is this possible? I already send data from

Solution 1:

It looks that the core of your question is

How to to create the fetch()'s body argument in such way that it includes both the form data object and other values from JavaScript variables.

new FormData(form) returns a FormData object.

To add a value not included on the HTML form to an existing FormData use FormData.append()

One way to apply the above to the code to add one value is to replace

fetch(scriptURL, { method: 'POST', body: newFormData(form)})
                        .then(response =>console.log('Success!', response))
                        .catch(error =>console.error('Error!', error.message))

by something like:

var something = 'New value';
var fetchBody = new FormData(form);
fetchBody.append('newKey',something);
fetch(scriptURL, { method: 'POST', body: fetchBody})
                        .then(response => console.log('Success!', response))
                        .catch(error => console.error('Error!', error.message))

To add more variables, add more fetchBody.append() as needed.

Note: I didn't tested the above yet.

Related Q

References

Solution 2:

While using a doPost function could work, it could be more convenient to use google.script.run to send data from a bounded script attached to a Google spreadsheet as it's suggested on HTML Service: Communicate with Server Functions.

Related

Post a Comment for "Is It Possible To Send Js Variables To Google Sheets? (not Form Data)"