SamYoungNY SamYoungNY - 1 year ago 91
JSON Question

Getting a specific part of e.parameter

I have a Google App Script that writes form data to a spreadsheet upon submission on my site.

I have many different forms, and I'd like to write their data to the same Spreadsheet, but to different 'Sheets' (i.e tabs @ bottom "form1, form2, form3") within.

Each form has a hidden input that names it. I think that by isolating the input name, I can write an

if statement
that pushes the form data to the right sheet.

example of HTML:

<input type="hidden" name="form-type" value="form1">
*Name: <input type="text" name="john">

<input type="hidden" name="form-type" value="form2">
*Name: <input type="text" name="peter">

Javascript on website - handling the data for POST'ing to Google App Script:

function handleFormSubmit(event) { // handles form submit withtout any jquery
event.preventDefault(); // we are submitting via xhr below
var data = getFormData(); // get the values submitted in the form

var url =;
var xhr = new XMLHttpRequest();'POST', url);

xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

// url encode form data for sending as post data
var encoded = Object.keys(data).map(function(k) {
return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]);
console.log("ENCODED IS: " + encoded);

The encoded data looks like this:

ENCODED IS: form-type=form1&name=john
ENCODED IS: form-type=form2&name=paul

The Google App Script function responsible for populating the spreadsheet is:

try {
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
var sheet = doc.getSheetByName('form1'); // here is where to define which sheet a response should be added to
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow()+1; // get next row
var row = [ new Date() ]; // first element in the row should always be a timestamp
// loop through the header columns
for (var i = 1; i < headers.length; i++) { // start at 1 to avoid Timestamp column
if(headers[i].length > 0) {
row.push(e.parameter[headers[i]]); // add data to row
// more efficient to set values as [][] array than individually
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
catch(error) {
finally {

How do I access the specific
) that I need in order to be able to discern which form was used and route it accordingly?

For more info you can reference (")

Answer Source

From the doGet parameter documentation:

The second parameter is called parameter, and it is an Object containing each parameter and value pair

So, something like:

var type = e.parameter['form-type'];

Should work?